Constructing HTML to output a set of numbers 5 per line
I am realy stuck with part of my php code! I have a range of numbers from 1 to 40 say that I pull from a table in my database and out put onto the screen using a while loop! with these numbers I am using a submit button which i will replace with a image button later on! Just now i can only get them in one line using a table but I want to get them into groups of say 5 or so colums and then go to next line print the next 5 or so colums! I have been trying for loops but they print out 1111, 2222, 3333, 4444, etc. in diffrent lines which is not what i want! I want,
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
etc
Please help me i have been baffeld with this for ages this is my code so far!
<?
$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'];
开发者_Go百科 $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>
Please help me i have been stuck on this problem for a good number of days and its driving me loopy lol!
Thank you Stephen
nested for-loops is the key:
$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';
}
something like this
I'm assuming that the 'real' problem lays somewhere between <table>
and </table>
and that you want to put the values returned by the fourth query (for some reason mapped to $row3
) in a table with 5 columns.
Using the PHP modulo operator ('%'), you could do something like this:
<?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)) + "\"> </td></tr>";
}
?>
I haven't tested the code myself.
For the sake of clearity, please consider using proper indentation and consistent variable naming in your code. Also note that using a proper title and less exclamation marks in your question would improve the clearity of it.
精彩评论