我ali,aliiii,asd
通过回显得到结果$row['GROUP_CONCAT(mem_name)'];
。
我怎样才能像列表一样回显它们,以便我可以在点击功能上添加锚标记等。我有查询
$sql=mysql_query("
SELECT cat_id, GROUP_CONCAT(mem_name)
FROM o_newcatmem GROUP BY cat_id
");
我希望结果 b 显示如下:
ali
aliiii
asd
你有一个$string
这样的
$string = "ali,aliiii,asd";
echo "<pre>";
你有三个选择:
您可以使用str_getcsv
将 CSV 字符串转换为数组
$list = str_getcsv($string);
foreach ( $list as $var ) {
echo $var, "\n";
}
使用它们将它们转换为数组explode
$list = explode(",", $string);
echo implode("\n", $list);
使用只是使用str_replace
$list = str_replace(",", "\n", $string);
echo $list;
MySQL GROUP_CONCAT function has a SEPARATOR keywords.
GROUP_CONCAT(mem_name SEPARATOR ',') AS mems_name
in your SQL statement.mem_name
field.AS mems_name
, so your PHP code looks simple, like $row['mems_name']
.In your PHP just explode data by your separator $list = explode('|', $row['mems_name']);
.
<?php
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>