Retrieve data insert database!!

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
ipohismytown
Forum Newbie
Posts: 4
Joined: Tue Feb 09, 2010 10:02 am

Retrieve data insert database!!

Post by ipohismytown »

i want to retrieve the information from database to display a product pictures.

Database Information

Code: Select all

 
Table name : product
Column name : prod_img_url 
Information inside the column : pic1.jpg,pic2.jpg,pic3.jpg
 
Image display ( i only want display 1 result for "pic1.jpg")

Code: Select all

<img src="/localhost/images/<?php echo $row['prod_img_url']"?>

How to code for only take the first picture address (pic1.jpg) and ignored other?Please help Urgent. Thx
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Retrieve data insert database!!

Post by alex.barylski »

Code: Select all

 
$products = array_map('trim', explode(',', $row['prod_img_url']));
 
echo '<img src="'.$products[0].'" />';
The idea being you take the CSV list of products and 'split' them into individual names on the comma. array_map() is basically saying iterate over the list and strip any pre or post whitespace, which might be introduced, deliberately or accidently.

Code: Select all

product1.jpg,product2.gif, product3.bmp
The last item is separated by a comma but has whitespace which might cause issues somewhere down the line, so it's best to just strip extraneous whitespace.

Cheers,
Alex
Post Reply