2012-06-19 28 views
-2

好吧,我有我想這個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 
    } 

?> 

我得到的錯誤是

警告:在 /家庭/電影/的public_html/TPL /車的foreach()提供的參數無效。第27行上的tpl

發現谷歌搜索這是我發現:

  1. ,你可以做一個foreach
  2. JSON用於product_details內的foreach正在返回以下

    { 
        "_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" 
    } 
    
+1

什麼是PHP代碼?它會給你什麼錯誤?還是它根本就不起作用? – Dre

+0

你會得到什麼錯誤? – xdazz

+0

這不會顯示任何東西,因爲你沒有迴應任何東西 – Satya

回答

0

編輯

試圖找到答案之前,我們建議您在腳本的頂部添加以下兩行:

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

編輯2

從JSON甩了你應該在你的foreach循環使用或者$product_details->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> 
+0

沒有修復foreach錯誤 – RussellHarrower

+0

@RussellHarrower'$ fetch-> getbarcode()'期待什麼? –

+0

它從mongoDB獲取條形碼 – RussellHarrower

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