viral viral - 1 year ago 69
Javascript Question

How to Use javascript inside a php while loop for displaying map markers

How do i show multiple database entries in loop ? So that it looks like this:

enter image description here

But with multiple markers.

I'm not able to loop javascript in php to show multiple markers. Is it possible to do so?

dataProvider: {
map: "worldLow",
images: [
<?php include ('query.php');
while($row= mysql_fetch_array($fetch)){
$cty = $row['city'];
$lat = $row['lati'];
$lon = $row['longi'];
zoomLevel: 5,
scale: 0.5,
title: "<?php echo $cty;?>",
latitude: <?php echo $lon;?>,
longitude: <?php echo $lat;?>

Answer Source

I advise you to use json_encode function so as to avoid problems with extra quotes and other symbols:

dataProvider: {
    map: "worldLow",
    <?php include ('query.php');
    // store all markers here
    $markers = array();
    while ($row= mysql_fetch_array($fetch)) {
        // add new marker data
        $markers[] = array(
            'zoomLevel' => 5,
            'scale' => 0.5,
            'title' => $row['city'],
            'latitude' => $row['lati'],
            'longitude' => $row['longi'],
    images: <?=json_encode($markers)?>

For further debugging - use developers console and/or see rendered javascript.