akien_ghie09 0 Newbie Poster

Hi,

I have a problem with the way my program run. The situation is that I have a form where it ask for the date then when the button submit is clicked the records which has the same date will display. After that there is a button extract where if i clicked it the results will export into a CSV file.

This is my code:

<html>
	<h1> TC/GC Per Sales Frequency Bracket </h1>
		<body>
			<?php	header("Content-type: application/vnd.ms-excel;");
				// header("Content-type: text/plain; charset=utf-8");
				header("Content-disposition: attachment" . date("Y-m-d") . ".csv");
				header("Content-disposition: filename=".$filename.".csv");

				$df = $_POST['df'];
				$dt = $_POST['dt'];
				$db = 'mds_reports';
				
				echo 'Date From'.$df.'<br />';
				echo 'Date To'.$dt.'<br />';

				if($connect = mysql_connect("172.16.8.32", "mds_reports", "password"))
				$connect = mysql_select_db($db);
					else die ("Unable to connect".mysql_error());

				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>';

				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>';

					$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";
					// echo $sql;
					
					$result = mysql_query($sql)
					          or die("SELECT Error: ".mysql_error());
					$num_rows = mysql_num_rows($result);
					print "There are $num_rows records.<br>";

					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>';

					echo '<form action = "TCFrequencyBracket.php" method = "post">';					
					echo '<div>';
					echo '</div>';
					echo '<table>';
					echo '<br><input name = "extract" type = "submit" id = "extract" value = "extract"/></td><td></br></br>';
					echo '<tr><td></table></td></td>';

					$out = '';

					if (isset($_POST['extract'])) {
					$out .= $_POST['extract'];
					}

					$filename = $file."_".date("Y-m-d_H-i",time());

					print stripslashes($out);

					exit;
			       }
			?>
		</body>
</html>

Now the error is when I try to open the program I make a prompt is already displayed even if the main page haven't been displayed :-/. And also the content of the file is the code not the result.

Your help is much appreciated. Best Regards

:$ :confused: :idea:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.