php - retrieve values from database and use foreach -
i have database 3 columns: product_id, product_name, product_image. need run query retrieve values , create list of data.
product_id | product_name | product_image | 1 | ball | ball.jpg | 2 | shirt | shirt.jpg | 3 | car | car.jpg |
this code i'm using:
$q1 = $db->execute("select * products"); $q1_items = array(); while(!$q1->eof){ $q1_items[] = $q1->fields; $q1->movenext(); } foreach ($q1_items $items) { echo '<a href="index.php?main_page=product_info&products_id='. $items['products_id'] .'"><img src="images/'. $items['products_image'].'" alt="'. $items['products_name'].'" title="'. $items['products_name'].'" /></a>\n'; }
this zen cart site $db->execute
defined , works fine.
the output i'm expecting this:
<a href="index.php?main_page=product_info&products_id=1"><img src="ball.jpg" alt="ball" title="ball" /></a> <a href="index.php?main_page=product_info&products_id=2"><img src="shirt.jpg" alt="shirt" title="shirt" /></a> <a href="index.php?main_page=product_info&products_id=3"><img src="car.jpg" alt="car" title="car" /></a>
however, i'm getting this:
<a href="index.php?main_page=product_info&products_id=1"><img src="ball.jpg" alt="ball" title="ball" /></a> <a href="index.php?main_page=product_info&products_id=1"><img src="shirt.jpg" alt="ball" title="ball" /></a> <a href="index.php?main_page=product_info&products_id=1"><img src="car.jpg" alt="ball" title="ball" /></a> <a href="index.php?main_page=product_info&products_id=2"><img src="ball.jpg" alt="shirt" title="shirt" /></a> <a href="index.php?main_page=product_info&products_id=2"><img src="shirt.jpg" alt="shirt" title="shirt" /></a> <a href="index.php?main_page=product_info&products_id=2"><img src="car.jpg" alt="shirt" title="shirt" /></a> <a href="index.php?main_page=product_info&products_id=3"><img src="ball.jpg" alt="car" title="car" /></a> <a href="index.php?main_page=product_info&products_id=3"><img src="shirt.jpg" alt="car" title="car" /></a> <a href="index.php?main_page=product_info&products_id=3"><img src="car.jpg" alt="car" title="car" /></a>
basically, duplicates entire row each image , changes image name. doing wrong , how output need?
shouldn't using value
foreach ($q1_items $item => $items) { echo '<a href="index.php?main_page=product_info&products_id='. $items['products_id'] .'"><img src="images/'. $items['products_image'].'" alt="'. $items['products_name'].'" title="'. $items['products_name'].'" /></a>\n'; }
Comments
Post a Comment