我有一个包含3列的数据库:product_id,product_name,product_image。我需要运行查询来检索所有值,然后创建该数据的列表。
product_id | product_name | product_image |
1 | ball | ball.jpg |
2 | shirt | shirt.jpg |
3 | car | car.jpg |
这是我正在使用的代码:
$q1 = $db->Execute("select * from products");
$q1_items = array();
while(!$q1->EOF){
$q1_items[] = $q1->fields;
$q1->MoveNext();
}
foreach ($q1_items as $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';
}
这是一个Zen Cart网站,因此$db->Execute
已经定义并且工作得很好。
我期待的输出是这样的:
<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>
然而,我得到了这个:
<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>
基本上,复制每个图像的整行,并仅更改图像名称。我做错了什么,如何获得我需要的输出?
答案 0 :(得分:0)
你不应该使用值
foreach ($q1_items as $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';
}