Formatting results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CerIs
Forum Newbie
Posts: 22
Joined: Sat May 29, 2004 9:20 am

Formatting results

Post by CerIs »

Say i know how many results im have got from my query, i then want to format them into a table on the page. If want to have 5 columns and i have 17 results back, i want to go along putting each result in a cell till it gets to the end of the row then start a new row. Im not sure how to do this. Ie get it to only print out 2 cells on the last row.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

<?php
$NoOfResults=23;
echo '<TABLE BORDER=1><TR>';

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

 $row=mysql_fetch_array($query);
 echo '<TD>'.$row['whatever'].'</TD>';
 if ($i%5==0)
  echo '</TR><TR>';

}

echo '</TR></TABLE>';
?>
CerIs
Forum Newbie
Posts: 22
Joined: Sat May 29, 2004 9:20 am

Post by CerIs »

:) :) :) That worked !!! Thanks for your help Anjanesh
CerIs
Forum Newbie
Posts: 22
Joined: Sat May 29, 2004 9:20 am

Post by CerIs »

One question though. In ($i%5==0 ) whats the % do ?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

It's the modulus operator.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
CerIs
Forum Newbie
Posts: 22
Joined: Sat May 29, 2004 9:20 am

Post by CerIs »

ahhh so its testing to see if theres a remainder: eg 5,10,15 will have no remainder and so start a new row? Thats pretty smart. :o
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

a%b gives the remainder when a is divided by b
Post Reply