Problem in using foreach and while loop in displaying data on table format
Hi...
I tried to use foreach in displaying my table header, but I encountered problem when I tried to display data on the first row , my query only display the last Sum for the last Comp.
here is my code:
PHP Code:
<html>
<head>
<title>Half Shell</title>
<link rel="stylesheet" type="text/css" href="kanban.css" />
<?php
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set("Asia/Singapore"); //set the time zone
$con = mysql_connect('localhost', 'root','');
$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";
$res_comp = mysql_query($sql, $con);
while($row_comp = mysql_fetch_assoc($res_comp)){
$Comp[] = $row_comp['Comp'];
}
echo "<th> </th>";
foreach($Comp AS $Comp){
echo "<th>$Comp</th>";
}
echo "<tr>
<td>Total Kg/Compound</td>";
$sql_sec = "SELECT SUM(TotalKg) AS TotalKg FROM sales_order WHERE Comp = '$Comp' ORDER BY Comp";
$res_sec = mysql_query($sql_sec, $con);
Can you isolate your problem? I mean copy this code in a new page and get rid of all code that is not necessary there in such way that you can specify exactly your problem.
$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";
$res_comp = mysql_query($sql, $con);
while($row_comp = mysql_fetch_assoc($res_comp)){
$Comp[] = $row_comp['Comp'];
}
echo "<tr><th> </th>";
foreach($Comp AS $Comp){
echo "<th>$Comp</th>";
}
echo "<tr><td>Total Kg/Compound</td>";
$Compound = array();
$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";
$res_comp = mysql_query($sql, $con);
while($row_comp = mysql_fetch_assoc($res_comp)){
$Compound[] = $row_comp['Comp'];
}
foreach($Compound AS $Compound)
{
$sql_sec = "SELECT SUM(TotalKg) AS TotalKg FROM sales_order WHERE Comp = '$Compound' GROUP BY Comp ORDER BY Comp";
# add error code compliant with the rest of error code management you are already using
$result = mysql_query($sql_sec, $con) ;
Working days is SUM of workingdays of 3 months, I have code for getting the consecutive 3 months but now I got a problem in getting the SUM of working days.
foreach( monthNames($FromMonth, $ToMonth) as $month){ echo $month,'<br>';
$sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE Month_Name = '$month' ORDER BY MonthName";
$res = mysql_query($sql, $con);
$row = mysql_fetch_assoc($res);
$WorkingDays = $row['WorkingDays'];
echo "<td>$WorkingDays</td>";
}
but I got this error:
Apr
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\Program Files\xampp\htdocs\MES PROJECT\HalfShellParam.php on line 147
May
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\Program Files\xampp\htdocs\MES PROJECT\HalfShellParam.php on line 147
Jun
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\Program Files\xampp\htdocs\MES PROJECT\HalfShellParam.php on line 147
Could you try using mysql_fetch_array instead of mysql_fetch_array
And WorkingDays is a string and it should be a number, that is why you are receiving those warnings
I tried this for testing before I put on my forms:
PHP Code:
<?php
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set("Asia/Singapore"); //set the time zone
$con = mysql_connect('localhost', 'root','');
if (!$con) {
echo 'failed';
die();
}
mysql_select_db("mes", $con);
$sql = "SELECT FromMonth, ToMonth FROM so_month";
$res = mysql_query($sql,$con);
foreach( monthNames($FromMonth, $ToMonth) as $month){
//$sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE Month_Name IN ('.implode( ',' , $month_ ).') ORDER BY MonthName";
$sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName";
$res = mysql_query($sql, $con);
$SumWorkingDays = 0;
while($row = mysql_fetch_array($res)){
Instead of just printing the result, now print row by row, with some additional information like MonthName, you will find the answer there I mean something like
PHP Code:
$sql = "SELECT MonthName, WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName"; $res = mysql_query($sql, $con);
<div id="fieldset_PS">
<?php
echo "<table>";
//--List of Compounds----//
$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";
$res_comp = mysql_query($sql, $con);
while($row_comp = mysql_fetch_assoc($res_comp)){
$Comp[] = $row_comp['Comp'];
}
echo "<tr><th> </th>";
foreach($Comp AS $Comp){
echo "<th>$Comp</th>";
}
//------Total Kg/Compound----//
echo "<tr><td>Total Kg/Compound</td>";
$Compound = array();
$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";
$res_comp = mysql_query($sql, $con);
while($row_comp = mysql_fetch_assoc($res_comp)){
$Compound[] = $row_comp['Comp'];
}
foreach($Compound AS $Compound)
{
$sql_sec = "SELECT SUM(TotalKg) AS TotalKg FROM sales_order WHERE Comp = '$Compound' GROUP BY Comp ORDER BY Comp";
# add error code compliant with the rest of error code management you are already using
$result = mysql_query($sql_sec, $con) ;
foreach( monthNames($FromMonth, $ToMonth) as $month){
$sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName";
$res = mysql_query($sql, $con);
$SumWorkingDays = 0;
while($row = mysql_fetch_array($res)){
}
}
//-------Demanded per day-----//
echo "<tr><td>Demand per day (Kg)</td>";
$DemandedPerDay = $TotalKg / $SumWorkingDays;
$Compound = array();
$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";
$res_comp = mysql_query($sql, $con);
while($row_comp = mysql_fetch_assoc($res_comp)){
$Compound[] = $row_comp['Comp'];
}
foreach($Compound AS $Compound)
{
$sql_sec = "SELECT SUM(TotalKg) AS TotalKg FROM sales_order WHERE Comp = '$Compound' GROUP BY Comp ORDER BY Comp";
# add error code compliant with the rest of error code management you are already using
$result = mysql_query($sql_sec, $con) ;
echo "<tr><td>Batched per day</td>";
$Compound = array();
$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";
$res_comp = mysql_query($sql, $con);
while($row_comp = mysql_fetch_assoc($res_comp)){
$Compound[] = $row_comp['Comp'];
}
foreach($Compound AS $Compound)
{
$sql_sec = "SELECT DISTINCT CompKg FROM sales_order WHERE Comp = '$Compound' GROUP BY Comp ORDER BY Comp";
Firstly get rid of the @
Again check row by row, or where the error occurs print the data and check what information you have there, as a general rule, before dividing by something ALWAYS check that the divisor<>0
and from your jpg I also noticed that your divisor is very very big in compared to your dividend and in such cases you are going to have a quotient<0.02 probably due to that you are only getting zeros.
Here is my suggestion for future issues: Break the program until the point where you know it is working correctly and print out the results where it is not working and check the data, and start making changes from there.
Bookmarks