2011-08-29 46 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通过引用接受变量并且不能创建或重新分配它。