Page 1 of 1

Downloading Data

Posted: Fri Apr 08, 2005 5:00 am
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

Posted: Fri Apr 08, 2005 5:32 am
by JayBird
did you ask before?

We answered then didnt we?

viewtopic.php?t=30941&highlight=csv+download

Posted: Fri Apr 08, 2005 6:00 am
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

Posted: Fri Apr 08, 2005 6:17 am
by JayBird
No probs.

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

Mark

Posted: Fri Apr 08, 2005 6:39 am
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??

Posted: Fri Apr 08, 2005 8:36 am
by mohson
can anyone help with this?

Posted: Fri Apr 08, 2005 8:49 am
by JayBird
put

Code: Select all

Header ("Content-type: text/csv");
At the very top of the script...first line

Posted: Fri Apr 08, 2005 8:58 am
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";
}
 
?>

Posted: Fri Apr 08, 2005 9:05 am
by JayBird
try lower case H or header

Posted: Fri Apr 08, 2005 9:09 am
by Chris Corbyn
Pimptastic wrote:try lower case H or header
And the content-disposition header too :wink:

Posted: Mon Apr 11, 2005 5:30 am
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;
 
?>

Posted: Mon Apr 11, 2005 10:35 am
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

Posted: Mon Apr 11, 2005 10:40 am
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";
}

?>

Posted: Mon Apr 11, 2005 10:43 am
by JayBird
can i see the script in action somehow?

Posted: Wed Apr 13, 2005 7:59 am
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']
	);

 
?>