简体   繁体   English

array_shift()无法识别数组变量

[英]array_shift() not recognizing array variable

I created a Web App for a Delivery service. 我为交付服务创建了一个Web应用程序。 It incorporates google maps as well as queries a database for open deliveries. 它结合了谷歌地图,以及查询数据库的公开交付。 The problem is that when I hooked up a button to shift the delivery array by array_shift() through an ajax call, is wont recognize the array. 问题是,当我挂上一个按钮以通过ajax调用通过array_shift()移动传递数组时,将无法识别该数组。

<?php
session_start();
require 'ConnectTest.php';

// IF YOU'RE NOT LOGGED IN, KICK BACK TO LOGIN SCREEN
if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){
  header("Location:signin.php");
}

$servername = "localhost:3306";
$user = "sonic_client";
$pass = "client";
$dbName = "sonicStrains";

$drop = "DROP TABLE ".$_SESSION['username']."_deliveries";
mysqli_query($server, $drop);

//CREATE INDIVIDUAL DRIVER TABLE TO HOLD DELIVERIES
$createQuery = "CREATE TABLE ".$_SESSION['username']."_deliveries (
                transaction_id VARCHAR(13),
                timePaid INT(11),
                username VARCHAR(30),
                user_location VARCHAR(255),
                user_lat float(10,6),
                user_long float(10,6),
                item_name VARCHAR(20),
                item_quantity float,
                driver_username VARCHAR(60),
                driver_lat float(10,6),
                driver_long float(10,6),
                on_delivery tinyint(1))"
;

$created = mysqli_query($server, $createQuery);

if ($created){

  //query the deliveries for open deliveries up untill 5 deliveries
  $queryString = "SELECT * FROM deliveries LIMIT 5";

  $query = mysqli_query($server, $queryString);

  if($query){

    // CREATE ARRAY TO HOLD QUERY ROWS
    $rows = array();

    while($queryresult = mysqli_fetch_assoc($query)){
      $rows = $queryresult;
    }


    //INSERT & UPDATE THE TABLES WITH DELIVERY QUERIES
    foreach($rows as $row){

      // INSERT INTO INDIVIDUAL DRIVER TABLE
      $insertQuery = "INSERT INTO ".$_SESSION['username']."_deliveries (transaction_id, user_location, user_lat, user_long) VALUES('$row[transaction_id]', '$row[user_location]', '$row[user_lat]', '$row[user_long]')";

      $insertExec = mysqli_query($server, $insertQuery);

      // UPDATE MASTER LIST OF DELIVERIES SO THAT OTHER DRIVERS DONT QUERY SAME ORDER
      $updateQuery = "UPDATE deliveries SET on_delivery=true, driver_username='$_SESSION[driver_id]' WHERE transaction_id='$row[transaction_id]'";
      $updateExec = mysqli_query($server, $updateQuery);
    }

  }else{echo mysqli_error($server);}


}else{echo mysqli_error($server);}


if(isset($_GET['pop'])){
  array_shift($rows);
}

echo<<<_

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <style> /* the stylesheet below */ </style>
</head> 

<body>

  <div class="Banner">
      <div class="TitleText">Sonic Strains &copy;</div>
  </div>


  <div class="login">Logout</div>
  <div class="gallery" id="container">
      <div class="map" id="mapInsert"></div>
      <div class="navButton">Start Nav</div><div class="orderButton">Order Details</div>
      <div class="abortButton">Abort</div><div class="confirmButton" onclick="shiftOrder()">Confirm</div><div class="disclaimer"></div>
  </div>


  <script>
      function initMap() {

        navigator.geolocation.getCurrentPosition(function(position) {

            var initialLocation = {lat:$rows[user_lat], lng:$rows[user_long]};
            var Map = new google.maps.Map(document.getElementById('mapInsert'));
            Map.setCenter(initialLocation);
            Map.setZoom(13);
            // MAKE ANOTHER MARKER FOR THE CLIENT LOCATION
                    var userLocation = {lat:$rows[user_lat], lng:$rows[user_long]};
                    var marker = new google.maps.Marker({
                        position:userLocation, 
                        map:Map,
                        draggable:false, 
                        clickable:false
                        });
                    marker.setMap(Map);

        }, function(positionError) {

        //---------- User denied geolocation prompt - default to Chicago

        Map.setCenter(new google.maps.LatLng(39.8097343, -98.5556199));
        Map.setZoom(5);
      },{enableHighAccuracy:true, timeout: 3000, maximumAge:1000});

      } 

      function shiftOrder(){

          var http = new XMLHttpRequest();
          http.onreadystatechange = function(){
              console.log(this.responseText);

          }
          http.open("GET", "driverIndex.php?pop='pop'", true);
          http.send();

      }

  </script>
  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap" 
      async defer></script>

</body>
</html>

_;

?>

I have called print_r($rows) and it returns values as it should. 我已经调用了print_r($ rows),它会返回值。 I even use it to reference lat & long in my Google Maps, so why can't array_shift($rows) work? 我什至在我的Google地图中用它来引用经纬度,为什么array_shift($ rows)不能工作?

If needed to reproduce my example above, here is the used CSS stylesheet: 如果需要重现上面的示例,请使用以下CSS样式表:

.TitleText{
  font-size:200%;
}

.Banner{                                      /*This is test code to hold the top ad*/
  width:100%;
  text-align:center;
  border-style:solid;
  border-color:grey;
  color:green;
  font-size:230%;
  font-weight:heavy;
  letter-spacing:1px;
}

.TopNav{

  margin-top:20px;
}

a:link{
  text-decoration:none;
}

.login{
  margin-top:15px;
  margin-right:15px;
  float:right;
  border-style:solid;
  border-color:grey;
  border-radius:10px;
  background-color:blue;
  color:white;
  font-size:30px;

}

.gallery{                                         /*This will hold the gallery Items*/
  text-align:center;
  display: inline-block;
  width: 100%;
  height: auto;
  margin-top: 16px;
  border-style: solid;
  border-color: green;
}

.map{
  width:100%;
  height:400px;
  border-style:solid;
}

.navButton{
  background-color:green;
  color:white;
  font-size:30px;
  margin-top: 16px;
  text-align:center;
  width:100%;
  display: inline-block;
  border-radius:10px;
  border-style:solid;
  border-color:red;
}

.orderButton{
  background-color:green;
  color:white;
  font-size:30px;
  margin-top: 16px;
  text-align:center;
  float:right;
  display: inline-block;
  border-style:solid;
  border-right:red;
  border-color:purple;
  border-radius:10px;
  width:100%;
  height:auto;

}

.abortButton{
  background-color:green;
  color:white;
  font-size:30px;
  margin-top: 16px;
  text-align:center;
  border-style:solid;
  border-color:yellow;
  border-radius:10px;
  width:100%;
  height:auto;
  display: inline-block;
}

.confirmButton{
  background-color:green;
  color:white;
  font-size:30px;
  margin-top: 16px;
  text-align:center;
  border-style:solid;
  border-color:pink;
  border-radius:10px;
  width:100%;
  height:auto;
  display: inline-block;
}

h1{
  font-weight:bold;
}


h3{
  font-weight:bold;
  color:green;
}

.disclaimer{
  width:100%;
  height:30px;

  border-style:solid;
  border-radius:10px;
  border-color:grey;
  text-align:center;
}

Even if it could not be conclusively clarified in the comment discussion, you propably expected the code 即使无法在注释讨论中最终阐明它,您也可能期望代码

while($queryresult = mysqli_fetch_assoc($query)){
  $rows = $queryresult;
}

to add new rows as entries to the array. 将新行添加为数组的条目。 In fact it overwrites the variable with an associative array containing the latest row, ie columns. 实际上,它使用包含最新行(即列)的关联数组覆盖变量。

To get an array of rows, use 要获取行数组,请使用

while($queryresult = mysqli_fetch_assoc($query)){
  $rows[] = $queryresult;
}

This will append array items to the existing array. 这会将数组项追加到现有数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM