SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP / HTML table issue

    I'm having a hard time with displaying data from my database in a three cell table layout.

    Here's what I'm using right now for code:
    PHP Code:
          while ($row mysql_fetch_array($result)) {
          echo 
    '
          <table class="cells">
             <tr>
                  <td style="width: 162px;">
                     <img src="' 
    $row['m_img'] . '" alt="' $row['m_title'] . '" />
                     <br />
                     <a href="' 
    $row['m_url'] .'">' $row['m_title'] . '</a>
                  </td>
             </tr>
          </table> 
    What happens is that each item and image is displayed as one row in a table. Any suggestions?
    Get to know the man behind the screen

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '<table class="cells">';
    while (
    $row mysql_fetch_array($result)) {
          echo 
    '
             <tr>
                  <td style="width: 162px;">
                     <img src="' 
    $row['m_img'] . '" alt="' $row['m_title'] . '" />
                     <br />
                     <a href="' 
    $row['m_url'] .'">' $row['m_title'] . '</a>
                  </td>
             </tr>'
    ;
    }
    echo 
    '</table>'
    You are outputting the <table> tag in the loop, which means it gets outputted for every row in your result set, which means every row gets its own table.

  3. #3
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, yes of course. Now, the only problem is that each <td> is under one another, what I'd like is this (and I've been trying different selections:

    ----------
    |td|td|td|
    |td|td|td|
    |td|td|td|
    ----------

    I'm stumped.
    Get to know the man behind the screen

  4. #4
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wht kinda o/p u want

  5. #5
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just like I posted above, just like this:
    ----------
    |td|td|td|
    |td|td|td|
    |td|td|td|
    ----------

    Right now this is the output:
    |td|
    |td|
    |td|
    Get to know the man behind the screen

  6. #6
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <table width="100%" border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    use such table structur in ur loop

  7. #7
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see how that's possible with this code:
    PHP Code:
    echo '<table class="cells">';
          while (
    $row mysql_fetch_array($result)) {
          echo 
    '
             <tr>
                  <td style="width: 162px;">
                     <img src="' 
    $row['m_img'] . '" alt="' $row['m_title'] . '" />
                     <br />
                     <a href="' 
    $row['m_url'] .'">' $row['m_title'] . '</a>
                  </td>
             </tr>'
    ;
       }
       echo 
    '</table>'
    I know how to make a table, just not using PHP data.
    Get to know the man behind the screen

  8. #8
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '<table class="cells">';
    while (
    $row mysql_fetch_array($result)) {
          echo 
    '<tr>
                    <td><img src="' 
    $row['m_img'] . '" alt="' $row['m_title'] . '" /></td>
                    <td> Somethin here </td>
                    <td><a href="' 
    $row['m_url'] .'">' $row['m_title'] . '</a></td>
                  </tr>'
    ;
                
    }
    echo 
    '</table>'
    is this wht u want

  9. #9
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Play around with the modulo operator:

    PHP Code:
    <?php
        $max     
    10;
        
    $i        1;
        while (
    $i<$max)
        {
            if (
    $i &#37; 3 == 0) print $i . "<br>";
            
    $i++;
        }
    ?>
    Output:

    PHP Code:
    3
    6

    This bit... "$i % 3 == 0" tells PHP to do something everytime $i is dividable by 3. Use this to end and start your rows.

    Hope this helps

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im cutting and pasting some code, you will need to adapt it.

    PHP Code:
    // how many columns we want our table to have
    $columns 3;



    echo 
    "<table>\n";

    for (
    $i 0$row mysql_fetch_assoc($result); $i++) {

        if ((
    $i $columns) == 0) {
            echo 
    " <tr>\n";
        }

        echo 
    "  <td>" $row['fieldname'] . "</td>\n";

        if ((
    $i $columns) == ($columns 1)) {
            echo 
    " </tr>\n";
        }

    }

    // find out how many empty <td></td> we need to output now
    // to make the html correct and well formed
    $remainder = ($columns - ($i $columns)) % $columns;

    // do we have remainders?
    if ($remainder 0) {
        echo 
    str_repeat("  <td></td>\n"$remainder);
        
    // or alternatively
        // echo "  <td colspan=\"$remainder\"></td>\n";
        
    echo " </tr>\n";
    }
    echo 
    "</table>\n"
    you could also do similar with array_chunk() and array_pad()

  11. #11
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what i'm using:
    PHP Code:
    // how many columns we want our table to have
          
    $columns 3;
          echo 
    "<table>\n";
          for (
    $i 0$row mysql_fetch_assoc($result); $i++) {
          if ((
    $i &#37; $columns) == 0) {
            
    echo " <tr>\n";
           }

            echo 
    "  <td>"<img src=\"' . $row['m_img'] . ' \" alt=\"' . $row['m_title'] . '\" />"</td>\n";

          if ((
    $i % $columns) == ($columns - 1)) {
            echo " 
    </tr>\n";
           }

          }

          // find out how many empty <td></td> we need to output now
          // to make the html correct and well formed
          
    $remainder = ($columns - ($i % $columns)) % $columns;

          // do we have remainders?
          if (
    $remainder > 0) {
           echo str_repeat("  
    <td></td>\n", $remainder);

           // or alternatively
           // echo "  
    <td colspan=\"$remainder\"></td>\n";
           echo 
    " </tr>\n";
          }
          echo 
    "</table>\n"
    and I get this:
    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/ogden2k/public_html/ogden2k/iedb/movies/gallery.php on line 36

    Same thing if I take the \ out before the "
    Get to know the man behind the screen

  12. #12
    SitePoint Addict Procode's Avatar
    Join Date
    Dec 2006
    Location
    New York
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly is line 36?

  13. #13
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo " <td>"<img src="' . $row['m_img'] . ' \" alt=\"' . $row['m_title'] . '\" />"</td>n";
    Get to know the man behind the screen

  14. #14
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any suggestions?
    Get to know the man behind the screen

  15. #15
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it to work... the problem was the " vs the '.
    Get to know the man behind the screen

  16. #16
    SitePoint Addict Mainer82's Avatar
    Join Date
    Aug 2003
    Location
    Maine, USA
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got it to work... the problem was the " vs the '.
    Get to know the man behind the screen

  17. #17
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    PHP Code:
    <? echo " <td><img src='".$row['m_img']."' alt='".$row['m_title']."' /></td>n"?>


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
  •