php mysql一对多查询,显示具有一对多关系的php mysql查询结果

Heres A Sample Table:

info table

info_id name

1 john

2 peter

------------------------

details table

details_id log date

1 test log for john 2013-08-01

1 another log for john 2013-08-02

2 test log for peter 2013-08-02

Here's my Sample Query:

SELECT info.info_id, info.name, details.details_no, details.log, details.date

FROM info JOIN details ON details.details_id = info.info_id

GROUP BY info.info_id

And Here's display i want to achieve:

john

1 test log for john 2013-08-01

1 another test log for john 2013-08-02

peter

2 test log for peter 213-08-02

I have tried using foreach loop and then execute another foreach loop inside the first loop.

Please Help Guys

解决方案

You are probably going to have to take the data and make the array that you want.

$data = array();

foreach ($result as $item) {

$key = $item['name']; // or $item['info_id']

if (!isset($data[$key])) {

$data[$key] = array();

}

$data[$key][] = $item;

}

// Build your table with the new $data array

EDIT

This is just an example. As amaster507 points out, if your name field isn't unique, you will need to build your array on a unique key. Not terribly different from this, as you could probably just change instances of $item['name'] to $item['info_id'].

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值