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!

Community
  • 1
  • 1
lakyii
  • 1
  • 1
  • 1
  • Try writing your javascript code inside document load event as below. document.onreadystatechange = function () { if (document.readyState == "complete") { .. here.. } } – Nabil Dec 06 '14 at 08: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 03:20

4 Answers4

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>
Frankey
  • 3,679
  • 28
  • 25
  • i changed code line to `$valueMap[$row['clm_num']] = $row['clm_amnt'];` but nothing happened :( it's retrieve nothing. – lakyii Dec 06 '14 at 08:05
  • Lets do it step by step, directly after the while loop, on the next line: echo "
    ".print_r($valueMap, true)."
    "; and excute to see  it the php array is generated.
    – Frankey Dec 06 '14 at 08: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 06 '14 at 08:13
  • just updated the answer, though it would help if you could show what the desired html output would be. – Frankey Dec 06 '14 at 08: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.

Yash
  • 1,446
  • 1
  • 13
  • 26
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
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
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>");
    }
}
Sannek8552
  • 123
  • 1
  • 3