6 Replies - 476 Views - Last Post: 01 June 2012 - 07:35 PM Rate Topic: -----

#1 xuany  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 03-March 10

php data not shown

Posted 30 May 2012 - 08:17 AM

Dear guys,
i want the data to be downloaded in csv. please help me to have a look, what goes wrong to my coding, that i only ge the title row in excel, and i do not get the content. Please show how should i amed my program to make the data shown in php.

thank you.

<? ob_start(); ?>
<?php
if(isset($_POST['download']))
{
	$con = mysql_connect("localhost","root","");
	if (!$con) die('could not connect'.mysql_error());
	mysql_select_db("dbregnahc",$con);

$filename = 'NAHC'."_".date("d-m-Y_H-i",time());
$fp = fopen($filename,"w");

$sql=mysql_query("SELECT * FROM vrecord") or die(mysql_error());
$row = mysql_fetch_assoc($sql);

$seperator="";
$comma ="";

foreach($row as $name => $value)
{
$seperator .=$comma .'' .str_replace('','""',$name);
$comma =",";
}
 $seperator .="\n";
 
 echo $seperator;

fputs($fp,$seperator);

mysql_data_seek($sql,0);

while($row = mysql_fetch_assoc($sql))
{
$seperator="";
$comma ="";

foreach($row as $name => $value)
{
$seperator .=$comma .'' .str_replace('','""',$value);
$comma =",";
}
 $seperator .="\n";
 fputs($fp,$seperator);
}
fclose($fp);
}
 
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
 
exit;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>
<? ob_flush(); ?>


Is This A Good Question/Topic? 0
  • +

Replies To: php data not shown

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2954
  • View blog
  • Posts: 10,176
  • Joined: 08-August 08

Re: php data not shown

Posted 30 May 2012 - 08:27 AM

You don't say what the problem is, so I'll just list what I see at a glance:
  • Don't use short tags.
  • Don't break out of PHP and then back into it.
  • Use prepared statements. mysql functions are not secure.
  • What is this trying to do??? str_replace('','""',$name);
  • Use proper indenting!
  • No PHP output means nothing shows up at the browser.

This post has been edited by CTphpnwb: 30 May 2012 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 xuany  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 03-March 10

Re: php data not shown

Posted 30 May 2012 - 02:52 PM

thanks CTphpnwb

my problem is when i click a button from the other page, the action goes to this script. which i want to download the data to csv.
but i only get the title row when i open the csv file. but the content is not shown.

please advise, and i am still a beginner in php, sorry about your previous post that some of the term i'm still not understand. do you mind to highlight.

thank you.
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: php data not shown

Posted 30 May 2012 - 03:12 PM

What I think CTphpnwb is saying is that you have several core issues with your code that you need to look at before you start trying to figure out why only the headers come out.

For example, this line:
	$seperator .=$comma .'' .str_replace('','""',$name);


...seems to be erroneous and redundant in a couple of places. I think what you mean is this:
	$seperator .= $comma . '"' . $name . '"';


If you had properly indented code, didn't switch between single and double quotes, didn't jump in and out of PHP, etc, then the code would be much easier to debug. These are skills you should work on, as it will make your life much easier in the long run.

All this aside, have you tried outputting to screen to see what comes out? I would suggest outputting to screen, then writing it to a file. You could have file-writing issues that are completely separate to what data you are trying to write.
Was This Post Helpful? 1
  • +
  • -

#5 xuany  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 03-March 10

Re: php data not shown

Posted 30 May 2012 - 07:37 PM

thank you.

when i run my code without
header("Content-type: application/vnd.ms-excel");
48
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
49
header( "Content-disposition: filename=".$filename.".csv");
50
  
51
exit;


the content is shown. but when i insert that code, then my content gone.

please advise.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2954
  • View blog
  • Posts: 10,176
  • Joined: 08-August 08

Re: php data not shown

Posted 30 May 2012 - 07:44 PM

Are you trying to do a forced download?

I suggest that you fix the things you've been told are wrong, then if it still isn't working post your current code with a precise description of the problem.
Was This Post Helpful? 0
  • +
  • -

#7 xuany  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 03-March 10

Re: php data not shown

Posted 01 June 2012 - 07:35 PM

thanks guys, i'd found the solution... very much appreciate of your answers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1