开发者

fetchArray() omitting the first result

I got the following code on my php script:

$result = $conect->query("SELECT nome, cla开发者_StackOverflowssificacao 
        FROM ".$conecta->prefix('inscritos_resultados')." 
        WHERE classificacao >=1 AND classificacao <=50 
        ORDER BY classificacao");

$o_grupo = 0;
while ($usuarios = $conecta->fetchArray($result)) {
    if ($o_grupo == 0){
        echo "<tr class=\"even\"><th colspan=\"1\"> Classificação </th>
             <th colspan=\"2\"> Nome </th>";
        $o_grupo = 1;
    } else{
        echo "<tr class=\"odd\" onmouseout=\"this.className='odd';\"
             onmouseover=\"this.className='even';\"><th colspan='1'>
             <strong>".$usuarios["classificacao"]." </strong></th>
              <th colspan='2'> ".$usuarios["nome"]." </th>";
    }
}

Why it is omitting the first result?


Because you echo the data in the else statement. Even if $o_grup == 0 you still want to display the first record, so you do not want the else. Simply an if:

while ($usuarios = $conecta->fetchArray($result)) {
    if ($o_grupo == 0){
        echo "<tr class=\"even\"><th colspan=\"1\"> Classificação </th><th colspan=\"2\"> Nome </th>";
        $o_grupo = 1;
    } 
    echo "<tr class=\"odd\" onmouseout=\"this.className='odd';\" onmouseover=\"this.className='even';\"><th colspan='1'><strong> ".$usuarios["classificacao"]." </strong></th><th colspan='2'> ".$usuarios["nome"]." </th>";
}

Should solve that issue.

EDIT

But why even have that in the loop anyways?

echo "<tr class=\"even\"><th colspan=\"1\"> Classificação </th><th colspan=\"2\"> Nome </th>";
while ($usuarios = $conecta->fetchArray($result)) {
    echo "<tr class=\"odd\" onmouseout=\"this.className='odd';\" onmouseover=\"this.className='even';\"><th colspan='1'><strong> ".$usuarios["classificacao"]." </strong></th><th colspan='2'> ".$usuarios["nome"]." </th>";
}

Should work as well, without the need for the if in the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜