2013-05-08 1 views
0

product_id, product_name, product_image의 3 개 열이있는 데이터베이스가 있습니다. 쿼리를 실행하여 모든 값을 검색 한 다음 해당 데이터의 목록을 만들어야합니다.데이터베이스에서 값을 검색하고 foreach를 사용하십시오.

$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 이미 정의 된 선 장바구니 사이트와 잘 작동합니다 :

product_id | product_name | product_image | 
1   | ball   | ball.jpg  | 
2   | shirt  | shirt.jpg  | 
3   | car   | car.jpg  | 

내가 사용하고 코드입니다.

는 내가 기대하고있어 결과는 다음과 같다 :

<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

를 사용하지 않나요? – brbcoding

+0

phpmyadmin에서 쿼리는 무엇을 보여줍니까? 쿼리가 괜찮습니까? 어쩌면 "제품"은별로 그룹이없는보기입니다. – Robert

+0

$ q1_items [] = $ q1-> fields; - 배열을 반환하고 $ q1_items는 다차원 배열이됩니다 ... 각각에 대해 다차원 배열을 만들어야합니다. – user170851

답변

0

당신은`인 print_r ($의 Q1)`출력을 무엇 값

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