【发布时间】:2013-05-08 19:20:06
【问题描述】:
我有一个包含 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>
基本上,复制每个图像的整行并仅更改图像名称。我做错了什么以及如何获得所需的输出?
【问题讨论】:
-
print_r($q1)输出什么? -
查询在 phpmyadmin 中显示什么?你确定查询没问题?也许“产品”是一个没有分组依据的视图。
-
$q1_items[] = $q1->字段; - 返回数组 & $q1_items 成为多维数组...每个都必须为多维数组制作
标签: php mysql foreach zen-cart