0

Ive been trying this for hours now but it wont quite get there.

I have a database which amongst other things contains geocodes, lat and lon. I have accessed these using the following PHP

<?php
 mysql_connect("localhost", "tompublic", "public") or die(mysql_error()); 
 mysql_select_db("first_section") or die(mysql_error()); 

$data = mysql_query("SELECT geo_lat, geo_lon FROM first_page_data")
or die(mysql_error());
while ($row = mysql_fetch_assoc($data)){
$lat[] = $row['geo_lat'];
$lon[] = $row['geo_lon'];
}
?>

These values in $lat and $lon then need to be put into an array in a javascript function like so:

var latit = [];
var longi = [];
latit = '<?php echo $lat[]; ?>';
longi = '<?php echo $lon[]; ?>';

But it wont work! Any ideas?

1
  • Wouldn't latit just equal Array? Commented Jan 10, 2014 at 22:51

6 Answers 6

1

Try:

var latit = <?php echo json_encode($lat); ?>;
var longi = <?php echo json_encode($lon); ?>;

Edit: Also, the mysql_ functions are deprecated.

Sign up to request clarification or add additional context in comments.

Comments

1

You could try this:

var latit = [<?php echo implode(",",$lat) ?>];
var longi = [<?php echo implode(",",$lon) ?>];

Comments

1

First thing is first try to switch to MySQLi due to the fact that Mysql is depreciated.

But try

var latit = <?php echo json_encode($lat); ?>;
var longi = <?php echo json_encode($lon); ?>;

Comments

0

You can either do as @Robbert stated

OR Using php json_encode convert it to JSON string and you need to JSON.parse() it to convert it to javascript object

 var latit = JSON.parse("<?php echo json_encode($lat); ?>");
 var longi = JSON.parse("<?php echo json_encode($lon); ?>");

Comments

0

JavaScript arrays are created using a comma separated list surrounded by brackets

var latit = [];

To use your PHP values

var latit = [<?PHP echo implode(",",$lat); ?>];

This assumes your values are numbers. If not, you'll need to include quotes.

var latit = ['<?PHP echo implode("','",$lat); ?>'];

Finally, json_encode is a good option as many of the other answers indicate.

2 Comments

what does 'implode' do?
Implode is the same as join, it takes an array and joins the items together with the glue, in this case a comma: us3.php.net/function.implode
0

Try this:

<?php
 mysql_connect("localhost", "tompublic", "public") or die(mysql_error()); 
 mysql_select_db("first_section") or die(mysql_error()); 

$data = mysql_query("SELECT geo_lat, geo_lon FROM first_page_data")
 or die(mysql_error());
while ($row = mysql_fetch_assoc($data)){
$lat[] = $row['geo_lat'];
$lon[] = $row['geo_lon'];
}
 echo '
 <script type="text/javascript">    
  var latit  = '.json_encode($lat).';
  var longi  = '.json_encode($lon).';
 </script>
';
?>

Comments

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.