我是php的新手。
我在mysql(示例)中有一个表
id id_pro name
1 2 budi
2 1 adi
3 1史蒂夫
4 1 jono
5 2 galang
我想像这样打印
id_pro名称
1 adi,steve,jono
2 budi,galang
我做过,但看起来像这样。 (请帮帮我)
id_pro名称
1 adi
1史蒂夫
1 jono
2 budi
2 galang
<?php
$a = mysql_connect("localhost","root","admin");
mysql_select_db("aa");
$result = mysql_query("select * from member");
echo '<table>
<tr><th>id_pro</th><th>name</th></tr>';
while($data = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td> '. $data["id_pro"] .'</td>';
echo '<td> '. $data["name"] .'</td>';
echo '</tr>';
}
&GT;
答案 0 :(得分:2)
<?php
$result = array();
$data = array(array("id_pro"=>1,"name"=>"adi"),array("id_pro"=>2,"name"=>"budi"),array("id_pro"=>1,"name"=>"steve"));
foreach($data as $value){
$result[$value["id_pro"]][] = $value["name"];
}
foreach($result as $key => $value){
echo $key . "----" . implode("," , $value);
echo "\n";
}
?>
或者您可以像这样更改您的sql:
select id_pro,group_concat(name) from member group by id_pro;