SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Dec 26, 2008, 22:07 #1
- Join Date
- Jun 2006
- Posts
- 166
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
how to display 3 mysql table data in one html table row
Hi all,
I have a mysql table contains products info, such as product image and code. I'm tring to display them in a html table, and I need to put three different products in one table row, then another three products in the second table row, and so on.
PHP Code:<?php
while ($list = mysql_fetch_assoc($result)) {
?>
<TR>
<TD>
<a href="images/<?php echo $list['image']; ?>.JPG" rel="lightbox" title="Ba <?php echo $list['itemcode'];?>"><IMG SRC="images/a_<?php echo $list['image']; ?>.JPG" ><BR> View Larger Image</a>
<BR><?php echo $list['itemcode'];?>
</TD>
<TD>
<a href="images/<?php echo $list['image']; ?>.JPG" rel="lightbox" title="Ba <?php echo $list['itemcode'];?>"><IMG SRC="images/a_<?php echo $list['image']; ?>.JPG" ><BR> View Larger Image</a>
<BR><?php echo $list['itemcode'];?>
</TD>
<TD>
<a href="images/<?php echo $list['image']; ?>.JPG" rel="lightbox" title="Ba <?php echo $list['itemcode'];?>"><IMG SRC="images/a_<?php echo $list['image']; ?>.JPG" ><BR> View Larger Image</a>
<BR><?php echo $list['itemcode'];?>
</TD>
</TR>
<?php
}
?>
Many thanks!
-
Dec 26, 2008, 23:00 #2
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
The easiest way is to not have all three td cells inside the while loop. If you want to have three columns per row you can use an incrementer or modulus to create the rows. Something like
PHP Code:$incr = 3;
while .... {
$incr = ($incr == 3) ? 1 : $incr++;
if($incr == 1) echo "<tr>";
echo "<td> ........ ";
if($incr == 3) echo "</tr>";
}
if($incr == 1) echo "<td></td><td></td></tr>";
if($incr == 2) echo "<td></td></tr>";
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 27, 2008, 14:53 #3
- Join Date
- Jun 2006
- Posts
- 166
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks Mittineague, I'm not very good at php, and I cant get it to work, could you please explain the above code in more details?
-
Dec 27, 2008, 15:45 #4
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
The fetch() gets results 1 row at a time putting the fields into an array.
The incrementer is used to keep track of how many rows have been returned. I used a ternary to either reset it to 1 or increase it by 1 as needed.
As you want 3 columns per row, the if conditionals test to see if there should be a new row, and if so, writes the tr tags. Otherwise the loop just writes the td tags. Unless you always get results in multiples of 3, the possiblity exists that the last row won't have 3 columns in it, so after the loop it "fills in" the row with any empty cells that might be needed.
If you got confused by the dots ......, that's where your existing code for that goes, I didn't put in in so you could see the logic of the incrementing easier.
If you did put your code in for the dots but aren't getting the correct output, what are you getting?Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 27, 2008, 20:38 #5
- Join Date
- Jun 2006
- Posts
- 166
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks again Mittineague, this is the code I'm using:
<?php
// while there are rows to be fetched...
$incr = 3;
while ($list = mysql_fetch_assoc($result)) {
$incr = ($incr == 3) ? 1 : $incr++;
if($incr == 1) echo "<TR>";
?>
<TD width="180" class="product">
<a href="images/bangle/DSC<?php echo $list['image']; ?>.JPG" rel="lightbox" title="Ba <?php echo $list['itemcode'];?>"><IMG SRC="images/bangle/a_DSC<?php echo $list['image']; ?>.JPG" WIDTH="160" HEIGHT="120" BORDER="0" ALT=""><BR> View Larger Image</a>
<BR><BR><B>Ba <?php echo $list['itemcode'];?></B>
</TD>
<?php
if($incr == 3) echo "</TR>";
}
if($incr == 1) echo "<td></td><td></td></tr>";
if($incr == 2) echo "<td></td></tr>";
?>
-
Dec 27, 2008, 20:53 #6
- Join Date
- Jan 2006
- Location
- Boise, ID
- Posts
- 556
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:<?php
$total = mysql_num_rows($result);
echo '<tr>';
for ($i = 1; $list = mysql_fetch_assoc($result); $i++) {
echo '<td>...</td>';
if (($i % 3 == 0) && ($i < $total)) {
echo '</tr><tr>';
}
}
echo '</tr>';Brad Hanson, Web Applications & Scalability Specialist
► Is your website outgrowing its current hosting solution?
► PM me for a FREE scalability consult!
► USA Based: Available by Phone, Skype, AIM, and E-mail.
-
Dec 28, 2008, 00:15 #7
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
:d'oh: Sorry about that. I made a mistake using ++ in the ternary. It should be
PHP Code:$incr = ($incr == 3) ? 1 : $incr + 1;
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 28, 2008, 12:49 #8
- Join Date
- Jun 2006
- Posts
- 166
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks very much Mittineague and Bhanson! Both methods work great! Have a fab new year!
Bookmarks