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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用