0

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!

2
  • Try writing your javascript code inside document load event as below. document.onreadystatechange = function () { if (document.readyState == "complete") { .. here.. } } Commented Dec 6, 2014 at 8:01
  • You probably meant to write $valueMap[$row['clm_num']] = $row['clm_amnt']]; instead of $valueMap[$row['clm_num'] & $row['clm_amnt']]; Commented Dec 27, 2014 at 3:20

4 Answers 4

2

You should enable PHP displaying errors, which would tell you there is an error while constructing your array.

<?php
 ## Turn on error reporting
 error_reporting(-1);
 ini_set('display_errors', 'On');
 ....
 $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>
4
  • i changed code line to $valueMap[$row['clm_num']] = $row['clm_amnt']; but nothing happened :( it's retrieve nothing. Commented Dec 6, 2014 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. Commented Dec 6, 2014 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 Commented Dec 6, 2014 at 8:13
  • just updated the answer, though it would help if you could show what the desired html output would be. Commented Dec 6, 2014 at 8:28
1

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.

0

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
0

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

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.