2013-06-06 3 views
0

나는 단 하나의 계산서를 계산대에 게시했지만 반복되는 여러 필드를 게시하려면 양식이 필요합니다.다차원 배열 PHP 오류

내 배열은 지금

Array 
(
    [0] => Array 
     (
      [id] => 65983c1c16d925e9a8cc4c4b3bdda1f5 
      [item] => Array 
       (
        [1] => Brent New Product 
        [2] => Crazy Product 
       ) 

      [unitprice] => Array 
       (
        [1] => 100 
        [2] => 125 
       ) 

      [quantity] => Array 
       (
        [1] => 1 
        [2] => 1 
       ) 

     ) 

) 

을 보여주고있다 그러나 그것은 나의 양식은 모든 값을 수집이 PHP 스크립트로 이동이 page에서 게시하는 경우

Array 
(
    [0] => Array 
     (
      [id] => 65983c1c16d925e9a8cc4c4b3bdda1f5 
       (
        [product] => Brent New Product 
        [unitprice] => 100 
        [quantity] => 1 
       ) 
       [id] => 4326526662262 
       (
        [product] => Brent New Product2 
        [unitprice] => 140 
        [quantity] => 5 
       ) 



     ) 

) 

처럼 표시 할 필요가있다. 그것은 신갈 배열 전에 일,

session_start(); 
$price = $_POST['price']; 
$id = $_POST['id']; 
$item = $_POST['item']; 
$quantity = $_POST['quantity']; 

echo '<h1>Add to cart</h1>'; 

echo "<p>Thank you for wanting a <strong>$item</strong>!</p>"; 

//We define an associative array with the details of our new item 
$cart_row = array(
    'id' => md5(rand()), 
    'item' => $item, 
    'unitprice' => $price, 
    'quantity' => $quantity 
); 

그런 다음 카트 페이지에 나는 바구니에 항목을 표시하고자하지만 난 아무것도 채우기 상자를 얻을.

session_start(); 

foreach($_SESSION['cart'] as $item) { ?> 
<tr> 
    <td><?php echo $item['item']; ?></td> 
    <td><?php echo $item['unitprice'];?></td> 
    <td><?php echo $item['quantity']; ?></td> 
    <td></td> 
    <td><form id="id" name="id" method="post" action="cart.php?remove=<?php echo $item["id"] ?>"> 
     <input name="id" type="hidden" id="id" value="<? echo $item["id"]; ; ?>" /> 
     <input name="Submit" type="submit" class="formbox" value="remove" /> 
    </form></td> 
</tr> 
<?php } ?> 
+2

원하는 출력은 n입니다. ot 유효한 PHP 배열 ... 하위 배열에 대한 누락 된 키 –

+0

어떻게 키를 사용하여 수정하겠습니까? – Brent

답변

0

나는 당신이

Array 
(
      [65983c1c16d925e9a8cc4c4b3bdda1f5] => (
       [product] => Brent New Product 
       [unitprice] => 100 
       [quantity] => 1 
      ) 
      [4326526662262] => (
       [product] => Brent New Product2 
       [unitprice] => 140 
       [quantity] => 5 
      ) 
) 

처럼 할 수있는 배열 대안의 유형이 그리고이

session_start(); 
foreach($_SESSION['cart'] as $id => $item) { ?> 
<tr> 
<td><?php echo $item['item']; ?></td> 
<td><?php echo $item['unitprice'];?></td> 
<td><?php echo $item['quantity']; ?></td> 
<td></td> 
<td><form id="id" name="id" method="post" action="cart.php?remove=<?php echo $id ?>"> 
    <input name="id" type="hidden" id="id" value="<? echo $id; ?>" /> 
    <input name="Submit" type="submit" class="formbox" value="remove" /> 
    </form></td> 
</tr> 
<?php } ?> 

처럼 인쇄 그리고 당신이 세션을 채울 수 질수 있다고 생각 이 코드

session_start(); 
$cart = array(); 

$cart[] = array(
    $id => array(
     'product' => $product_name, 
     'unitprice' => $unit_price, 
     'quantity' => $quantity 
    ) 
); 

$_SESSION['cart'] = $cart; 
+0

죄송합니다.이 코드는 작동하지 않습니다. 배열이 처리되는 방식과 관련이 있습니다. – Brent

+0

@ BrentFrench 그것은 내 로컬 환경에서 작동하는 것 같습니다. 장바구니 솔루션을위한이 튜토리얼을 시험해 볼 수 있습니다 http://www.peachpit.com/articles/article.aspx?p=1962481 –

+0

내 배열은 지금 인쇄 할 때 이렇게 보입니다. http://pastebin.com/hcpDh4ZS – Brent