在';PHP中的最后一个元素

在';PHP中的最后一个元素,php,while-loop,Php,While Loop,我有一个while循环用于编写列表。我希望每个元素都用逗号分隔,但在最后一个元素后写一个点,而不是逗号。 这是我的密码 $exec = mysql_query($req); while ($row = mysql_fetch_assoc($exec)){ echo '<strong>'.$row['name'].'</strong>, '; } $exec=mysql\u查询($req); while($row=mysql\u fetch\u assoc

我有一个while循环用于编写列表。我希望每个元素都用逗号分隔,但在最后一个元素后写一个点,而不是逗号。 这是我的密码

$exec = mysql_query($req);
    while ($row = mysql_fetch_assoc($exec)){
    echo '<strong>'.$row['name'].'</strong>, ';
}
$exec=mysql\u查询($req);
while($row=mysql\u fetch\u assoc($exec)){
回显“”.$row['name']”。;
}
但我不知道怎么做。我尝试使用count()或end(),但都不起作用。一点帮助就好了

$html=array();
$html = array();

$exec = mysql_query($req);
while ($row = mysql_fetch_assoc($exec)){
    $html[] = '<strong>' . htmlspecialchars($row['name']) . '</strong>';
    // --------------------^^^^^^^^^^^^^^^^! (1)
}

$html = implode(', ', $html) . '.';
$exec=mysql\u查询($req); while($row=mysql\u fetch\u assoc($exec)){ $html[]=''.htmlspecialchars($row['name'])。'; // --------------------^^^^^^^^^^^^^^^^! (1) } $html=内爆(',',$html)。';


(1) -在未正确转义的情况下,切勿将数据输出到HTML。

生成字符串,但不要输出。然后,修剪该命令并添加一个fullstop:

$exec = mysql_query($req);
$output = "";

while ($row = mysql_fetch_assoc($exec))
{
    $output .= '<strong>'.htmlentities($row['name']).'</strong>, ';
}

if($output) echo rtrim($output, ', ').".";
$exec=mysql\u查询($req);
$output=“”;
while($row=mysql\u fetch\u assoc($exec))
{
$output.=''.htmlentities($row['name'])。;
}
如果($output)echo rtrim($output,“,”).“;

我喜欢将内爆函数用于此:)

$list=array();
$exec=mysql\u查询($req);
while($row=mysql\u fetch\u assoc($exec)){
$list[]=“”。$row['name']”。;
}
$string=内爆(“,”,$list);
echo$string。".";
HTH

试试这个:

$exec = mysql_query($req);
$output = array();
while ($row = mysql_fetch_assoc($exec)){
    $output[] = '<strong>'.$row['name'].'</strong>';
}
echo implode(',', $output), '.';
$exec=mysql\u查询($req);
$output=array();
while($row=mysql\u fetch\u assoc($exec)){
$output[]=“”。$row['name']”。;
}
回波内爆(',',$output),';

@MyHead,@Breandan:谢谢,修正了。:)接受答案以获得声誉我做了,我只是在等待,因为我无法在前几分钟内接受;)@杰尼给它一个机会。这个问题只有12分钟了!:)@布伦丹·布伦:我帮助过他。而且,在得到我真正需要的东西后,我迫不及待地想我不得不说,这只是我第二次问这样的问题,但答案总是非常快。太棒了!
$exec = mysql_query($req);
$output = array();
while ($row = mysql_fetch_assoc($exec)){
    $output[] = '<strong>'.$row['name'].'</strong>';
}
echo implode(',', $output), '.';