SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    London
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie Php Table Loop

    I'm sure this is such a newbie question, I'm almost embarrased to ask it...

    I am trying to display results called from a MYSQL database in an HTML table. I have managed to get it to work, but am now trying to get it to loop after 3 columns. IE, having a table that starts a new row every 3 columns.

    Anyway, the code I have used so far is:

    <html>
    <head>
    <title>Displaying MySQL Data</title>
    </head>
    <body>
    <?php
    $db_host = "localhost";
    $db_user = "root";
    $db_pwd = "";
    $db_name = "UPLOAD";
    mysql_connect($db_host, $db_user, $db_pwd);
    mysql_select_db($db_name);
    ?>
    <table border="1" align="center">
    <tr>
    <?php
    $sql = "SELECT * FROM upload2";
    $query = mysql_query($sql);
    while($row = mysql_fetch_array($query)) {
    echo "<td align='center' width='100'>"; echo "<a href='PHOTOS/".$row['name']."'><img src='PHOTOS/".$row['name']."'height='100'></a><br>".$row['name']."</td>";
    }
    ?>
    </tr>
    </table>
    </body>
    </html>

    Any ideas...I'm sure one of you fabulous people will be able to answer this in a blink of an eye, and I will be in awe of your supreme PHP ability.

    Many thanks,

    Dan

  2. #2
    SitePoint Addict lmasi02's Avatar
    Join Date
    Aug 2004
    Location
    Zambia
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replace this with
    Quote Originally Posted by dannyb77
    <table border="1" align="center">
    <tr>
    <?php
    $sql = "SELECT * FROM upload2";
    $query = mysql_query($sql);
    while($row = mysql_fetch_array($query)) {
    echo "<td align='center' width='100'>"; echo "<a href='PHOTOS/".$row['name']."'><img src='PHOTOS/".$row['name']."'height='100'></a><br>".$row['name']."</td>";
    }
    ?>
    </tr>
    </table>
    with
    PHP Code:
    <table border="1" align="center">

    <?php
    $sql 
    "SELECT * FROM upload2";
    $query mysql_query($sql);
    while(
    $row mysql_fetch_array($query)) {
    echo 
    "<tr>";
    echo 
    "<td align='center' width='100'>"; echo "<a href='PHOTOS/".$row['name']."'><img src='PHOTOS/".$row['name']."'height='100'></a><br>".$row['name']."</td>";
    echo 
    "</tr>";
    }
    ?>

    </table>
    Power of Knowledge

  3. #3
    SitePoint Addict lmasi02's Avatar
    Join Date
    Aug 2004
    Location
    Zambia
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey with this
    PHP Code:
    <table border="1" align="center"> 

    <?php 
    $sql 
    "SELECT * FROM upload2"
    $query mysql_query($sql); 
    $i=0;//counter
    while($row mysql_fetch_array($query)) { 
    if(
    $i%3==0) echo "<tr>";  
    echo 
    "<td align='center' width='100'>"
    echo 
    "<a href='PHOTOS/".$row['name']."'><img src='PHOTOS/".$row['name']."'height='100'></a><br>".$row['name']."</td>"
    if(
    $i%3==0) echo "</tr>"
    $i++;//incrementing counter

    ?> 

    </table>
    Power of Knowledge

  4. #4
    SitePoint Zealot musher's Avatar
    Join Date
    Nov 2005
    Location
    Hull, England
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit: What that ^ dude said.

    PHP Code:
    <table border="1" align="center">
    <tr>
    <?php
    $sql 
    "SELECT * FROM upload2";
    $query mysql_query($sql);
    $colCount 0;
    while(
    $row mysql_fetch_array($query)) {
    echo 
    "<td align='center' width='100'>"
    echo 
    "<a href='PHOTOS/".$row['name']."'><img src='PHOTOS/".$row['name']."'height='100'></a><br>".$row['name']."</td>";

    if ((
    $colCount++)%3=0) echo "</tr><tr>";
    }
    ?>
    </tr>
    </table>
    Something along these lines will print three columns, new row, three columns. It's off the top of my head and I never get the '%' thing right first time.


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
  •