Downloading Data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Downloading Data

Post by mohson »

Ok, I want my users to run a mailmerge from the data they view on the web application.

the best way to do this is to allow them to download the data and then they can just use excell and word to do the mail merge.

Im having problems with how to go about dowloading data, I was told that the 'trick is to create a link to the website adding '\download' to the url or something like this.

anyone got any advice
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

did you ask before?

We answered then didnt we?

viewtopic.php?t=30941&highlight=csv+download
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

sorry pimpster posted the question on the wrong forum - I wanted to see what the guys on 'sitepoint' had to say about it.

Im now going back to the advice that you guys gave - only problem was that last time loads of people chipped in and gave so many conflicting views on what was the easiest way that I got confused, ive now decided im gonna do it your way - using the export_csv method and and then linkinkg to the download page.

Cheers
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

No probs.

All methods achieve the same reuslts...just whatever works best for you.

Mark
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Mark This is what Ive tried

Ive created a file called export_csv.php

heres the code for that file:

Code: Select all

<?php

/* Connecting, selecting database */
$link = mysql_connect("xxxxx", "xxx", "xxxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");


Header ("Content-type: text/csv"); 
 
//This is my code, change your queries and formatting
 
$query = "SELECT * FROM people ORDER BY organisation";
 
$result = mysql_query($query) or die(mysql_error());
 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) &#123;
	echo "".$line['person_id'].",".$line['salutation'].",".$line['firstname'].",
	".$line['surname'].",".$line['organisation'].",".$line['role'].",
	".$line['address1'].",".$line['address2'].",".$line['city'].",".['postcode'].",
	".$line['telephone'].",".$line['mobile'].",".$line['fax'].",
	".$line['dateoflastcontact'].","$line.['datecontactagain'].",
	".$line['notes'].",".$line['email'].",".$line['org_id']."\n";
&#125;
 
?>
Ok I have then created a link on my view 'People' Page that reads like this:
when I click on the link all that happens that the browsers searchs for the site then states 'done' in the bottom corner all I am left with is a blank white screen.

any ideas as to whats going wrong??
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

can anyone help with this?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

put

Code: Select all

Header ("Content-type: text/csv");
At the very top of the script...first line
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

did it and the same thing happens???

Code: Select all

<?php
Header ("Content-type: text/csv"); 

/* Connecting, selecting database */
$link = mysql_connect("xxxx", "xxx", "xxxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");



 
//This is my code, change your queries and formatting
 
$query = "SELECT * FROM people ORDER BY organisation";
 
$result = mysql_query($query) or die(mysql_error());
 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	echo "".$line['person_id'].",".$line['salutation'].",".$line['firstname'].",
	".$line['surname'].",".$line['organisation'].",".$line['role'].",
	".$line['address1'].",".$line['address2'].",".$line['city'].",".['postcode'].",
	".$line['telephone'].",".$line['mobile'].",".$line['fax'].",
	".$line['dateoflastcontact'].","$line.['datecontactagain'].",
	".$line['notes'].",".$line['email'].",".$line['org_id']."\n";
}
 
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try lower case H or header
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Pimptastic wrote:try lower case H or header
And the content-disposition header too :wink:
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Ive also tried this but again the page loads with a blank screen and nothing downloads - am I missing something here or should something happen when I get to the export_csv link.




Code: Select all

<?php
header ("Content-type: text/csv"); 

/* Connecting, selecting database */
$link = mysql_connect("xxxx", "xxx", "xxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");



 
//This is my code, change your queries and formatting
 
$query = "SELECT * FROM people ORDER BY organisation";
 
$result = mysql_query($query) or die(mysql_error());
 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) &#123;
	echo "".$line['person_id'].",".$line['salutation'].",".$line['firstname'].",
	".$line['surname'].",".$line['organisation'].",".$line['role'].",
	".$line['address1'].",".$line['address2'].",".$line['city'].",".['postcode'].",
	".$line['telephone'].",".$line['mobile'].",".$line['fax'].",
	".$line['dateoflastcontact'].","$line.['datecontactagain'].",
	".$line['notes'].",".$line['email'].",".$line['org_id']."\n";
&#125;
 
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

im using that exact same code and when i click the link i get the standard pop-up download dialog.

Im stumped as to why it isn't working for you
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

this is the exact code i am currently using

Code: Select all

<?php 
header ("Content-type: text/csv"); 

$BasePath = str_repeat("../", substr_count(dirname($_SERVER["SCRIPT_NAME"]), "/"));

require ($BasePath."ocdaintranet/includes/db_connection.php");

$query = "SELECT * FROM contacts ORDER BY company";

$result = mysql_query($query) or die(mysql_error());

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	echo "".$line['id'].",".$line['company'].",".$line['name'].",".$line['position'].",".$line['address'].",".$line['notes'].",".$line['tel'].",".$line['direct_line'].",".$line['fax'].",".$line['mobile'].",".$line['email'].",".$line['web']."\n";
}

?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

can i see the script in action somehow?
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Ive had a look over my downloading data code and ive finally got it to work im experiencing one problem though all the data is currently being printed on screen - I was told by a technical expert that I need to change the header code to one which opens a file save box rather than print the raw data on screen - can someone help with regards to the correct header information.

the working code is below:

Code: Select all

<?php
header ("Content-type: text/csv"); 

/* Connecting, selecting database */
$link = mysql_connect("xxxx", "xxx", "xxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");



 
//This is my code, change your queries and formatting
 
$query = "SELECT * FROM people ORDER BY organisation";
 
$result = mysql_query($query) or die(mysql_error());
 
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) 
	
	printf ("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
	$line['person_id'],$line['salutation'],$line['firstname'],
	$line['surname'],$line['organisation'],$line['role'],
    $line['address1'],$line['address2'],$line['city'],$line['postcode'],
	$line['telephone'],$line['mobile'],$line['fax'],
	$line['dateoflastcontact'],$line['datecontactagain'],
	$line['notes'],$line['email'],$line['org_id']
	);

 
?>
Post Reply