1

I have my data in mysql. I am using PDO to pull data with php then using json_encode to convert to js. When i use following scenario, i always get output NaN and if i use static array it works. So I am guessing it's either wrong json format or something with php.

This works if I set values for array smoothly: codepen: http://codepen.io/pghiran/pen/QEvRjW

Dynamic (pulling data from db and storing in array, doesn't work. My code for doing this:

PHP

$exchangeRates =  array();

$sql = 'SELECT * FROM exchange';

try {
    $stmt = $conn->prepare($sql);
    $stmt->execute();

    for ($i=0; $row=$stmt->fetch();$i++){
        $exchangeRates[] = array($row['rs3'],$row['07'],$row['deadman'],$row['currency']);
    }
}catch(exception $e){
    return $e;
}

var_dump($exchangeRates);

var_dump displays: array(1) { [0]=> array(4) { [0]=> string(3) "0.5" [1]=> string(3) "1.5" [2]=> string(3) "3.5" [3]=> string(1) "$" } }

JS/Jquery:

var amount = $('#amount'),
                    goldtype = $('#goldtype'),
                    exchange= <?php echo json_encode($exchangeRates);?>,
                    price = $('#price');

                    console.log(exchange); 

                    goldtype.change(function () {
                        var exchangeIndex = $("select[name='goldtype'] option:selected").index();
                        price.val(amount.val() * exchange[exchangeIndex]);
                    })

console.log(exchange) displays:

[Array[4]]
0
:
Array[4]
0
:
"0.5"
1
:
"1.5"
2
:
"3.5"
3
:
"$"
length
:
4

https://gyazo.com/b167b38d62d9c4b567f31cc35ce00289

7
  • Because you don't need to json_encode it.. just add [0] to your calling of the data when multiplying it to amount.val() e.g. price.val(amount.val() * exchange[0][exchangeIndex]); Commented Jul 2, 2016 at 20:20
  • var exchange= <?php echo json_encode($exchangeRates);?>, is variable, if i remove this line i have no data. Commented Jul 2, 2016 at 20:26
  • exchange= <?php echo $exchangeRates;?> it is already in an array format similar to the hard coded one you show on codepen. Commented Jul 2, 2016 at 20:27
  • i did what you say and now, i don't even get NaN just blank Commented Jul 2, 2016 at 20:30
  • just add console.log(exchange[0][0] + ' ' + exchange[0][1] + ' ' + exchange[0][2] + ' ' + exchange[0][3]); and check it's collecting the correct data from the array. Commented Jul 2, 2016 at 20:36

1 Answer 1

1
  $exchangeRates[] = array($row['rs3'],$row['07'],$row['deadman'],$row['currency']);

This was saving in exchangeRates[0] only. I had change this line to:

  $exchangeRate = array($row['rs3'],$row['07'],$row['deadman'],$row['currency']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.