虽然循环只显示一行?

时间:2016-11-22 06:22:31

标签: php jquery mysql dictionary while-loop

<?php
    include 'db.php';   
    $sql_locations = "SELECT * FROM location";          
    $result = mysqli_query($conn,$sql_locations);
    $count = mysqli_num_rows($result);      
    $markers = array();

    while($row = mysqli_fetch_array($result,MYSQL_ASSOC))           
    {  

        $markers = array(       
        array(
        'abc',
        $row['latitude'],
        $row['longitude']
        ));     

    } ?>

这是我从数据库中获取位置的代码。我有以下脚本来显示地图。

<script>
function initialize() {
    var map;
    var bounds = new google.maps.LatLngBounds();
    var mapOptions = {
        mapTypeId: "roadmap",
        center: new google.maps.LatLng(20.5937, 78.9629), // somewhere in the uk BEWARE center is required
        zoom: 1,
    };
    // Display a map on the page
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    map.setTilt(45);

    // Multiple Markers
    var markers = <?php echo json_encode( $markers );?>;



    // Display multiple markers on a map
    var infoWindow = new google.maps.InfoWindow();
    var marker, i;

    // Loop through our array of markers & place each one on the map
    for (i = 0; i < markers.length; i++) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0]
        });

        // Allow each marker to have an info window
        google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
                infoWindow.setContent(infoWindowContent[i][0]);
                infoWindow.open(map, marker);
            }
        })(marker, i));

        // Automatically center the map fitting all markers on the screen
        map.fitBounds(bounds);
    }

    //Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) {
        this.setZoom(5);
        google.maps.event.removeListener(boundsListener);
    });

}
google.maps.event.addDomListener(window, 'load', initialize);

我的地图工作正常。我只是面临问题来显示我的数据库中的所有记录。我的while循环只从表中首先显示,请帮助我。 我想用数据库显示数据库中的所有位置。在print_r o $ markers中它只显示第一行表格。在$ rows上的var_dump上显示所有数据。但我只获得了第一行的一个标记。我的表中总共有2行。提前谢谢。

3 个答案:

答案 0 :(得分:1)

您正在更新整个数组,而不是向其添加元素。

变化

   $markers = array(       
    array(
    'abc',
    $row['latitude'],
    $row['longitude']
    ));     

 $markers[] = array(       
    array(
    'abc',
    $row['latitude'],
    $row['longitude']
    ));     

答案 1 :(得分:1)

您正在$markers循环中覆盖while变量。所以你需要这样做: -

$markers[] = array('abc',$row['latitude'],$row['longitude']); 

或(如果需要外部数组)

$markers[] = array(array('abc',$row['latitude'],$row['longitude']));

答案 2 :(得分:1)

您在每次迭代时都会覆盖变量$markers

您只需添加一个新的数组元素:

// Define the variable
$markers = array();

while($row = mysqli_fetch_array($result,MYSQL_ASSOC))           
{  
    // Push a new element to the array instead of overwrite it.
    $markers[] = array(
        'abc',
        $row['latitude'],
        $row['longitude']
    );     

}
相关问题