逗号在php中分隔列表

时间:2011-01-13 17:07:03

标签: php mysql drupal

我正在尝试构建一个用逗号分隔的列表,它应该看起来像这样(绿色,橙色,红色)。

$i=0;
$taxonomy = $form_state[values][taxonomy][5];
foreach ($taxonomy as $key => $value){
  $result = db_query("SQL CODE goes here");
  if (mysql_num_rows($result)){     
  while ($i<mysql_num_rows($result)){
    $resultset = db_fetch_array($result);
    $comma_separated = implode(",", $resultset);
    $i++;      
  }
  form_set_error("Date", t("$comma_separated. cannot be booked more than once "));
}

3 个答案:

答案 0 :(得分:4)

$resultset=array();
while ($data = db_fetch_array($result)) {
    $resultset[] = $data;
}
$comma_separated = implode(",", $resultset);

答案 1 :(得分:2)

有人发布了它,我打算投票,但他们删除了它。我认为mysql GROUP_CONCAT将是一个很好的解决方案,因为它看起来像获取逗号分隔列表是此查询的唯一目的。

答案 2 :(得分:0)

试试这个:

$i=0; 
$taxonomy = $form_state[values][taxonomy][5]; 
$result='';
foreach ($taxonomy as $key => $value)
{ 
    $result = db_query("SQL CODE goes here"); 
    if (mysql_num_rows($result)){
        while ($row = mysql_fetch_row()){
           result.=$row[0].',';
        } 
    }
}
result=substr($result,0,-1);
echo $result;
相关问题