[英]Pass value from href link to detail page
我有一張顯示所有用戶的表格。 我想在用戶名周圍放置一個鏈接,這樣當您單擊用戶名時,它會將您帶到他們的個人資料頁面。
如何將用戶名放入 href 鏈接並將其傳遞到詳細信息頁面?
這就是我現在所擁有的:
if ($allPlayers > 0 ) {
while ($allPlayersItem = mysqli_fetch_assoc($playersQuery)) {
echo "<a href='detailpage.php'>".$allPlayersItem['gamertag']."</a>";
echo "<br>";
}
}
您可以將玩家的gamertag
添加到查詢參數中,例如:
echo "<a href='detailpage.php?player={$allPlayersItem['gamertag']}'>".$allPlayersItem['gamertag']."</a>";
然后在detailpage.php
中可以寫:
$player = $_GET['player'] ?? '';
if ($player != '') {
// display player details...
}
請注意,如果gamertag
值可能包含特殊字符,那么您應該在將其用作參數之前對該值進行urlencode
,即
echo "<a href='detailpage.php?player=" . urlencode($allPlayersItem['gamertag']) . "'>".$allPlayersItem['gamertag']."</a>";
首先,我建議您分開從數據庫獲取數據和渲染數據的責任。 最常見的方法可能是遵循 MVC 模式。
就像是:
# Controller/PlayerController
final class PlayerController
{
public function index(): Response
{
$players = [/* list of players to display. From the DB, for example*/];
return $this->render('player/index.[blade|twig|php]', $players);
}
}
# Views/player/index.php
<?php foreach ($players as $player): ?>
<a href='detailpage.php'>" . <?= $player['gamertag']?> . "</a><br>"
<?php endforeach ?>
我建議您使用google研究和了解有關此主題的更多信息,例如。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.