2

print_r($getPrice) result is:the elements $getPrice contains is not know, it can 1 or 2 or like the following(3 elements)

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

        )

)

the jquery code: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); 
    });
});​

now, I want to get compare the result with [price_qty] from the $getPrice array. if the result less than 0,then using the result multiply the [price] (from the $getPrice array) value.

eg:

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

How to do it? Thank you.

Is there a way to put the php array into a javascript array and then make the compare?

Community
  • 1
  • 1
stackoverflow002
  • 329
  • 1
  • 3
  • 10

1 Answers1

3

Use the json_encode function of PHP to convert the array $getPrice into a JSON string. then assign it to a JavaScript variable.

Update:

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

?>

to parse it in jQuery with

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

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

    });
</script>
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101