2011-08-29 47 views
1

自上午9點以來,我一直在我的桌上跳動我的頭,並且我所做的所有搜索,我仍然沒有得到我需要的結果。我擁有的是一個使用動態創建的數組。陣列與數組

array_push($this->product_info, 
     $subline1, 
     $subline2, 
     $subcarving, 
     $subpicture, 
     $extra_cost, 
     $subtotal); 

    if(!empty($this->cart)){ 
    array_push($this->cart, $this->product_info); 
    } 
    else 
    { 
    $this->cart = $this->product_info; 
    } 

設置後,用戶可以添加其他產品或繼續結帳。如你所見,如果他們添加另一個產品,我使用array_push()追加新的數組。所以現在數組有更多的1個數組。我希望能夠抓住每個陣列中的兩個元素。所以我寫了這一點:

$count = count($_SESSION['cart']); 
for($i=0;$i<$count;$i++){ 
     foreach($_SESSION['cart'][$i] as $key => $value){ 
      echo "This is element $i with key $key: $value<br />";  
     } 
} 

這是什麼是輸出到瀏覽器:

This is element 0 with key 0: VP1021-G 
This is element 0 with key product_id: VP1021-G 
This is element 0 with key 1: Pendant 
This is element 0 with key product_type: Pendant 
This is element 0 with key 2: Vertical 
This is element 0 with key product_format: Vertical 
This is element 0 with key 3: Goldtone 
This is element 0 with key setting_color: Goldtone 
This is element 0 with key 4: 125 
This is element 0 with key price: 125 
This is element 0 with key 5: N/A 
This is element 0 with key 6: N/A 
This is element 0 with key 7: Black and White 
This is element 0 with key 8: 01_faces.jpg 
This is element 0 with key 9: 0 
This is element 0 with key 10: 125 
This is element 1 with key 0: Array 

能搶我我想要的東西,如果我用這個:

print_r($_SESSION['cart'][0][0]); 
echo "<br /><br />"; 
print_r($_SESSION['cart'][1][0][0]); 

,但我不會如果有多於2個,則能夠抓住陣列的其餘部分。 這裏有兩個功能:

#Process 
function ProcBuildCameo(){ 
    global $session, $form; 

    $retval = $session->BuildCameo($_POST['product_type'], $_POST['product_format'], $_POST['setting_color'], $_POST['carving_color'], $_POST['line1'], $_POST['line2'], $_FILES['picture']['name']); 

    /* Submit order successful */ 
    if($retval == 0){ 
     $_SESSION['product_info'] = $session->product_info; 
     if(empty($_SESSION['cart'])){ 
      $_SESSION['cart'] = $session->cart; 
     } 
     else{ 
      array_push($_SESSION['cart'],$session->cart); 
     } 

     //var_dump($_SESSION['cart']); 
     //die(); 
     $_SESSION['info'] = true; 
     $_SESSION['value_array'] = $_POST; 
     header("Location: view_cameo.php"); 
     die(); 
    } 
    /* Error found with form */ 
    else if($retval == 1){ 
     $_SESSION['value_array'] = $_POST; 
     $_SESSION['error_array'] = $form->GetErrorArray(); 
     header("Location: cameo.php"); 
     die(); 
    } 
} 
#Session 
function BuildCameo($subtype, $subformat, $subsetting, $subcarving, $subline1, $subline2, $subpicture){ 
    global $mysql, $form; 
    $extra_cost = 0; 

    /* type error checking */ 
    $field = "product_type"; 
    if(!$subtype || strlen($subtype = trim($subtype)) == 0){ 
     $form->SetError($field, "* Product not checked!"); 
    } 
    /* format error checking */ 
    $field = "product_format"; 
    if(!$subformat || strlen($subformat = trim($subformat)) == 0){ 
     $form->SetError($field, "* Format not checked!"); 
    } 
    /* setting color error checking */ 
    $field = "setting_color"; 
    if(!$subsetting || strlen($subsetting = trim($subsetting)) == 0){ 
     $form->SetError($field, "* Setting color not checked!"); 
    } 
    /* carving color error checking */ 
    $field = "carving_color"; 
    if(!$subcarving || strlen($subcarving = trim($subcarving)) == 0){ 
     $form->SetError($field, "* Carving color not checked!"); 
    } 
    /* checks if line1 is empty */ 
    if(!$subline1 || strlen($subline1 = trim($subline1)) == 0){ 
     $subline1 = "N/A"; 
    } 
    else{ 
     $extra_cost = 15; 
    } 
    /* checks if line2 is empty */ 
    if(!$subline2 || strlen($subline2 = trim($subline2)) == 0){ 
     $subline2 = "N/A"; 
    } 
    else{ 
     $extra_cost = $extra_cost + 15; 
    } 
    $field = "picture"; 
    $valid = array('jpg','jpeg'); 
    if(!$subpicture || strlen($subpicture = trim($subpicture)) == 0){ 
     $form->SetError($field, "* Select a picture to upload!"); 
    } 
    if(!in_array(end(explode('.',$subpicture)),$valid)){ 
     $form->SetError($field, "* Please upload jpg or jpeg files!"); 
    } 
    /* Errors exist, have user correct them */ 
    if($form->num_errors > 0){ 
     return 1; //Errors with form 
    } 
    else{ 
     if($mysql->GetProductInfo($subtype, $subformat, $subsetting)){ 
      $this->product_info = $mysql->GetProductInfo($subtype, $subformat, $subsetting); 
      $subtotal = $this->product_info['price'] + $extra_cost; 
      array_push($this->product_info, $subline1, $subline2, $subcarving, $subpicture, $extra_cost, $subtotal); 
      if(!empty($this->cart)){ 
       array_push($this->cart, $this->product_info); 
      } 
      else{ 
       $this->cart = $this->product_info; 
      } 

      if(is_uploaded_file($_FILES["picture"]["tmp_name"])){ 
       /** 
       * Give video file unique name 
       * and copy from temp folder 
       * to videos folder 
       */ 
       copy($_FILES["picture"]["tmp_name"],"pictures/" . $subpicture); 

      }   
      return 0; 
     } 
    } 

} 
+0

你沒有提供解決這個問題所需的所有代碼,'$ this-> cart'如何成爲'$ _SESSION'的一部分?有些東西不正確,你如何將它追加到數組 – Jakub

+0

它被設置在不同的頁面中; – Ray

+0

if(empty($ _ SESSION ['cart'])){ \t \t \t \t $ _SESSION ['cart'] = $ session-> cart; \t \t \t} \t \t \t否則{ \t \t \t \t array_push($ _ SESSION [ '車'],$會話級>車); \t \t \t} – Ray

回答

1

我相信你在這裏混淆了你的嵌套數組。您需要一個包含一個或多個product_info數組的cart數組。剛落調用array_push(),並使用此語法:

if(!empty($this->cart)){ 
    // Add new product to array $this->cart 
    $this->cart[] = $this->product_info); 
} else { 
    // Create Array with one product 
    $this->cart = array($this->product_info); 
} 
+0

謝謝你的建議讓我非常感激 – Ray

0

你不需要任何驗證一樣,如果(空) - 這是PHP

$這個 - >購物車[] = $這個 - >產品的詳細信息;

所以但如果你使用array_push,你必須做這個驗證,因爲array_push通過引用接受變量並且不能創建或重新分配它。