构造HTML以输出每行5个数字

时间:2011-02-07 23:13:41

标签: php mysql while-loop

我真的被我的部分PHP代码卡住了!我有一系列从1到40的数字说我从数据库中的表中拉出来并使用while循环输出到屏幕上!使用这些数字我正在使用提交按钮,我将在稍后用图像按钮替换!刚才我只能用一张桌子将它们排成一行,但是我想把它们分成5个左右的列,然后到下一行打印下5个左右的列!我一直在尝试循环,但他们打印出1111,2222,3333,4444等不同的线,这不是我想要的!我想要,

 1  2  3  4  5 
 6  7  8  9 10
11 12 13 14 15 
etc

请帮助我,我已经被这个多年了,这是我的代码到目前为止!

<?     
  $q3 = "SELECT * FROM tblgame";
  $r3 = mysql_query($q3);
  while($row2 = mysql_fetch_array($r3))
  {
    $game_cost = $row2['game_cost'];      

    echo "<p>Game ID: ".$row2['game_id'];
    echo "<br>Game Day: ".$game_day;
    echo "<br>Next Game Date: ".$row2['game_date_day']."/".$row2['game_date_month']."/".$row2['game_date_year'];

    $fyear = $row2['game_date_year'];
    $fmonth = $row2['game_date_month'];
    $fday = $row2['game_date_day'];
    $tmonth = $date_month;
    $tyear = $date_year;
    $tday = $date_day;
    $days_between = abs((mktime ( 0, 0, 0, $fmonth, $fday, $fyear) - mktime ( 0, 0, 0, $tmonth, $tday, $tyear))/(60*60*24)); 

    echo "<br>Days till next draw: ". $days_between;
    echo "<br />Game Cost: £".$row2['game_cost']."</p>";
 ?>
 <table>
 <tr>
 <?   
    $q4 = "SELECT * FROM tblnewgameitem WHERE game_id = $row2[game_id] ORDER BY 'game_number' ";
    $r4 = mysql_query($q4);
    $n1 = mysql_num_rows($r4);
    $i=0;
    while($row3 = mysql_fetch_array($r4))
    {
    ?>
     <td>
     <?
       if($row3['user_email'] == "")
       { 
       ?>
           <form action="buyanumber.php" method="POST">
             <input type="hidden" name="game_id" value="<?echo $row2['game_id'];?>">
             <input type="hidden" name="num" value="<? echo $row3['game_number']; ?>">
             <input type="submit" value="<?echo $row3['game_number'];?>" name="submit">
           </form>                                
       <?
       }
       else
       {
         $n = $n + 1;
         echo " ".$row3['game_number']." ";       
       }
       ?>
     </td>
     <?
   }
   ?>
 </tr>
 </table>

请帮助我,我已经被困在这个问题上好多天了,它让我开始啰嗦哈哈!

谢谢 斯蒂芬

2 个答案:

答案 0 :(得分:0)

嵌套for循环是关键:

$list=array();
while($row2 = mysql_fetch_array($r3)) $list[] = $row2;

$countList = count($list);
$cols = 5;
$rows = ceil($countList / $maxPerRow);

for ($i=0; $i<$rows; $i++) {
    echo 'opening stuff per row... <tr> or something';
    for ($j=0; $j<$rows; $j++) {
        echo 'your stuff per item... might be somthing like <td>s';
    }

    echo 'closing stuff per row... </tr> or something';
}

类似这样的事情

答案 1 :(得分:0)

我假设“真正的”问题位于<table></table>之间,并且您希望将第四个查询返回的值放入(由于某种原因映射到$row3在一个有5列的表中。

使用PHP模运算符('%'),您可以执行以下操作:

<?php
    $r4 = <your mysql_query>
    $i = 0;
    while ($row3 = mysql_fetch_array($r4)) {
        if ($i % 5 == 0) { // true for 0, 5, 10, ...
            echo "<tr>";
        }
        echo "<td>";
        // what you want to put between your <td> tags comes here
        echo "</td>";
        if (($i+1) % 5 == 0) { // true for 4, 9, 14, ...
            echo "</tr>";
        }
        $i++;
    }
    // if the number of rows is not a multiple of 5, we must clean up after ourselves:
    if ($i % 5 != 0) {
        echo "<td colspan=\"" + (5-($i % 5)) + "\">&nbsp;</td></tr>";
    }
?>

我自己没有测试过代码。

为了清楚起见,请考虑在代码中使用适当的缩进和一致的变量命名。另请注意,在您的问题中使用正确的标题和较少的惊叹号可以提高它的清晰度。