Reputation: 1
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); ?>;
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!
Upvotes: 0
Views: 2761
Reputation: 3757
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>
Upvotes: 2
Reputation: 1436
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.
Upvotes: 1
Reputation: 133
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>");
}
}
Upvotes: 0
Reputation: 4148
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
Upvotes: 0