I am try to export CSV report, for this first i display the table and at the bottom of this page i create a one button which is used to export the report, but its not seem to working. please check out my code,,,any help is appriciate.
if(isset($_POST['submit']))
{
$selectcondition = "SELECT DATE FROM baltimore where DATE='".$_POST['search']."'"; //Select Query Which check the date whether the checklist for the given date is exit or not.
$check=mysql_query($selectcondition,$con);
$fetch=mysql_fetch_row($check);
if(!$fetch) //If checklist for the given date is not exit execute if condition
{
echo '"<font color="red">';
echo "<br>";
echo "No Checklist Found In The Database";
echo "<br>";
echo "</font>";
}
else //if checklist for the given date is exit into the database execute this else part
{
$sql= "select DATE,JOBNAME,TIME,INITIALS,TIMESTAMP from $table where DATE='".$_POST['search']."'";
$comment= "select COMMENTS,DATE from comments where DATE='".$_POST['search']."'";
$my_comments=mysql_query($comment,$con);
$mydata=mysql_query($sql,$con);
echo "<table width='100%' height='10%' border='1' cellspacing='0'>
<tr>
<th>DATE</th>
<th>JOBNAME</th>
<th>TIME</th>
<th>INITIALS</th>
<th>SYSTEM TIMESTAMP</th>
</tr>";
while($records = mysql_fetch_array($mydata))
{
echo "<tr>";
echo "<td>" . $records['DATE'] . " </td>";
echo "<td>" . $records['JOBNAME']. " </td>";
echo "<td>" . $records['TIME'] . " </td>";
echo "<td>" . $records['INITIALS'] . " </td>";
echo "<td>" . $records['TIMESTAMP'] . " </td></tr>";
}
$records_comments = mysql_fetch_array($my_comments);
echo "<tr>";
echo '<td >';
echo "<h4>COMMENTS</h4>";
echo '</td><td colspan="2" >';
echo $records_comments['COMMENTS'];
echo '</td></tr>';
echo "</table>";
echo "<br>";
echo "<b> Generate Report </b>";
echo "<br>";
echo "<center>";
echo "<input type='submit' name='csv' value='CSV Report'>";
}
if(isset($_POST['csv']))
{
$filename = 'uploads/'.strtotime("now").'.csv';
$fp=fopen($filename,"w");
$selectcondition = "SELECT ID,DATE,JOBNAME,TIME,INITIALS FROM sample where DATE='".$_POST['search']."'";
$mydata=mysql_query($selectcondition,$con);
$row=mysql_fetch_assoc($mydata);
$seperator="";
$comma="";
foreach($row as $name => $value)
{
$seperator .= $comma . '' .str_replace('','""',$name);
$comma = ",";
}
$seperator .= "\n";
fputs($fp,$seperator);
mysql_data_seek($mydata,0);
while($row=mysql_fetch_assoc($mydata))
{
$seperator="";
$comma="";
foreach($row as $name => $value)
{
$seperator .= $comma . '' .str_replace('','""',$value);
$comma = ",";
}
$seperator .= "\n";
//echo $seperator;
fputs($fp,$seperator);
}
fclose($fp);
}
}
?>
</form>
</body>
</html>