Page 1 of 1

UDT for CSV File Download

Posted: Fri Nov 12, 2010 4:20 pm
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?

Re: UDT for CSV File Download

Posted: Fri Nov 12, 2010 5:31 pm
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.

Re: UDT for CSV File Download

Posted: Fri Nov 12, 2010 6:57 pm
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. 

Re: UDT for CSV File Download

Posted: Fri Nov 12, 2010 7:50 pm
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.

Re: UDT for CSV File Download

Posted: Fri Nov 12, 2010 9:16 pm
by Wishbone
Correction.

Code: Select all

echo "Click <a href=\"$csv_file\">HERE</a> to download the CSV file.";

Re: UDT for CSV File Download

Posted: Sat Nov 13, 2010 3:14 pm
by boaks
Thanks this is going to work.

Re: UDT for CSV File Download

Posted: Sun Nov 14, 2010 2:47 am
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

Re: UDT for CSV File Download

Posted: Sun Nov 14, 2010 7:04 am
by Wishbone
Assign your page to a template that just contains {content}... Your UDT content will be the only data outputted.

Re: UDT for CSV File Download

Posted: Sun Nov 14, 2010 2:48 pm
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)

Re: UDT for CSV File Download

Posted: Sun Nov 14, 2010 4:52 pm
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.