1

我有这个代码:

<?
$kate=mysql_query("select category_id from movies_cat where movie_id='$ids'");
   while($cat=mysql_fetch_array($kate)){
      $kate2=mysql_query("select name_cat from categories where id='$cat[category_id]'");
      $html = array();
        while($cat2=mysql_fetch_array($kate2)){
            $cat2['name_cat'].",";
    }
   }?>

我想输出没有最后一个逗号的类别!谢谢 !

4

3 回答 3

1

试试这个,在你的while循环之后,使用这样的phptrim函数:

while($cat2=mysql_fetch_array($kate2)){
            $cat2['name_cat'].",";
}
$cat2['name_cat'] = trim($cat2['name_cat'],",")

它将,从字符串的末尾删除

于 2012-10-30T17:23:10.543 回答
0

尝试这个:

$display = "";
while($cat2=mysql_fetch_array($kate2)) {
    if ($display == "") {
        $display = $cat2['name_cat'];
    } else {
        $display .= ", " . $cat2['name_cat'];
    }
}

echo $display;
于 2012-10-31T10:30:21.077 回答
0
<?
$kate=mysql_query("select category_id from movies_cat where movie_id='$ids'");
while($cat=mysql_fetch_array($kate))
{
    $kate2=mysql_query("select name_cat from categories where id='$cat[category_id]'");
    $html = array();
    $first = true;
    while($cat2=mysql_fetch_array($kate2))
    {
        if ($first)
            $first = false;
        else
            print ",";

        print $cat2['name_cat'];
    }
}?>
于 2012-10-30T17:17:24.640 回答