Hi Friends!
My problem is that I have to make a program where the results of my query can be extracted into csv file. The problem is when I declare the actions in the button of extract and run it, it display nothing. I can't see anything wrong with my code Or I just misplace it. I need help, this is my code:
<html>
<h1> TC/GC Per Sales Frequency Bracket </h1>
<body>
<?php
$df=$POST['df'];
$dt=$POST['dt'];
$db='mds_reports';
echo 'Date From'.$df.'<br />';
echo 'Date To'.$dt.'<br />';
// Connection to the Database
if($connect = mysql_connect("172.16.8.32","mds_reports","password"))
$connect=mysql_select_db($db);
else die ("Unable to connect".mysql_error());
// Design of the Page
echo '<form action="TCFrequencyBracket.php" method="post">';
echo '<table>';
echo '<tr><td>Date From :</td><td><input name="df" type="text" /></td></tr>';
echo '<tr><td>Date To :</td><td><input name="dt" type="text" /></td></tr>';
echo '<tr><td><input name="submit" type="submit" id="submit" value="submit"/></td></tr>';
echo '</table>';
echo '</div>';
echo '</div>';
echo '</form>';
// When Submit Button is clicked
if (isset($_POST['submit']))
{
echo '<table border="1" cellspacing="1">';
echo '<th>Restaurant Code</th>';
echo '<th>Restaurant Name</th>';
echo '<th>100 & BELOW</th>';
echo '<th>101-200</th>';
echo '<th>201-300</th>';
echo '<th>301-400</th>';
echo '<th>401-500</th>';
echo '<th>501-600</th>';
echo '<th>601-700</th>';
echo '<th>701-800</th>';
echo '<th>801-900</th>';
echo '<th>901-1000</th>';
echo '<th>1001 & ABOVE</th>';
echo '<th>TOTAL TC</th>';
// The query
$sql = "SELECT restaurant_master.code, restaurant_master.name,
COUNT(CASE WHEN mds_orders.GrossTotal < '100'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '100' AND mds_orders.GrossTotal < '200'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '200' AND mds_orders.GrossTotal < '300'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '300' AND mds_orders.GrossTotal < '400'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '400' AND mds_orders.GrossTotal < '500'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '500' AND mds_orders.GrossTotal < '600'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '600' AND mds_orders.GrossTotal < '700'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '700' AND mds_orders.GrossTotal < '800'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '800' AND mds_orders.GrossTotal < '900'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '900' AND mds_orders.GrossTotal < '1000'
THEN 1 END),
COUNT(CASE WHEN mds_orders.GrossTotal >= '1000'
THEN 1 END),
COUNT(mds_orders.GrossTotal)
FROM mds_orders
JOIN restaurant_master
ON mds_orders.RestaurantID = restaurant_master.PKID
WHERE mds_orders.OrderDate BETWEEN '$df' AND '$dt'
AND mds_orders.StatusFKID = 2
GROUP BY restaurant_master.code, restaurant_master.name";
$result = mysql_query($sql)
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "There are $num_rows records.<br>";
// Display the results
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $fields)
print "\t<td>$fields</td>\n";
print "</tr>\n";
}
echo '</table>';
// Another Button for extraction
echo '</div>';
echo '</div>';
echo '<table>';
echo '<tr><td><input name="extract" type="submit" id="extract" value="extract" onclick="extract();"/></td></tr>';
echo '</table>';
// Function for extraction
function extract(){
$out = '';
if (isset($_POST['extract'])) {
$out .= $_POST['extract'];
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel;");
header("Content-disposition: attachment" . date("Y-m-s") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print stripslashes($out);
exit;
}
}
?>
</body>
</html>
Best Regards. Thanks in Advance.
🙂 😕 :o :bemused: