SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    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
    }
    ?>
    Obviously it will display the same product three times in one table row, but how to display different products in one row please?

    Many thanks!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,156
    Mentioned
    190 Post(s)
    Tagged
    2 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) ? $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>"

  3. #3
    SitePoint Zealot
    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?

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,156
    Mentioned
    190 Post(s)
    Tagged
    2 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?

  5. #5
    SitePoint Zealot
    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>";
    ?>
    It displays one products per row. When I view the page source, there's no </TR> tag echoed. What's the possible cause for this?

  6. #6
    Use The Cloud
    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 == 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.

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,156
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    :d'oh: Sorry about that. I made a mistake using ++ in the ternary. It should be
    PHP Code:
    $incr = ($incr == 3) ? $incr 1

  8. #8
    SitePoint Zealot
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •