1

I'm working on passing 2 fields of SQL through php to javascript. I have read many tutorials on how to create a multidimensional javascript array. Where I get confused is how to code from php to javascript. I have seen a couple of tutorials on how to get the php data to javascript, but none on how to do this with 2 dimensions.

My first hangup is that if I'm creating a multidimensional array I need to count the number of records in my sql data before I declare the java array right?

update:

I got the data to JSON format as suggested below. Is there a way for me to get all of the contents printed to the web page so that I can see them and then narrow down what is displayed?

update III:

code:

mysql_connect("localhost", "bikemap", "pedalhard") or die(mysql_error()); 
 mysql_select_db("test") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM gpsdata");
 $new_row = array();
$new_column = array();
 while($info = mysql_fetch_array($data)){

   foreach( $info as $row_num => $row)
{
      $thisItem = $row;
      $new_row[] = $thisItem;
   }
   array_push($new_column = $new_row);

 }
 $json = json_encode($new_column);
echo $json;    
?>

Working code:

 $data = mysql_query("SELECT * FROM gpsdata");     
$aData = array();
while($row = mysql_fetch_assoc($data))
   $aData[$row['idgpsdata']] = array($row['userID'],$row['date'],$row['lat'], $row['longi'], $row['alt']);


 $json = json_encode($aData);
echo $json;
4
  • 3
    You can shorten the current code: var scriptAr = <?php echo json_encode($lines); ?>; Commented Nov 13, 2011 at 19:32
  • @LorenZimmer Yea, you're using mysql_fetch_array incorrectly. This only gets one row at a time. Programming languages don't know about tables, so you grab a row at a time. You need to actually build the 2D array. Commented Nov 14, 2011 at 11:08
  • update... the code above returns all of the values but not the keys. The weird thing for me is that it creates duplicate values when plugged into @Laith Shadeed 's java routine. Commented Nov 15, 2011 at 2:19
  • I think I've got it see code above! Commented Nov 15, 2011 at 2:32

2 Answers 2

0

Fill a PHP array first, it's easier than building the string for a javascript array. Then - as ThiefMaster said as comment - use JSON to make the javascript array.

2
  • If I add the varable $json between <?php and ?> can it be used outside as well? Commented Nov 13, 2011 at 20:20
  • Yes, it can be used in any PHP section of the script. Commented Nov 13, 2011 at 21:02
0

In PHP, you can use JSON PECL extension

<?php
$arr = array( 1=>array(3,4),
              2=>array(4,5));
$json = json_encode($arr);
echo $json;
?>

Output

{"1":[3,4],"2":[4,5]}

In Javascript

var obj = JSON.parse('{"1":[3,4],"2":[4,5]}');
for(var i in obj){
    for(var j in obj[i]) {
        console.log(obj[i][j]);
    }
}

JSON.Parse is for Gecko (Firefox alike), for cross-browser javascript JSON parse check jQuery.parseJSON or https://stackoverflow.com/search?q=json+parse+javascript

Sample implementation in PHP/jQuery, would be something like this:

json.php

$arr = array( 1=>array('Name', 'Age'),
              2=>array('Fares','18'));
$json = json_encode($arr);
echo $json;

json.html

<html><head></head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script>
$.getJSON('json.php', function(data) {
  var items = [];

  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});
</script>
</body>
</html>
1
  • thanks for your suggestions. In my original question you will see that I have substituted some code to get sql results converted to json but I have not been completely successful. Any thoughts what I have entered incorrectly. I have a feeling that I am not passing the information into an array correctly. Commented Nov 14, 2011 at 11:01

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.