2012-11-21 21 views
2

print_r($getPrice)結果爲:$用getPrice包含不知道的元素,它可以1或2或類似下面的(3片)如何使用php輸出並在jQuery中進行比較?

Array 
(
    [0] => Array 
     (
      [price_qty] => 2 
      [price] => 100.0000 

     ) 

    [1] => Array 
     (

      [price_qty] => 5 
      [price] => 90.0000 
     ) 

    [2] => Array 
     (

      [price_qty] => 8 
      [price] => 80.0000 

     ) 

) 

jQuery代碼:get all the input value and make an addition

$(document).ready(function() { //wrap in a document.ready event handler 
    $('input').on('keyup', function() { 
     var result = 0; 
     $('.liste_couleur_qty li input').each(function() { 
      result += parseInt(this.value, 10); 
     }); 
     $('div#qtyvalue').text(result); 
    }); 
});​ 

現在,我想要得到的結果與[price_qty]$getPrice陣列進行比較。如果結果小於0,則使用結果乘以[price](來自$ getPrice數組)的值。

如:

if result(from the jquery code) < 2, then price= result*110. then $('div#qtyvalue').text(result); change to $('div#qtyvalue').text("the qty is "+result+" the price is "+price); 

if between 2--5(contain 5), the price is 100. if between 5--8(not contain 8), the price is 90 

怎麼辦呢?謝謝。

是否有把PHP數組轉換成JavaScript數組,然後進行比較的方法嗎?

回答

3

使用PHP的json_encode功能的陣列$getPrice轉換成JSON字符串。然後將其分配給JavaScript變量。

更新:

<?PHP 

$getPrice = array(
array("price_qty" => 2,"price" => 100.0000), 
array("price_qty" => 4,"price" => 300.0000), 
array("price_qty" => 6,"price" => 500.0000) 
); 

$json = json_encode($getPrice); 

?> 

<script> 
    $(document).ready(function() { 
    var parsedData = <?PHP echo $json; ?>; 

    for(k in parsedData) 
     alert(parsedData[k].price_qty); 

    }); 
</script> 
+0

解析它在jQuery的我不知道該怎麼辦/。你可以給我更多的details.many感謝 – stackoverflow002

+0

@ stackoverflow002:請查看更新的答案.. – mithunsatheesh

+0

元素$用getPrice包含不知道,它可以1或2或其他 – stackoverflow002

相關問題