1

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
4

3 回答 3

2

你有一个$string这样的

$string = "ali,aliiii,asd";
echo "<pre>";

你有三个选择:

  1. 您可以使用str_getcsv将 CSV 字符串转换为数组

    $list = str_getcsv($string);
    foreach ( $list as $var ) {
        echo $var, "\n";
    }
    
  2. 使用它们将它们转换为数组explode

    $list = explode(",", $string);
    echo implode("\n", $list);
    
  3. 使用只是使用str_replace

    $list = str_replace(",", "\n", $string);
    echo $list;
    
于 2012-10-06T12:39:39.853 回答
1

MySQL GROUP_CONCAT function has a SEPARATOR keywords.

  • You can use GROUP_CONCAT(mem_name SEPARATOR ',') AS mems_name in your SQL statement.
  • Use separator that does not exist in mem_name field.
  • Use alias 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']);.

于 2012-10-06T12:46:13.320 回答
0
<?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; // *

?>
于 2012-10-06T12:43:28.777 回答