displaying sql returns

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
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

displaying sql returns

Post by hward »

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Code: Select all

$sql = "SELECT image
	FROM $table_name 
	 	 ORDER BY image ";
	
$result = @mysql_query($sql,$connection)
	or die("Couldn't execute query.");
 
while ($row = mysql_fetch_array($result)) {
	$image_name = $row['image'];
	
	$display_block .= "
	<img border='1' src='$image_name' width='135' height='100'>
	<p>
	";


}
this is opens my database to get the image names and then the display block displays it when i echo it in the html but i dont want to returns all the images in a single collum going down i would like to do the return with a row of 4 and then the next 4 below that and so on.


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Do something like:

Code: Select all

$idx = 0;
while ($row=mysql_fetch_array($result)) {
  if (($idx%4)==0) $display_block .= "<br />";
  $display_block .= "<img border='1' src='$image_name' width='135' height='100'>";
  $idx++;
}
For information about the modulus operator: http://us2.php.net/operators.arithmetic
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

that displays the first picture for all of them so where i have 50 pictures to display it give me 50 of the first one
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

hward wrote:that displays the first picture for all of them so where i have 50 pictures to display it give me 50 of the first one
Your missing the point, the point is you would do something like

loop
{
if x dividable by 4, add a column, if it is, start a new row

x+1

}
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


works now    thanks

Code: Select all

$idx = 0;
while ($row=mysql_fetch_array($result)) {  
if (($idx%4)==0) echo "<br />";  
echo "<img border='1' src='$row[0]' width='135' height='100'>";  $idx++;}

Code: Select all

src='$row[0]'

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

:P Excellent. Glad we could help.
Post Reply