我目前正在編寫一個代碼,該代碼從數據庫中檢索名字和姓氏,并使用錨鏈接添加到其他頁面的鏈接。這是代碼:<?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 關注
- 109 瀏覽
添加回答
舉報
0/150
提交
取消