I have a simple query to select some information from the database:
$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);
I then have the following code to put products with a cost price of 0.00 into an array. I then check the number of elements in the array. If the array has some information in it, I want to display an alert for the user to tell him which items have a cost price of 0. The following code works, but I am wondering if its possible to display each entry from the array on a separate line in the alert box? Just to make it more user friendly.
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>";
}
If there is a better way of doing this, I am more than happy to take on suggestions!
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));
first get a js variable ready with the php array value . Like this
<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>
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.