【问题标题】:Foreach is not getting all dataForeach 没有获取所有数据
【发布时间】:2017-05-17 04:44:21
【问题描述】:

我正在使用 foreach 语句获取所有数据并在表中打印,还使用 ​​ajax 获取该区域的所有区域和驱动程序,根据他的来源,每一行都有不同的驱动程序,但问题是当我加载页面时它向我展示了所有用户数据仅在表格中,但区域和驱动程序只显示一行其他行没有显示我的驱动程序下面是我的代码请告诉我我错在哪里

                    <tbody>
                <?php
                $counter = 0;
                foreach($result as $user){?>

                <form action="<?php echo SURL?>dashboard/set_driver" method="POST">
                <tr>
                <td><?php get_username($user['client_id']); ?></td>
                <td><?php echo $user['pickfrom']; ?></td>
                <td><?php echo $user['dropat']; ?></td>
                <td><?php echo $user['pickdate'].' '.$user['picktime']; ?></td>
                <td><?php echo $user['distance']; ?></td>
                <input type="hidden" id="txtSource" value="<?php echo trim($user['pickfrom']); ?>">
                <td>
                <div class="form-group">
                <select class="form-control" name="driver_id" id="drivers_<?php echo $counter;?>"><option>Select Driver</option>
                </select>
                </div></td><input type="hidden" id="latitude" name="latitude" value="<?php echo $user['latitude'];?>">
                <input type="hidden" id="longitude" name="longtitude" value="<?php echo $user['longtitude'];?>">
                <input type="hidden" name="request_id" value="<?php echo $user['id'];?>">

                <td>
                <button class="btn btn-default" name="submit">Set Driver</button>
                </td>
                </tr>
                </form>
                <?php echo $div_id = '#drivers_'.$counter;?>


                <script>
                var map;
                var coord1 = new google.maps.LatLng(<?php echo $user['latitude'];?>, <?php echo $user['longtitude'];?>);

                function initialize() {
                var map = new google.maps.Map(
                document.getElementById("map_canvas"), {
                center: new google.maps.LatLng(33.714760, 73.083160),
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP
                });
                <?php 
                $counter=0;
                if(!empty($zone)){
                foreach($zone as $findzone)
                {
                $exploded_data=explode('),',$findzone['zone_latlog']);
                $count=count($exploded_data);
                $zone_id = $findzone['id'];
                ?>  
                var bermudaTriangle = new google.maps.Polygon({
                map: map,
                paths: [
                <?php 
                for($i=0;$i<$count;$i++){
                echo "new google.maps.LatLng".$exploded_data[$i]."),";
                }
                ?>
                ]
                });
                var bounds = new google.maps.LatLngBounds();
                for (var i=0; i<bermudaTriangle.getPath().getLength(); i++) {
                bounds.extend(bermudaTriangle.getPath().getAt(i));
                }
                bounds.extend(coord1);
                var marker1 = new google.maps.Marker({
                map: map,
                position: coord1
                });
                map.setCenter(bounds.getCenter());
                map.setZoom(11);
                var result = checkInPolygon(marker1, bermudaTriangle);
                if(result)
                {

                var zone_id = <?php echo $zone_id;?>;
                alert(zone_id);
                var lati = $('#latitude').val();
                var longi = $('#longitude').val();
                $.ajax({        
                url: '<?php echo SURL;?>dashboard/check_driver/<?php echo $zone_id;?>/'+longi+'/'+lati,
                data: 'zone_id='+ zone_id+'&lati='+lati+'&longi='+longi,
                type: "POST",
                dataType: 'json',
                cache: false,
                success: function(response) {
                // alert(response);
                for(var i = 0; i < response.length; i++) {
                alert(response[i].driver_name);
                $('<?php echo $div_id?>').append($('<option>', {
                value: response[i].driver_id,
                text: response[i].driver_name
                }));
                } 
                }
                }); 

                }
                <?php 
                }}?> 
                }

                function checkInPolygon(marker, polygon) {
                var infowindow = new google.maps.InfoWindow();
                var html = "";
                if (google.maps.geometry.poly.containsLocation(marker.getPosition(), polygon)) {
                return true;
                } else {
                return false;
                }
                infowindow.setContent(html);
                infowindow.open(map, marker);

                }


                </script>

                <?php
                $counter++;
                }
                ?>
                </tbody>

如果我在 foreach 之外编写脚本,我会显示驱动程序的单个数据,如果我在内部编写,结果是相同的。

【问题讨论】:

  • 你能打印 $results 变量吗
  • 是的,它显示了所有结果
  • 我无法在此处写入所有结果,但一个数组是 Array ( [0] => Array ( [id] => 41 [driver_id] => 0 [client_id] => 54 [pickdate] => 2017-05-16 [pickfrom] => Bahria Town, Rawalpindi, Punjab, Pakistan [dropat] => PWD Housing Society, Rawalpindi, Punjab, Pakistan [距离] => 17.0 km [picktime] => 15:53: 15 [droptime] => [status] => pending [ip] => ::1 [latitude] => 33.495896 [longtitude] => 73.10563009999998) [1] =>

标签: javascript php mysql foreach


【解决方案1】:

更改你的 foreach 循环使其动态化,首先计算结果,然后将其传递给数组块,然后是一个 for 循环,用于获取你得到的数字,并在其中一个 foreach

【讨论】:

  • 仍然只得到一个结果?兄弟你能把输出粘贴到问题部分吗?让它变大。如果我没有看到结果,我无法理解
  • imgur.com/a/YmeUS 这第二张图片我在第二行得到结果,但第一行是空的...
  • 你的数据格式搞砸了,数据来自不同的数组而不是一个数组,因此问题
  • 我该如何解决这个问题?
  • 首先说明这显示了什么 echo(count($result ));
猜你喜欢
  • 2019-07-27
  • 1970-01-01
  • 2020-12-28
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 2016-05-21
相关资源
最近更新 更多