gpt4 book ai didi

javascript - 在 Javascript 警告框中的新行中显示 PHP 数组的每个条目

转载 作者:行者123 更新时间:2023-11-30 09:54:09 24 4
gpt4 key购买 nike

我有一个简单的查询来从数据库中选择一些信息:

  $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>";
}

如果有更好的方法,我很乐意接受建议!

最佳答案

Try this to show like array values on each line

alert(arrayname.join("\n"));

Try this to show like an array:

alert(JSON.stringify(your_array));

首先用php数组值准备好一个js变量。像这样

<script type="text/javascript">
var obj = <?php echo json_encode($Your_array); ?>;
</script>

A full Tried and tested example below

<?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>

关于javascript - 在 Javascript 警告框中的新行中显示 PHP 数组的每个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34853215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com