array_shift()无法识别数组变量

时间:2019-01-04 21:21:54

标签: javascript php ajax html5

我为交付服务创建了一个Web应用程序。它结合了谷歌地图,以及查询数据库的公开交付。问题是,当我挂上一个按钮以通过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>

_;

?>

我调用了print_r($ rows),它返回了应该的值。我什至在Google地图中甚至用它来引用经纬度,为什么array_shift($ rows)不能正常工作?

如果需要重现上面的示例,请使用以下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;
}

1 个答案:

答案 0 :(得分:0)

即使不能在评论讨论中最终确定它,您也可能期望该代码

<template>
<div>
  <b-btn v-b-toggle.logs>
    <span class="when-opened">Close</span>
    <span class="when-closed">Open</span>
    Trace
  </b-btn>
  <b-collapse id="logs">
    <b-embed :src="src" v-if="this.$parent.$data.show"></b-embed>
    <div>Data: {{this.$parent.$data.show}}</div>
  </b-collapse>
</div>
</template>


<script lang="ts">
import Vue from "vue";
import { Prop, Component, Inject } from "vue-property-decorator";

@Component
export default class HelloWorld extends Vue {
  src = "http://localhost:7681/";
}
</script>

将新行添加为数组的条目。实际上,它使用包含最新行(即列)的关联数组覆盖变量。

要获取行数组,请使用

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

这会将附加个数组项到现有数组。