【发布时间】:2016-05-27 23:32:56
【问题描述】:
我正在使用 while 循环和 array_push 将日期从 query 存储到数组中。
$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']);
}
我认为打印该数组返回到这很好。
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
)
我尝试这样使用:
$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>";
}
}
但它只取第一个值2016-05-23。 while 循环似乎没有进行到下一个 foreach 值。我该如何解决这个问题?
【问题讨论】:
-
在做任何其他事情之前,请摆脱
mysql_函数调用。这些功能现在已被弃用很长时间。使用mysqli_或 PDO。 -
把你的 foreach 循环放到你的 while 循环中,你就成功了