2014-02-07 5 views
0

я сделать страницу PHP, в котором я хочу, чтобы получить общее количество строк, но это дает мне результат нулевой, как я просуммировать Num строк в PHP, вот мой код:Как получить общее количество строк из цикла?

<? 
$recepients=0; 
$count=count($_POST['events']); 
for($i=0; $i<$count; $i++){ 
    $select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id"; 
    $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__); 
    $record=mysqli_num_rows($res); 
} 
echo $recepients+=$record; 

?> 
+0

ли вы проверить, если он входит в цикл? А также отлаживать '$ res'. Или у вас есть нулевой результат из db –

+0

да, я проверил результат, показаны –

ответ

2

Попробуйте добавить в петля

<? 
    $recepients=0; 
    $count=count($_POST['events']); 
    for($i=0; $i<$count; $i++){ 
        $select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id"; 
     $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__); 
     $record=mysqli_num_rows($res); 

     $recepients = $recepients + $record; 
    } 
    echo $recepients; 

    ?> 
1
<? 
$recepients=0; 
$count=count($_POST['events']); 
for($i=0; $i<$count; $i++){ 
    $select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id"; 
    $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__); 
    $record = mysqli_num_rows($res); 
    $recepients += $record; // i think you're missing this 
} 
echo $recepients; // edited - forgot to edit this one. 
?> 

Я думаю, вы просто пропустите 1 строку кода.

+1

Я думаю, у вас есть одна дополнительная строка '$ recepients + = $ record'. обновить его до '$ recepients' –

1

Расчет должен быть сделан внутри петли,

$recepients = 0; 
    for($i=0; $i<$count; $i++){ 
      $select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id"; 
      $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__); 
      $record=mysqli_num_rows($res); 
      $recepients += $record; 
    } 
    echo $recepients; 
Смежные вопросы