doujiu8178 2016-01-18 11:07
浏览 36
已采纳

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

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!

  • 写回答

1条回答 默认 最新

  • douyong4623 2016-01-18 11:09
    关注

    Try this to show like array values on each line

    alert(arrayname.join("
    "));
    

    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("
    "));
    
     </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线