简体   繁体   English

将值从 href 链接传递到详细信息页面

[英]Pass value from href link to detail page

I have a table that shows all the users.我有一张显示所有用户的表格。 I'd like to put a link around the usernames so when you click on a username it takes you to their profile page.我想在用户名周围放置一个链接,这样当您单击用户名时,它会将您带到他们的个人资料页面。

How do I put the username into a href link and pass it to the detail page?如何将用户名放入 href 链接并将其传递到详细信息页面?

This is all I have at the minute:这就是我现在所拥有的:

if ($allPlayers > 0 ) {
    while ($allPlayersItem = mysqli_fetch_assoc($playersQuery)) {
        echo "<a href='detailpage.php'>".$allPlayersItem['gamertag']."</a>";
        echo "<br>";
    }
}

You can add the player's gamertag to a query parameter for example:您可以将玩家的gamertag添加到查询参数中,例如:

echo "<a href='detailpage.php?player={$allPlayersItem['gamertag']}'>".$allPlayersItem['gamertag']."</a>";

Then in detailpage.php you can write:然后在detailpage.php中可以写:

$player = $_GET['player'] ?? '';
if ($player != '') {
     // display player details...
}

Note that if the gamertag value may contain special characters then you should urlencode the value before using it as a parameter ie请注意,如果gamertag值可能包含特殊字符,那么您应该在将其用作参数之前对该值进行urlencode ,即

echo "<a href='detailpage.php?player=" . urlencode($allPlayersItem['gamertag']) . "'>".$allPlayersItem['gamertag']."</a>";

First of all, I suggest you to split the responsability of getting the data from the DB and the rendering the data.首先,我建议您分开从数据库获取数据和渲染数据的责任。 The most common approach for that it could be following the MVC pattern.最常见的方法可能是遵循 MVC 模式。

Something like:就像是:

# 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 ?>

I recommend you to research and learn more about this topic using google , for exmaple.我建议您使用google研究和了解有关此主题的更多信息,例如。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM