Jump to content


Photo

Create a new row every when $i = 5


  • Please log in to reply
5 replies to this topic

#1 samtwilliams

samtwilliams

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 02 February 2013 - 10:00 AM

I am trying to draw a table dynamically, I need a table that is 5 x 5, i know i'm close quite my code by just can't quite fathom it out. My sql returns 25 results which i want to return 25 cells in a square table.

<?PHP
$i = 0;
while($row = mysql_fetch_assoc($result)) {
$i++;
?>
<?PHP
if($i== 5) {
echo '<tr>';
}
?>
<td class="right bottom"><?PHP echo $row['GridID']; ?></td>
<td class="right bottom">&nbsp;</td>
<td class="right bottom">&nbsp;</td>
<td class="right bottom">&nbsp;</td>
<td class="bottom">&nbsp;</td>


<?PHP
if($i== 5) {
echo '</tr>';
$i=0;
}
?>


#2 razorsese

razorsese

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts

Posted 02 February 2013 - 10:23 AM

Have you tried replacing the statement if($i == 5) with if($i % 5 ==0 ) so at every 5 item its creating a new tr?

#3 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 02 February 2013 - 10:34 AM

You need to end the row BEFORE starting a new one. You're ending the new row you just created.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#4 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,229 posts
  • LocationCheshire, UK

Posted 02 February 2013 - 11:33 AM

or use floating divs, 20% wide

echo "<div style='width:200px'>\n";
for ($i=1; $i<=25; $i++) {
    echo "<div style='width:20%; float:left; text-align:center; border:1px solid gray;'>
    $i
    </div>";
}
echo "</div>\n";

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#5 samtwilliams

samtwilliams

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 02 February 2013 - 11:47 AM

I need to use tables, still can't get it to work, my table just looks a mess.

#6 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,229 posts
  • LocationCheshire, UK

Posted 02 February 2013 - 12:06 PM


echo "<table border='1' style='width:200px; border-collapse:collapse;'>\n";

echo "<tr>";

for ($i=1; $i<=25; $i++) {

	echo "<td style='text-align:center; '>

	$i

	</td>";

	if ($i%5==0)

		echo "</tr><tr>";

}

echo "</tr></table>\n";

Edited by Barand, 02 February 2013 - 12:07 PM.

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com