2013-05-08 74 views
0

我有一個包含3列的數據庫:product_id,product_name,product_image。我需要運行查詢來檢索所有值,然後創建該數據的列表。從數據庫中檢索值並使用foreach

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'; 
} 

這讓$db->Execute已經定義的Zen Cart的網站,工作得很好。

我期待的輸出是這樣的:

<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

什麼'的print_r($ Q1)'輸出? – brbcoding 2013-05-08 19:27:49

+0

查詢在phpmyadmin中顯示的內容是什麼?你確定查詢沒問題嗎?也許「產品」是一個沒有羣體的觀點。 – Robert 2013-05-08 19:32:42

+0

$ q1_items [] = $ q1-> fields; - 返回數組&$ q1_items變爲多維數組...對於每個數組都必須進行多維數組 – user170851 2013-05-08 20:42:35

回答

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'; 
}