duanditang2916 2013-05-08 19:20
浏览 82
已采纳

从数据库中检索值并使用foreach

I have a database with 3 columns: product_id, product_name, product_image. I need to run a query to retrieve all values and then create a list of that data.

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

This is the code I'm using:

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

This is a Zen Cart site so $db->Execute is already defined and works just fine.

The output I'm expecting is like 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 the entire row for each image and changes only image name. What am I doing wrong and how do I get the output I need?

  • 写回答

1条回答 默认 最新

  • dqxuiq7772 2013-05-08 20:13
    关注

    Shouldn't you be using the value

    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>
    ';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试