2014-04-11 104 views
0

只是進行了搜索,無法找到適當答案的問題。我試圖將一個product_id添加到數組的末尾,有些每次添加內容時,都會將其添加到數組中。目前,每次添加product_id時,它都會替換以前的任何內容。我得到的只是一個在Array [0]上的product_id .Below是我的代碼中添加到數組的一部分。然後我使用自動遞增數組索引(PHP)

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'] . "'"); 
    } 
+0

似乎是一個變量範圍的問題。 http://www.php.net/manual/en/language.variables.scope.php –

+0

請提供更多代碼。如果有一個周圍的環路 - 顯示它。 – tchow002

+0

我用更多的代碼更新了這個問題。 – user3521635

回答

0

你,當你做$order = array();
只要刪除該行並$order[] = $product_id;將其他項目添加到陣列清除$order之前的值。

+0

這仍然取代了一切。所以我現在使用的數組唯一的代碼是$ order [] = $ product_id; – user3521635

+0

@ user3521635在添加數組之前先檢查數組,以查看數組中的內容。 – Musa