PHP loop through database and limit number of entries

Discussion in 'PHP and MySQL' started by gilbertsavier, Jul 14, 2009.

  1. gilbertsavier New Member

    Hi,
    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. gregor171 New Member

    Code:
    [COLOR=#000000][COLOR=#0000bb]$result [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]mysql_query[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"SELECT [/COLOR][/COLOR]name[COLOR=#000000][COLOR=#dd0000], [/COLOR][/COLOR]description, image[COLOR=#000000][COLOR=#dd0000] FROM mytable"[/COLOR][COLOR=#007700]);
    [/COLOR][/COLOR][COLOR=#000000][COLOR=#007700]while ([/COLOR][COLOR=#0000bb]$row [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]mysql_fetch_array[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$result[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]MYSQL_NUM[/COLOR][COLOR=#007700])) {
        [/COLOR][COLOR=#0000bb]....[/COLOR][COLOR=#007700]
    }[/COLOR][/COLOR]
    I like this simple sample from:
    http://us.php.net/mysql_fetch_array
  3. bmcoll3278 New Member

    here is the code I use. notice the LIMIT and OFFSET

    so you change the limit to 3 and then do a second query and set the offset to 6 then a 3rd query with the offset set to 9 ect,,

    to see an ex. look at my site the news feed menu at the top is built this way. link in my sig. here is the code

    <?php
    $result = mysql_query("SELECT * FROM dyn_menu where cat='news'LIMIT 8");

    while($row = mysql_fetch_array($result))
    {

    $linky[0] = "$row[links]";
    $linky[1] = "$row[link_url]";



    $thelinks= "<b><a href='$linky[1]'>$linky[0]</a>,</b> ";

    echo"$thelinks";
    }
    echo"<BR>";
    $result = mysql_query("SELECT * FROM dyn_menu where cat='news'LIMIT 8 OFFSET 8");

    while($row = mysql_fetch_array($result))
    {

    $linky[0] = "$row[links]";
    $linky[1] = "$row[link_url]";



    $thelinks= "<b><a href='$linky[1]'>$linky[0]</a>,</b> ";

    echo"$thelinks";
    }
    echo"<BR>";
    ?>