2013-06-26 102 views
0

这是代码,我有foreach循环重复第一行

<?php 
$checkbox = $_REQUEST['checkbox']; 
for($i=0; $i<count($_REQUEST['checkbox']); $i++){ 
    $del_id = $checkbox[$i]; 
    $get_info=$db->prepare("SELECT * FROM `pending_payments` WHERE `id` = ?"); 
    $get_info->bind_param('i', $del_id); 
    $get_info->execute(); 
    $result = $get_info->get_result(); 
    $info[] = $result->fetch_assoc(); 
    foreach($info as $row){ 
     $amount = $row['amount']; 
     $email = $row['email']; 
     echo" 
     <input type='text' style='height: 35px;' name='Amount[]' value='".$amount."' /> 
     <input type='text' style='height: 35px;' name='EmailAddress[]' value='".$email."' /><br /> 
     "; 
    } 
} 
?> 

,我有是,它是复制它发现的第一行的问题。我有2项到数据库中,并且在继续之前的第三,当我print_r$info它给了我这个

Array ([0] => Array ([id] => 1 [username] => ccarson030308 [amount] => 5.00 [processor] => PayPal [email] => [email protected] [submitted_date] => 1372030166)) 

然后显示echo该行,然后重新开始,并出现了两次重复的第一行

Array ([0] => Array ([id] => 1 [username] => ccarson030308 [amount] => 5.00 [processor] => PayPal [email] => [email protected] [submitted_date] => 1372030166) [1] => Array ([id] => 2 [username] => tatsu91 [amount] => 5.00 [processor] => PayPal [email] => [email protected] [submitted_date] => 1372030166)) 

这应该是它开始的唯一事情,我的for循环中是否有问题?我通常不会循环forforeach,但它似乎是我尝试做的最好的方法,但我失败了。

不添加追加[]我得到的 Warning: Illegal string offset 'amount' Warning: Illegal string offset 'email'

回答

1

您的代码追加到$info 12个错误做出每个查询。因此,在做foreach($info ...)时,您会看到在此迭代中产生的结果和以前的所有结果的输出。

因此输出将是这样的:

-------------- $i = 0 
Output from #1 
-------------- $i = 1 
Output from #1 
Output from #2 
-------------- $i = 2 
Output from #1 
Output from #2 
Output from #3 
-------------- etc 

你大概的意思是写

$info = $result->fetch_assoc(); // direct assignment, no append 
+0

如果我不加追加,我得到许多错误,这就是为什么我添加了附加到开始与 '警告:非法字符串偏移'金额' '警告:非法字符串偏移'电子邮件' 它重复这些错误12次只是2行 – kira423

+0

@ kira423:如果你添加它,你也会得到错误。所以不要随意更改不起作用的代码,而是正确调试它。非法字符串偏移意味着'$ row'不是数组。找出为什么。 – Jon

+0

我在调试它不是一个随机的变化,我只是在提问之前尝试一种新方法,以确保我确实需要帮助。 – kira423