Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动递增数组索引(PHP)_Php_Arrays_Indexing_Add - Fatal编程技术网

自动递增数组索引(PHP)

自动递增数组索引(PHP),php,arrays,indexing,add,Php,Arrays,Indexing,Add,只是搜索了一下,找不到一个答案合适的问题。我试图在数组的末尾添加一个product_id,有些时候每次添加某个内容时,它也会添加到数组中。目前,每次我添加产品id时,它都会替换以前存在的内容。我得到的只是数组[0]中的一个product_id。下面是我将添加到数组中的代码部分。然后我使用 print_r($order); 显示数组中的内容 $order = array(); switch ($action) { // Add case "add": $order[] =

只是搜索了一下,找不到一个答案合适的问题。我试图在数组的末尾添加一个product_id,有些时候每次添加某个内容时,它也会添加到数组中。目前,每次我添加产品id时,它都会替换以前存在的内容。我得到的只是数组[0]中的一个product_id。下面是我将添加到数组中的代码部分。然后我使用

print_r($order);
显示数组中的内容

$order = array();

switch ($action)
    {

    // Add

case "add":

$order[] = $product_id;
        break;
        }
以下是页面中的全部代码,减去一些MySQL内容

include 'connection.php';

if (isset($_GET['action']))
    {
    $action = $_GET['action']; //Get's action from the URL
    }

if (isset($_GET['product_id']))
    {
    $product_id = $_GET['product_id']; //the action from the URL
    }

// if there is an product_id and that product_id doesn't exist display an error message



if ($product_id && !productExists($product_id))
    {
    die("Error. Product Doesn't Exist");
    }

switch ($action)
    { //decide what to do

    // Add

case "add":

$order[] = $product_id;

    if (isset($_SESSION['user']))
        {
        $productadd = sprintf("INSERT INTO tbl_basket (customer_id, product_id, basket_status) VALUES ('" . $_SESSION['user'] . "','$product_id','basket')");
        mysql_query($productadd);
        break;
        }
      else
        {

        $productadd = sprintf("INSERT INTO tbl_basket (customer_id, product_id, basket_status, user_type) VALUES ('" . $_SESSION['guestuser'] . "','$product_id','basket', 'guest')");
        mysql_query($productadd);
        break;
        }

    // Remove

case "remove":
    if (isset($_SESSION['user']))
        {
        $productremove = sprintf("DELETE FROM tbl_basket WHERE customer_id = '" . $_SESSION['user'] . "' AND product_id = '$product_id'");
        mysql_query($productremove);
        break;
        }
      else
        {
        $productremove = sprintf("DELETE FROM tbl_basket WHERE customer_id = '" . $_SESSION['guestuser'] . "' AND product_id = '$product_id'");
        mysql_query($productremove);
        break;
        }

    // Empty

case "empty":
    if (isset($_SESSION['user']))
        {
        $empty = sprintf("DELETE FROM tbl_basket WHERE customer_id = '" . $_SESSION['user'] . "' AND order_status = 'basket'");
        mysql_query($empty);
        break;
        }
      else
        {
        $empty = sprintf("DELETE FROM tbl_basket WHERE customer_id = '" . $_SESSION['user'] . "' AND order_status = 'basket'");
        mysql_query($empty);
        break;
        }
    }

if (isset($_SESSION['user']))
    {
    $result = mysql_query("SELECT * FROM tbl_basket a INNER JOIN tbl_products b ON a.product_id = b.product_id WHERE a.customer_id = '" . $_SESSION['user'] . "'");
    }
  else
    {
    $result = mysql_query("SELECT * FROM tbl_basket a INNER JOIN tbl_products b ON a.product_id = b.product_id WHERE a.customer_id = '" . $_SESSION['guestuser'] . "'");
    }

执行
$order=array()操作时,可以清除
$order
中以前的值

只需删除该行并
$order[]=$product\u id将向数组中添加另一项。

似乎是可变范围的问题。请提供更多代码。如果有一个循环-显示它。我已经用更多的代码更新了这个问题。我认为这与每次重新初始化数组有关。这仍然会替换所有内容。因此,我目前拥有的数组的唯一代码是$order[]=$product\u id@user3521635在添加到数组之前检查数组,以查看其中包含的内容。
$order = array();

switch ($action)
    {

    // Add

case "add":

$order[] = $product_id;
        break;
        }