Go Back   Free website templates > Web Template Help > PHP and MySQL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Join now to download all the free website templates or post on the forum. If you have never been on a forum before read the FAQ. It's quick, easy and free to join!
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-14-2009, 10:23 AM
Junior Member
 
Join Date: Jun 2009
Posts: 25
gilbertsavier is on a distinguished road
Default PHP loop through database and limit number of entries

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
Reply With Quote
  #2 (permalink)  
Old 07-28-2009, 01:51 AM
Junior Member
 
Join Date: Jul 2009
Posts: 1
gregor171 is on a distinguished road
Default

Code:
$result = mysql_query("SELECT name, description, image FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    ....
}
I like this simple sample from:
http://us.php.net/mysql_fetch_array

Last edited by CovertPea; 07-28-2009 at 03:24 AM.
Reply With Quote
  #3 (permalink)  
Old 08-01-2009, 05:37 PM
bmcoll3278's Avatar
Moderator
 
Join Date: Jun 2009
Posts: 294
bmcoll3278 is on a distinguished road
Default

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>";
?>
__________________
I hope to build a site with something for every body
www.bmcoll.com
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:24 AM.



mouseover mouseover mouseover