Page 1 of 1

Retrieve data insert database!!

Posted: Tue Feb 23, 2010 5:37 am
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

Re: Retrieve data insert database!!

Posted: Tue Feb 23, 2010 7:33 am
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