comma separated list in php

2019-03-07 06:53发布

I am trying to build a list separated by commas which should look like this (green, orange, red).

$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 "));
}

标签: php mysql drupal
3条回答
萌系小妹纸
2楼-- · 2019-03-07 07:09

Someone posted it and I was going to upvote, but they removed it. I think mysql GROUP_CONCAT would be a good solution, since it looks like getting a comma separated list is the only purpose of this query.

查看更多
我命由我不由天
3楼-- · 2019-03-07 07:11

Try this:

$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;
查看更多
forever°为你锁心
4楼-- · 2019-03-07 07:19
$resultset=array();
while ($data = db_fetch_array($result)) {
    $resultset[] = $data;
}
$comma_separated = implode(",", $resultset);
查看更多
登录 后发表回答