我有一个简单的查询来从数据库中选择一些信息:
$dep_qry = "select product_name, sum(theoretical_stock_level) as opening_stock, sum(quantity_scanned) as closing_stock, department_description, stock_item_code, custom_1 AS size, IFNULL(unit_cost_price, 0.00) AS cost_price from stocktake_products where stocktake_id = '{$stocktake_id}' AND is_deli = 0
group by product_name, department_description
order by department_description ASC;";
$dep_res = db::c()->query($dep_qry);
然后,我将以下代码将成本价格为0.00的产品放入数组中。然后我检查数组中的元素数量。如果数组中有一些信息,我想为用户显示一个警告,告诉他哪些项目的成本价格为0.以下代码有效,但我想知道是否可以显示数组中的每个条目警报框中有一行单独的行?只是为了让它更加用户友好。
while ($row1 = $dep_res->fetch(PDO::FETCH_ASSOC))
{
$cost_price = number_format($row1['cost_price'],2);
$pname = $row1['product_name'];
if ($cost_price == 0.00)
{
$products[$i] = $pname;
}
$i++;
}
$numOfNoCosts = count($products);
if ($numOfNoCosts > 0)
{
echo "<script> alert('".json_encode($products)."') </script>";
}
如果有更好的方法,我很乐意接受建议!
答案 0 :(得分:4)
尝试此操作以在每行显示相似的数组值
alert(arrayname.join("\n"));
尝试将其显示为数组:
alert(JSON.stringify(your_array));
首先使用php数组值获取js变量。 喜欢这个
<script type="text/javascript">
var obj = <?php echo json_encode($Your_array); ?>;
</script>
下面的完整测试和测试示例
<?php $phpArray = array(
0 => "Mon",
1 => "Tue",
2 => "Wed",
3 => "Thu",
4 => "Fri",
5 => "Sat",
6 => "Sun",
)
?>
<script type="text/javascript">
var jArray= <?php echo json_encode($phpArray ); ?>;
alert(jArray.join("\n"));
</script>