Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have mysql table like this

table = tbl_tst`

clm_num clm_amnt

1 - 25000
2 - 31700
5 - 52900
8 - 45000

I want to get that table data to php array like this

$temp = array([1,25000],[2,31700],[5,52900],[8,45000]);

After i'll convert php array into the javascript using this code

var jsArray = <? echo json_encode($temp); ?>;

Problem is when i run my code it's retrieve nothing. sometimes it's retrieving "Object" :(

This is my full php code

<?php
$con=mysql_connect("localhost","user","pass") or die("Failed to connect with database!!!!");
mysql_select_db("db", $con); 
$query = "SELECT * FROM tblnum"; 
$result = mysql_query($query) or die(mysql_error());
$valueMap = array();
while($row = mysql_fetch_array($result)){
    $valueMap[$row['clm_num'] & $row['clm_amnt']];
}
?>

<script>
var jsArray = <? echo json_encode($valueMap); ?>;
for(var i=0; i < jsArray .length; i++){
document.write("<li>"+jsArray [i]+"</li>");
}
</script>

Please help me to find this issue. Thanks in advance!

share|improve this question
    
Try writing your javascript code inside document load event as below. document.onreadystatechange = function () { if (document.readyState == "complete") { .. here.. } } – Nabil Dec 6 '14 at 8:01
    
You probably meant to write $valueMap[$row['clm_num']] = $row['clm_amnt']]; instead of $valueMap[$row['clm_num'] & $row['clm_amnt']]; – fejese Dec 27 '14 at 3:20

You should enable PHP displaying errors, which would tell you there is a little mistake on building your array in PHP.

<?php
....
$valueMap = array();
while($row = mysql_fetch_assoc($result)){
    $valueMap[$row['clm_num']] = $row['clm_amnt'];
}
?>

edit:

You requested a different sort of array I see:

while($row = mysql_fetch_assoc($result)){
     $valueMap[] = array($row['clm_num'], $row['clm_amnt']);
}

MySQL is no longer maintained, please start using MySQLI or PDO http://rudiv.se/Development/Resource/when-to-use-mysql-vs-mysqli-vs-pdo-in-php

edit:

<?php

$temp = array(
    array(1,2500),  
    array(2,31700)
);

?>

<ul id="list"></ul>

<script>

    var json_array = <?php echo json_encode($temp, true);?>;    

    console.log(json_array);    

    var ul = document.getElementById("list");
    for(i in json_array){       
        var li = document.createElement("li");
        li.appendChild(document.createTextNode(json_array[i][0]+','+json_array[i][1])); 
        ul.appendChild(li);
    }

</script>
share|improve this answer
    
i changed code line to $valueMap[$row['clm_num']] = $row['clm_amnt']; but nothing happened :( it's retrieve nothing. – lakyii Dec 6 '14 at 8:05
    
Lets do it step by step, directly after the while loop, on the next line: echo "<pre>".print_r($valueMap, true)."<pre>"; and excute to see it the php array is generated. – Frankey Dec 6 '14 at 8:09
    
yeah it's fine. it's working perfectly in php. but problem is i need to get that data array like this $temp = array([1,25000],[2,31700],[5,52900],[8,45000]); in javascript – lakyii Dec 6 '14 at 8:13
    
just updated the answer, though it would help if you could show what the desired html output would be. – Frankey Dec 6 '14 at 8:28
    
Now it's working. Thank you! :D – lakyii Dec 6 '14 at 8:59

First you have to check your php array is come or not. if it will be coming than use this code:

<script type='text/javascript'>
  var js_data = <?php echo json_encode($valueMap); ?>;
  var jsArray = js_data.toString().split(',');
  for(var i=0; i < jsArray.length; i++){
    alert(jsArray[i]);
  }
</script>

This one for one array or one dimensional array like array['amount']. i used this code for

$valueMap = array('25000','31700','52900','45000'); // php array

check this.

share|improve this answer

change this $valueMap[$row['clm_num'] & $row['clm_amnt']]; To $valueMap[] =array($row['clm_num'], $row['clm_amnt']);

    $valueMap = array();
    while($row = mysql_fetch_assoc($result)){
        $valueMap[] =array($row['clm_num'], $row['clm_amnt']);
    }
    ?>

<script>
var jsArray = <?php echo json_encode($temp); ?>;//change <? to <?php  it's give error when sort tag is not enable 
for(var i=0; i < jsArray .length; i++){
document.write("<li>"+jsArray [i]+"</li>");
}
</script>

//output

1,25000
2,31700
5,52900
8,45000
share|improve this answer

Try this:

while($row = mysql_fetch_array($result)){
    $valueMap[$row['clm_num']] = $row['clm_amnt'];
}

And then in js:

for(var i in jsArray){
    if (jsArray.hasOwnProperty(i)) {
        document.write("<li>"+jsArray[i]+"</li>");
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.