I am storing dates from a query to an array by using while loop and array_push.
$h = array();
$j = array();
while ($g = mysql_fetch_array($f)) {
$date = new DateTime($g['selected_date']);
echo "<th>" . date_format($date, 'd') . "</th>";
array_push($h, $date);
array_push($j, $g['selected_date']);
}
Printing that array returns to this which is fine, I think.
Array (
[0] => 2016-05-23
[1] => 2016-05-24
[2] => 2016-05-25
[3] => 2016-05-26
[4] => 2016-05-27
[5] => 2016-05-28
)
I try to use like this:
$a = mysql_query("select * from center") or die(mysql_error());
$arr = array();
foreach($j as $k) {
while ($b = mysql_fetch_array($a)) {
$l = "select sum(yid.center_inventory) as 'total'
from yl_inventory_details as yid
left join yl_inventory as yi
on yi.yl_inventory_no = yid.yl_inventory_no
where yid.center_no = '$b[center_no]'
and yi.date = '$k'";
$c = mysql_query("$l") or die(mysql_error());
$d = mysql_fetch_array($c);
echo "<tr><td>".$b['center']."</td><td>".$d['total']."</td></tr>";
}
}
But it only takes the first value which is 2016-05-23
. The while loop doesn't seem to proceed to the next foreach value. How can I fix this?
When you use mysql_fetch_array
it shifts onto the next row in the result. Once you reach the last result, it starts returning FALSE
.
To make your code work, all you have to do is swap the loops the other way round.
Eg
$a = mysql_query("select * from center") or die(mysql_error());
$arr = array();
while ($b = mysql_fetch_array($a))
{
foreach($j as $k)
{
$l = "select
sum(yid.center_inventory) as total
from yl_inventory_details as yid
left join yl_inventory as yi on yi.yl_inventory_no = yid.yl_inventory_no
where yid.center_no = {$b['center_no']} and yi.date = {$k}";
$c = mysql_query($l) or die(mysql_error());
$d = mysql_fetch_array($c);
echo
"<tr>
<td>{$b['center']}</td>
<td>{$d['total']}</td>
</tr>";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.