2010-05-10 5 views
0

Codeigniter 장바구니를 구축 중입니다. 장바구니 세부 정보 페이지에는 사용자가 제품에 필요한 수량을 입력 할 수있는 양식 입력 필드와 정보를 업데이트 기능에 게시하는 제출 버튼이 있습니다.PHP Codeigniter Undefined 오프셋 오류

카트에 항목이 하나만있을 때 수량을 업데이트 할 때 모든 항목이 정상적으로 작동합니다. 그러나 하나 이상의 항목이있는 경우 항목의 수량을 변경하고 제출을 클릭하면 모델의 다음 코드 (특히 배열의 두 줄)에서 '정의되지 않은 오프셋 1 : 오류'가 발생합니다.

이것은 위에서 참조하는 뷰 코드 :

<form action="<?php echo base_url(); ?>home/update" method="post"> 
     <div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div> 
     <div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div> 
     <div><input type="submit" value="update" class="update-quantity"/></div> 
</form> 

는 그리고 이것은 컨트롤러입니다 :

function update() 
    { 
    $this->products_model->validate_update_cart(); 
     redirect('cart'); 
    } 

이런 일이 왜 사람이 설명 할 수주십시오?

많은 감사,

매트

답변

1

나는 당신의 문제는 당신이 즉

<form action="<?php echo base_url(); ?>home/update" method="post"> 
     <div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div> 
     <div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div> 

     <div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div> 
     <div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div> 

     <div><input type="submit" value="update" class="update-quantity"/></div> 
</form> 

의 ROWID 및 수량의 2 개 항목을 가질 필요가 있다고 믿습니다.

link은 HTML 입력과 함께 표준 배열과 연관 배열을 모두 사용하는 예를 제공합니다.

EDIT OP 피드백에 기초 :

이 너무 언급 된 예였다

<label><input type="checkbox" name="choice[]" value="1"/> 1</label> 
<label><input type="checkbox" name="choice[]" value="2"/> 2</label> 
<!-- etc... --> 

// meanwhile, on the server... 
$choice = $this->input->post('choice'); 
print_r($choice); // Array ([0] => 1 [1] => 2); 

다른 예 : 실시 예는 입력을 사용

<form method="post" action=""> 
    <input maxlength="30" name="friend[]" size="30" type="text" /> 
    <input maxlength="30" name="friend[]" size="30" type="text" /> 
    <input maxlength="30" name="friend[]" size="30" type="text" /> 
    <input type="submit" value="Submit" /> 
</form> 

// ***** Server-Side PHP: ***** 

// Loop through the friend array 
foreach ($_POST['friend'] as $value) { 
    if ($value) { echo $value."<br />"; } 
} 

공지 그들이 배열로 돌아올 것으로 기대하는 각각의 가치에 대해 똑같은 "어쩌구 저쩌구"와 함께. 귀하의 코드에서 볼 때 하나의 rowid []와 하나의 qty [] 입력이 있습니다. 단일 요소의 경우이 배열에서 정의 된 하나의 요소가있는 b/c를 사용할 수 있습니다. 당신이 2 개의 아이템을 가지고 있고 총 아이템 변수를 업데이트하여 정확한 아이템 수를 나타내지 만, 존재하지 않는 두 번째 요소 (즉, 1)에 접근하려고 시도 할 때 루프가 발생하여 " 정의되지 않은 오프셋 1 "오류입니다. 대신

for($i=0;$i < $total;$i++) 

사용

+0

감사합니다. RC, 유용한 기사입니다. 제안 된대로 두 입력이 필요하다는 것을 알 수있는 내용이 없습니다. 이 작업을 수행하면 수량을 업데이트하기위한 입력 필드가 두 개가 생길 수 있습니다. – Matt

+0

답변을 업데이트했습니다. –

+0

오, 네가하는 말을 보았다. 나에게 돌아와서 고마워. 이걸 시험해 볼게 ... 다시보고 할게. – Matt

2

저도 같은 문제를 가지고
for($i=0;$i < count($item);$i++) 
2

; 문제가 장바구니보기 섹션에 있음을 확신합니다. 숨겨진 필드는 foreach {} 문 안에 들어 있지 않습니다. 따라서 장바구니에 하나의 제품이있을 때 수량을 편집 할 수 있지만 은 다른 제품을 추가 할 때 제품을 편집 할 수 없습니다. 나에게 도움이 된 코드 조각이있다.

<?php if ($this->cart->total_items()!=0) :?> 
<div id="cart"> 

    <?php if ($cart=$this->cart->contents()) :?> 
    <table> 
    <caption>Shopping Cart</caption> 
    <thead> 
     <th>Item Name</th> 
     <th>Option</th> 
     <th>Price</th> 
     <th>Qty</th> 
     <th></th> 
    </thead> 
    <?php foreach ($cart as $item): ?> 
    <tr> 
     <td><?=$item['name'];?></td> 
     <td> 
     <?php 
      if ($this->cart->has_options($item['rowid'])) { 
       foreach ($this->cart->product_options($item['rowid']) as $option => $value) { 
        echo $option.": <em> ".$value." </em>"; 
       }; 
      }; 
     ?> 
     </td> 
     <td>$<?=$item['subtotal'];?></td> 
     <?=form_open('index.php/shop/update_cart'); ?> 
     <td> 
     <?=form_input(array('name' => 'qty[]', 'value' => $item['qty'], 'maxlength' => '2', 'size' => '2')); ?> 
     </td> 
     <td class="remove"><?=anchor('index.php/shop/delete/'.$item['rowid'],'X','class="remove"');?></td> 
     <td> <?=form_hidden('rowid[]', $item['rowid']); ?></td> 
    </tr> 
    <?php endforeach;?> 
    <tr class="total"> 
     <td colspan="2"> <strong>Total</strong> </td> 
     <td>$<?=$this->cart->total();?></td> 
    </tr> 
    <tr> 
     <td><?php echo form_submit('submit', 'Update your Cart'); ?></td> 
     <!-- When you want to empty your cart using ajax, add 'class="empty"' as a third parameter. --> 
     <td><?=anchor('index.php/shop/empty_cart', 'Empty Cart', 'class="empty"');?></td> 
     <?=form_close();?> 
    </tr> 
    </table> 
    <?php endif;?> 
</div> 
<?php endif;?>