开发者

fetchArray() omitting the first result

开发者 https://www.devze.com 2023-01-10 08:43 出处:网络
I got the following code on my php script: $result = $conect->query(\"SELECT nome, cla开发者_StackOverflowssificacao

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

精彩评论

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