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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀