PHP的复选框爆炸函数


PHP explode function from checkboxes

我有一个表单,我提交一些值(从复选框)到另一个页面,在那个页面上,我使用一个爆炸函数来打破数组内的字符串。但是当我把count()函数放在爆炸上时,我得到了一个额外的(+1)值。

HTML

<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>
<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>
<tr align = 'center' class = "odd">
<?php
    while($row = mysql_fetch_array($run))
    {
        $i_id = $row['item_id'];
        $i_name = $row['item_name'];
        $i_price = $row['item_price'];
        ?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php
$item = $i_name ." ". $i_price;  
?>                                                      
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td> 
</tr>
<?php  }?><input type = "hidden" name = "check">
<button type=  "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php }  ?>  
</table>
</form>

PHP (on page 2)

 $prd = implode(",",$_POST['addcart']);
 $final = explode(",", $prd);
 for($i=0; $i<=count($final); $i++)
 {
 echo count($final);   //this is where I'm getting the +1 to original count and hence everything falls apart.
 echo $final[$i];
 }

注意:我已经包含了所有必要的文件,如config.php和PHP文件中的所有内容。

为什么这么多额外的代码,直接像下面这样做:-

 foreach($_POST['addcart'] as $val){
   echo $val;
}

因为count()从1开始计数,而for循环从0开始计数。

替换这一行:

for($i=0; $i<=count($final); $i++)

for($i=0; $i<=count($final)-1; $i++)