使用PHP的foreach错误,但$ .each在JS中工作

时间:2012-06-19 01:56:58

标签: php jquery foreach

好的我有一个小问题我试图把这个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()提供的参数无效   第27行/home/movies/public_html/tpl/cart.tpl

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

  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" 
    }
    

2 个答案:

答案 0 :(得分:0)

修改

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

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

修改2

在JSON转储中,您应该在$product_details->products循环中使用$product_details['products']foreach,具体取决于您调用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>

答案 1 :(得分:0)

由于产品详细信息未作为数组返回,因此您收到错误。可能会这样解决它:

<?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  );