根据COUNT查询创建行和列

时间:2011-11-10 01:46:29

标签: php html tablerow

简单的问题我相信任何具有最低PHP技能的人(我没有足够数量的haha)

$numrows = $retour['nb'] / 4;
echo $numrows;

echo "<table><tr>";
while ($callback = mysql_fetch_assoc($queryLocations2))
{   
echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) .  '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' .  $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' .  utf8_encode($callback['loc_state']) . '</td>');
}
echo "</tr></table>";
}

我将如何根据$numrows的值显示每行将包含4个结果(4列)的表格?

谢谢!

2 个答案:

答案 0 :(得分:1)

在循环中输出tr标签:

$count = 0;
echo "<table>"; 
while ($callback = mysql_fetch_assoc($queryLocations2)) 
{    
    if ($count % 4 == 0)
        echo '<tr>';

    $count++;

    echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) .  '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' .  $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' .  utf8_encode($callback['loc_state']) . '</td>'); 

    if ($count % 4 == 0)
        echo '</tr>';
}

if ($count % 4 != 0)
{
    // need to add missing td-s here

    echo '</tr>';
}
echo "</table>"; 

答案 1 :(得分:0)

$numrows = floor($retour['nb'] / 4);

echo $numrows;
$i=0;
echo "<table>";

while ($callback = mysql_fetch_assoc($queryLocations2))
{  
   if($i==4)

      {

         echo "<tr>"; 

         echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) .  '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' .  $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' .  utf8_encode($callback['loc_state']) . '</td>');

         echo "</tr>";

         $i=0;

      }

$i++;

}

while ($i<4) 
   { 

       echo '<td></td>';
       $i++;
   }

echo "</table>";

}



//if numrows used for # of rows then use following


$count=0;
while ($callback = mysql_fetch_assoc($queryLocations2) && $count<=$numrows)
{  

if($i==4)

echo "<tr>"; 

echo utf8_encode('<td><img src="/flags/' . strtolower($callback['loc_code']) .  '.png" id="' . $callback['loc_id'] . '"><input type="checkbox" value="' .  $callback['loc_url'] . '" />' . $callback['loc_city'] . ', ' .  utf8_encode($callback['loc_state']) . '</td>');

if($i==4)

{

   echo "</tr>"; 
   $i=0;
   $count++;

}

$i++;

}
while ($i<4) 

{ 
    echo '<td></td>';
    $i++;
}

echo "</table>";

}