UDT for CSV File Download

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
boaks
New Member
New Member
Posts: 3
Joined: Fri Nov 12, 2010 4:12 pm

UDT for CSV File Download

Post by boaks »

Hey all, I'm new to cmsms so hopefully you can help. 

I have a DB that contains user registrations for events.  I want to make a UDT to create a csv file for download so that the users can see the registered users for that event.  I have the php done that creates the file and lets the dl occur.  It works great outside of cmsms.

When i create the udt and call in from my page, the download occurs, execpt all the html is output into the csv file above the database records.  Any idea how i can get this to work.

Here is the code in my UDT Called Download_Registrations:

Code: Select all

$host = 'xxxxx';
$user = 'xxxxx';
$pass = 'xxxx';
$db = 'xxxx';
$table = 'user_reg';
$file = 'export';

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) 
{
	while ($row = mysql_fetch_assoc($result)) 
	{
		$csv_output .= $row['Field'].", ";
		$i++;
	}
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) 
{
	for ($j=0;$j<$i;$j++) 
	{
		$csv_output .= $rowr[$j].", ";
	}
	
	$csv_output .= "\n";
}


print $csv_output;
exit;
then in my page i call the udt with {Download_Registrations}

the output file starts with the tag and continues down until my db records start.  how can i get rid of the html code in this file that is generated from cmsms?  Any other ideas?
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: UDT for CSV File Download

Post by Wishbone »

You can have your UDT save the data to a file (unique file name), then give the URL to the file for the user to download. Have your UDT remove files older than an hour so that your disk doesn't fill up with CSV files.
Last edited by Wishbone on Fri Nov 12, 2010 5:36 pm, edited 1 time in total.
boaks
New Member
New Member
Posts: 3
Joined: Fri Nov 12, 2010 4:12 pm

Re: UDT for CSV File Download

Post by boaks »

so instead of this line:

Code: Select all

print $csv_output;
I would write the file to a directory on the server somewhere.  I got that, but how would i use the UDT to output the link to the file? Not real familiar with how the UDT's interact with cmsms. 
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: UDT for CSV File Download

Post by Wishbone »

Use something like:

Code: Select all

echo 'Click <a href="$csv_file">HERE</a> to download the CSV file.';
assuming that $csv_file contains the full path to the file name.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: UDT for CSV File Download

Post by Wishbone »

Correction.

Code: Select all

echo "Click <a href=\"$csv_file\">HERE</a> to download the CSV file.";
Last edited by Wishbone on Sat Nov 13, 2010 5:28 pm, edited 1 time in total.
boaks
New Member
New Member
Posts: 3
Joined: Fri Nov 12, 2010 4:12 pm

Re: UDT for CSV File Download

Post by boaks »

Thanks this is going to work.
octavian.cretu
New Member
New Member
Posts: 4
Joined: Thu Aug 27, 2009 3:12 pm

Re: UDT for CSV File Download

Post by octavian.cretu »

i have a similar problem. i want to create a function in a module which would return xml or json. now it appears the template processed and only at the end appears the json/xml code.
i tried writing in the module and stopping from processing the template by using echo and then exit but this did not help.

the solution mentioned above is not an acceptable one for me.

other suggestions are welcome.
thanks
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: UDT for CSV File Download

Post by Wishbone »

Assign your page to a template that just contains {content}... Your UDT content will be the only data outputted.
octavian.cretu
New Member
New Member
Posts: 4
Joined: Thu Aug 27, 2009 3:12 pm

Re: UDT for CSV File Download

Post by octavian.cretu »

it is a module, not a page.
the method is to use the CreateLink method to create a URL to the page that you want...
Add parameter showtemplate=false to the URL (to the array parameters)
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: UDT for CSV File Download

Post by Wishbone »

octavian.cretu wrote: it is a module, not a page.
the method is to use the CreateLink method to create a URL to the page that you want...
Add parameter showtemplate=false to the URL (to the array parameters)
Sorry.. I misread your post. Instead of showtemplate=false, have you tried pointing the page to a template with just {content} in it? I tried the showtemplate=false method, and I still ended up with , etc.
Post Reply

Return to “Developers Discussion”