Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am trying to fetch latitude, longitude and venue by typing

sites[0] // gets latitude
sites[1] // gets longitude
sites[2] // gets venue

this is my code, dont mind the sql querrys, i know they are bad but it's functional for now. i just need help with this array:

  var sites = [<?php

include_once'config/connect.php';

$search= $_POST['search'];
$search = stripslashes($search);

$search = mysql_real_escape_string($search);

$sql= "select * from venue where vID in (select vID from sv where sID in (select sID from sports where sN = '$search'));";

$result=mysql_query($sql)or die(mysql_error()); 

while($data=mysql_fetch_array($result))
{
    echo ",[".$data['latitude']."]";
    echo ",[".$data['longitude']."]";
    echo ",[".$data['venue']."]";

}

?> ];
share|improve this question

closed as unclear what you're asking by andrewsi, Sverri M. Olsen, Günter Zöchbauer, Marc Baumbach, zzlalani Jan 10 at 6:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
So what do you want and what error are you getting? –  user2936213 Jan 9 at 12:26
    
i want to get for example latitude if i type sites[0] but it doesn't work. let me check that –  Oscar Andersson Jan 9 at 12:31
    
what is the error? –  user2936213 Jan 9 at 12:32
1  
1. Don't use the mysql_ methods; they're deprecated. 2. Don't try to format your JSON yourself, use json_encode. –  kba Jan 9 at 12:37

3 Answers 3

up vote 1 down vote accepted

Create a normal PHP array containing the values you want, and call json_encode to convert it to JSON. Echo that, and parse it on the client.

<?php
    $data = mysql_fetch_array($result);
    $array = array($data['latitude'], $data['longitude'], $data['venue']);
?>

<script>
    var sites = <?php echo json_encode($array); ?>;
</script>

Edit: as @kba points out, the mysql_ methods are deprecated.

share|improve this answer

if u hav a php array $sites, u need to add it to js array sites

<?php
$sit="";
foreach($sites as $ar)
{
    if($sit=="")
    {
        $sit=$ar;
    }
    else
    {
        $sit=$sit.",".$ar;
    }
}
?>

in js:

var sites = [<?php echo $sit;] ?>;
share|improve this answer
    
What if $sit contains a ','? Or a ']'? –  johnnycardy Jan 9 at 12:40
    
its variable to store the values seperated with comma, after all the iteration sit contains all the values in array seperated by comma. –  ReNiSh A R Jan 9 at 12:41
    
Oh yeah. But you know what I mean - the PHP string isn't being escaped and could generate invalid JS. –  johnnycardy Jan 9 at 12:49
    
no its working fine for meeee,,, me using this for generating chart using high charts,,,, no error for me... –  ReNiSh A R Jan 9 at 12:55

If you sql is working fine, then you just need to remove the extras "[" "]", so it will be like this:

echo $data['latitude'].", ";
echo $data['longitude'].", ";
echo $data['venue'];

Then your javascript array will be working ok

share|improve this answer
    
Why downvote?? It fixes the js array construction, that he was doing wrong. –  LcSalazar Jan 9 at 12:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.