Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MySQL query result, show only 5 results per line, after 5 are shown, start a new line.

Posted on 2005-03-17
7
Medium Priority
?
368 Views
Last Modified: 2013-12-12
How would I go about looping the results of a query to only show 5 results per line in my HTML? For example, There are 10 entries in the database table, I need to retrieve those, and then display 5 entries on each line of my template.... something like:

Row 1, Row 2, Row 3, Row 4, Row 5 (start a new line afterwards)
Row 6, Row 7, Row 8, Row 9, Row 10

How can I go about that? I know it's possible, I've seen it done numerous times. I'd also like to have a "default content" if the number of rows is not divesible by 5. Like if I had only 8 rows, I'd like to have a   to "take their place".
0
Comment
Question by:MaybeItsJeremy
7 Comments
 
LVL 32

Assisted Solution

by:Batalf
Batalf earned 664 total points
ID: 13565427

echo "<table>";
$countRecords=0;
$res = mysql_query("your query here");
while($inf = mysql_fetch_array($res)){
    if($countRecords%5==0 && $countRecords>0)echo "</TR><TR>";
    echo "<td><p>Your sql data here</td>";
    $countRecords++;

}
0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 672 total points
ID: 13565449
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
   if ($i % 5 == 0 && $i > 0) {
      // start a new line here
   }
   //output entry here
  $i++;
}
while ($i % 5 != 0) {
  // output default &nbsp
  $i++;
}
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13565500
<?php

echo "<table>";
echo "<tr>";
while($row = mysql_fetch_assoc($result)) {
 foreach($i=1; $i<=count($row); $i++) {
  echo "<td> $row[$i-1] </td>";
  if($i > 1 && $i%5==0) { echo "</tr><tr>"; }
 }
 for($i=1; $i <= $i%5; $i++) {
  echo "<td>&nbsp;</td>";
  $isNextTr = true;
 }
 if(isset($isNextTr)) { echo "</tr>"; }
}

?>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 32

Expert Comment

by:ldbkutty
ID: 13565510
Typo.

<?php

echo "<table>";
echo "<tr>";

while($row = mysql_fetch_assoc($result)) {
 for($i=1; $i<=count($row); $i++) {
  echo "<td> $row[$i-1] </td>";
  if($i > 1 && $i%5==0) { echo "</tr><tr>"; }
 }
 for($i=1; $i <= $i%5; $i++) {
  echo "<td>&nbsp;</td>";
  $isNextTr = true;
 }
 if(isset($isNextTr) && $isNextTr == true) { echo "</tr>"; }
}

echo "</table>";
?>
0
 
LVL 32

Assisted Solution

by:ldbkutty
ldbkutty earned 664 total points
ID: 13565565
Sorry, change mysql_fetch_assoc to mysql_fetch_row

This is better :

<?php

echo "<table>";
echo "<tr>";

while($row = mysql_fetch_row($result)) {
 for($i=1; $i<=count($row); $i++) {
  echo "<td> $row[$i-1] </td>";
  if($i > 1 && $i%5==0) { echo "</tr><tr>"; }
 }
 for($i=1; $i <= count($row)%5; $i++) {
  echo "<td>&nbsp;</td>";
  if($i == count($row)%5) { echo "</tr>"; }
 }
}

echo "</table>";
?>
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13565669
(Sorry)^1000 and no more changes. (well, hopefully!)

Change the second "for" loop to this :

for($i=1; $i <= 5 - count($row)%5; $i++) {
  echo "<td>&nbsp;</td>";
  if($i == 5 - count($row)%5) { echo "</tr>"; }
 }
}
0
 

Author Comment

by:MaybeItsJeremy
ID: 13581119
Thanks everyone... points split an evenly as possible...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question