?
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
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 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