Page 1 of 1

displaying images from directory

Posted: Fri Nov 17, 2006 6:18 pm
by dbones
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to display 5 images from a directory alongside data Im retrieving from the database. How do I display the images in order without making the data from the database loop?

Code: Select all

$query='SELECT * FROM items ORDER BY id';
	if ($r=mysql_query($query)){
	while($row = mysql_fetch_array($r)){
	for($n=1; $n<5; $n++){
	print "	<p><image src = 'http://diabri.100webspace.net/images/book$n.jpg'> </p>
	<p><b>Title: {$row['title']}</b>
	<br>Author: {$row['author']}
	<br>Price: {$row['price']}</p>";
	}
	}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Nov 17, 2006 6:24 pm
by volka
How is $n in book$n.jpg related to the each recordset?

Posted: Fri Nov 17, 2006 6:28 pm
by dbones
I created book1.jpg, book2.jpg, book3.jpg, book4.jpg, book5.jpg in my images directory and I wanted the images to show in that order but the data from database is looping 5 times.

Posted: Fri Nov 17, 2006 6:31 pm
by evilchris2003
wont that sytax error surely to use the variable you will need this

Code: Select all

print " <p><image src = 'http://diabri.100webspace.net/images/book", $n ".jpg'> </p>";

Posted: Fri Nov 17, 2006 6:34 pm
by volka
ah ok, no association beside the position of the record, #1, #2, ...

try

Code: Select all

$query='SELECT
		title, author, price
	FROM
		items
	ORDER BY
		id
	LIMIT
		5';
$result=mysql_query($query) or die(mysql_error());

$n = 0;
while( false!==($row=mysql_fetch_array($r)) ) {
	$n += 1;
	echo "
		<p><image src = 'http://diabri.100webspace.net/images/book{$n}.jpg' /></p>
		<p>
			<b>Title: {$row['title']}</b>
			<br />
			Author: {$row['author']}
			<br />
			Price: {$row['price']}
		</p>";
}

Posted: Fri Nov 17, 2006 6:58 pm
by dbones
Volka, it worked. Thanks so much!!