我目前正在编写一个代码,该代码从数据库中检索名字和姓氏,并使用锚链接添加到其他页面的链接。这是代码:<?php$get_names_query_result = $get_names_query->get_result();if (!$get_names_query_result) { xecho("Names query failed."); } else { while ($name = $get_names_query_result->fetch_assoc()) {?> <a href="personDetails.php?id=<?php xecho($name['pID']) ?>"><?php xecho($name['first_name']) ?> <?php xecho($name['last_name']) ?></a>,<?php } // while fetch_assoc()} // if $get_names_query_result此代码在网页上显示:代码逗号我想消除最后一个元素末尾的尾随逗号,但我非常不知道如何仅针对显示的名称执行此操作。任何帮助将非常感激!
1 回答

Helenr
TA贡献1780条经验 获得超4个赞
我使用的一种方法是创建一个变量,例如 $comma,并将其初始化为“”。然后在第一次使用后,将其更改为“,”,这不会给您的输出添加任何内容。所以...
<?php
$comma = "";
$get_names_query_result = $get_names_query->get_result();
if (!$get_names_query_result) {
xecho("Names query failed.");
} else {
while ($name = $get_names_query_result->fetch_assoc()) {
?>
<?php echo $comma; ?> <a href="personDetails.php?id=<?php xecho($name['pID']) ?>"><?php xecho($name['first_name']) ?> <?php xecho($name['last_name']) ?></a>
<?php
$comma = ","
} // while fetch_assoc()
} // if $get_names_query_result
请注意,逗号现在添加在锚点之前,而不是像您那样添加在锚点之后。
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报
0/150
提交
取消