displaying images from directory

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
dbones
Forum Newbie
Posts: 3
Joined: Fri Nov 17, 2006 6:13 pm

displaying images from directory

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

How is $n in book$n.jpg related to the each recordset?
dbones
Forum Newbie
Posts: 3
Joined: Fri Nov 17, 2006 6:13 pm

Post 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.
User avatar
evilchris2003
Forum Contributor
Posts: 106
Joined: Sun Nov 12, 2006 6:43 am
Location: Derby, UK

Post 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>";
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>";
}
dbones
Forum Newbie
Posts: 3
Joined: Fri Nov 17, 2006 6:13 pm

Post by dbones »

Volka, it worked. Thanks so much!!
Post Reply