PHP loop through database and limit number of entries

Discussion in 'PHP and MySQL' started by gilbertsavier, Aug 4, 2009.

  1. gilbertsavier New Member

    Going to try to explain this as best I can. I have a bunch of stuff in a database and I need to display this info within a table. This isn't a problem at all but, I need it to only display 3 td tags per line and then move on to the row.

    so i need it to do :

    code:

    <table>
    <tr>
    <td>My First Row</td>
    <td>My First Row 2</td>
    <td>My First Row 3</td>
    </tr>

    <tr>
    <td>My Second Row</td>
    <td>My Second Row 2</td>
    <td>My Second Row 3</td>
    </tr>
    </table>



    and continue to do this through every entry in the database table.

    so I have something like this that will display the info:

    code:

    for ($i = 1; $i <= $num; $i++){

    $chairArray = mysql_fetch_array($chairResult);
    $chairName = $chairArray['name'];
    $chairDescription = $chairArray['description'];
    $chairImage = $chairArray['image'];

    echo "<tr>";
    echo "<td>";
    echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />";
    echo "<br />";
    echo $chairName;
    echo "</td>";
    echo "</tr>";


    }

    Thanks & regards
    Lokananth
  2. bmcoll3278 New Member

    I thought I answered this a few days ago.

    ok on your sql query you ad at the end LIMIT 1

    So I of course dont know your database name and so on but it will look a lot like this

    $result = mysql_query("SELECT * FROM dyn_menu where cat='news'LIMIT 1");

    for ($i = 1; $i <= $num; $i++){

    $chairArray = mysql_fetch_array($chairResult);
    $chairName = $chairArray['name'];
    $chairDescription = $chairArray['description'];
    $chairImage = $chairArray['image'];

    echo "<tr>";
    echo "<td>";
    echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />";
    echo "<br />";
    echo $chairName;
    echo "</td>";
    echo "</tr>";


    }

    $result = mysql_query("SELECT * FROM dyn_menu where cat='news'LIMIT 1 OFFSET 1");

    for ($i = 1; $i <= $num; $i++){

    $chairArray = mysql_fetch_array($chairResult);
    $chairName = $chairArray['name'];
    $chairDescription = $chairArray['description'];
    $chairImage = $chairArray['image'];
    echo "<tr>";
    echo "<td>";
    echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />";
    echo "<br />";
    echo $chairName;
    echo "</td>";
    echo "</tr>";


    }

    You repete the query over and over changing the OFFSET to 2,3,4,5 how ever many you need. I use this to put sql data into tables it works fine.