【问题标题】:foreach error with PHP but $.each working in JSPHP 的 foreach 错误,但 $.each 在 JS 中工作
【发布时间】:2012-06-19 01:56:57
【问题描述】:

好的,我有一个小问题,我正在尝试将此 jQuery 转换为 PHP

我需要做的是把它放在一个 foreach 函数中,这是我首先要做的 jQuery 代码。

$.each(obj.products, function(i, obj) {

    if(obj.barcode == barcode)
    {
        $("#showmoreinfohere").show();
        $("#moremovietop").html(obj.name.toUpperCase());
        $("img#selectedproduct").attr('src', ''+obj.logoURL+'');
        $("span#perticket").html(obj.price);
        currentproduct["barcode"] = obj.barcode;
        currentproduct["priceperticket"] = obj.price;
        currentproduct["cashbackperticket"] = obj.discount;
        $("span#VIPCashBack").html(obj.discount);
        total = obj.price * $("#qtyselect").val();
        $("span#totalprice").html("$"+total);
    }
});

我的 PHP 代码

<?php
    $cartdata = $fetch->cartitems($_COOKIE["sessionkey"]);
    foreach ($cartdata as $cart)
    {
        $product_details = $fetch->getbarcode('$cart["barcode]"');
    ?>

    <tr>
        <?php 
        foreach ($product_details as $product)
        {
        ?>

        <td><?php $product['name']?></td>
        <td><?php $product['price']?></td>
        <td><?php $cart['qty']?></td>
        <td><?php $product['discount']?></td>
        <?
        }
        ?>

    <?php
    }

 ?> 

我得到的错误是

警告:为 foreach() 提供的参数无效 /home/movies/public_html/tpl/cart.tpl 在第 27 行

谷歌搜索的问题这是我发现的:

  1. 您可以在 foreach 中执行 foreach
  2. product_details 的 JSON 返回以下内容

      {
       "_id": ObjectId("4f6ab67338fc5ded4f000000"),
       "company": "village",
       "logo": "http: \/\/...\/villagetop.png",
       "products": {
         "0": {
           "barcode": "236690091",
           "name": "Weekday",
           "logoURL": "http: \/\/...\/ticketpic1.png",
           "price": "12.50",
           "discount": "1.50" 
        },
        ...
      },
       "store_name": "movies" 
    }
    

【问题讨论】:

  • PHP 代码是做什么的?它会给你任何错误吗?还是根本不起作用?
  • 这不会显示任何内容,因为您没有回显任何内容
  • 报错信息的哪一部分是你没有具体理解的?

标签: php jquery foreach


【解决方案1】:

编辑

在尝试寻找答案之前,建议您在脚本顶部添加以下两行:

error_reporting(-1);
ini_set('display_errors', 'On');

编辑 2

从 JSON 转储中,您应该在 foreach 循环中使用 $product_details-&gt;products$product_details['products'],具体取决于您调用 json_decode() 的方式。


以下代码:

$product_details = $fetch->getbarcode('$cart["barcode]"');

不太可能工作,它的正确代码:

$product_details = $fetch->getbarcode($cart['barcode']);

在你的循环中:

<td><?php $product['name']?></td>

这不会输出任何东西,你想要这样的东西:

<td><?php echo htmlspecialchars($product['name']); ?></td>

【讨论】:

  • @RussellHarrower 你可能需要$product_details-&gt;products 或 $product_details['products']` 在你的 foreach 中。
【解决方案2】:

由于产品详细信息未以数组形式返回,因此您收到了错误消息。可能可以这样修复它:

<?php

$cartdata = $fetch->cartitems($_COOKIE["sessionkey"]);
foreach ($cartdata as $cart)
{
    /* This is returning null or invalid data so it is erroring on the loop below....  
       Maybe this function could return array() if empty so it passes the loop in the   
       block below. Otherwise a check is needed before entering the loop.
    */  

    $product_details = $fetch->getbarcode($cart['barcode']);
?>

    <tr>
        <?php             
        // Errors here due to null or invalid data
        foreach ($product_details as $product)
        {
        ?>
            <td><?php echo $product['name']?></td>
            <td><?php $product['price']?></td>
            <td><?php $cart['qty']?></td>
            <td><?php $product['discount']?></td>
      <? } 
}?>

编辑:看到您发布了 json,但是当您运行它时会返回什么?

 $product_details = $fetch->getbarcode($cart['barcode']);
 var_dump( $product_details  );    

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2017-12-27
    • 2016-06-21
    • 1970-01-01
    • 2013-01-12
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多