Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
foreach错误与PHP但$。每个都在JS中工作_Php_Jquery_Foreach - Fatal编程技术网

foreach错误与PHP但$。每个都在JS中工作

foreach错误与PHP但$。每个都在JS中工作,php,jquery,foreach,Php,Jquery,Foreach,好的,我有一个小问题,我正在尝试将这个jQuery转换为PHP 我需要做的是把它放在foreach函数中,这是我首先在jQuery代码中做的 $.each(obj.products, function(i, obj) { if(obj.barcode == barcode) { $("#showmoreinfohere").show(); $("#moremovietop").html(obj.name.toUpperCase());

好的,我有一个小问题,我正在尝试将这个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
    }

 ?> 
编辑

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

error_reporting(-1);
ini_set('display_errors', 'On');
编辑2

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

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

<td><?php echo htmlspecialchars($product['name']); ?></td>
<?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>
      <? } 
}?>

PHP代码做什么?它会给你任何错误吗?或者它根本不起作用吗?这不会显示任何内容,因为您没有回应任何内容。错误消息的哪一部分是您不了解的?RussellHarrower您可能需要在foreach中使用
$product\u details->products
或$product\u details['products']`。