Geting multiple rows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CerIs
Forum Newbie
Posts: 22
Joined: Sat May 29, 2004 9:20 am

Geting multiple rows

Post by CerIs »

Im trying to get multiple rows from mysql and echo them out.

This works but i dont want all of the rows at once

Code: Select all

while($row = mysql_fetch_array($result))

{ 
 extract ($row);
 echo "<td width='500'><img src='images/$Name'></td>";
 
 &#125;
when i try this for loop it doesnt work. It just prints out the first image 3 times?

Code: Select all

for($i = 1; $i <= 3; $i++)

&#123; 
 extract ($row);
  echo "<td width='500'><img src='images/$Name'></td>";
 
 &#125;
Thanks Paul
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

something like this?

Code: Select all

$cols = 3;
$c = 0;
while($row = mysql_fetch_assoc($result))
{
  if($c == 0)
    echo "&lt;tr&gt;";
  echo "&lt;td width="500"&gt;&lt;img src="images/{$row&#1111;'Name']}"&gt;&lt;/td&gt;";
  if($c == $cols - 1)
    echo "&lt;/tr&gt;";
  $c++;
  $c %= $cols;
}
CerIs
Forum Newbie
Posts: 22
Joined: Sat May 29, 2004 9:20 am

Post by CerIs »

Yeah thanks:) it still prints out all of the records though?

whats this bit doin if($c == $cols - 1) ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it checks to see if it's the last one for that table row..
CerIs
Forum Newbie
Posts: 22
Joined: Sat May 29, 2004 9:20 am

Post by CerIs »

is there a way to do it without using

while($row = mysql_fetch_assoc($result))

beause i only want to get 3 rows from the database.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

just add 'LIMIT 3' to your query
CerIs
Forum Newbie
Posts: 22
Joined: Sat May 29, 2004 9:20 am

Post by CerIs »

Hehe :) it works, thanks for the help.
Post Reply