[英]Create comma separated list of links with variable in PHP
I have a variable which renders a list of usernames. 我有一个变量,它呈现用户名列表。 Now, I need to add a link to each username. 现在,我需要为每个用户名添加一个链接。 The link is built from a fixed URL and the username as a variable. 链接是通过固定的URL和用户名作为变量构建的。
Example link: https://www.example.org/something?userid= $username 示例链接: https : //www.example.org/something?userid= $ username
$usernames = $_POST['username'];
$username = '';
foreach($usernames as $value) {
$username .= "'" . $value . "', ";
}
$username = rtrim($username, ', ');
And then, I have the construction of the list: 然后,我构建了清单:
<?php $urls = explode(',', $usernameid);
$nritems = count ($urls);
$positem = 0;
foreach($urls as $key => $usernameid)
{
echo "<a href="https://www.example.org/something?userid=' . $usernameid .
'">'. $usernameid . '</a>";
if (++$positem != $nritems) { echo ", ";}
}?>
The variable is also used in other parts of the code, so I can't change it. 该变量还在代码的其他部分中使用,因此无法更改。 The list is not displaying now. 该列表现在未显示。
Any help is appreciated. 任何帮助表示赞赏。
Update: the form: 更新:表格:
<form action="list.php" method="post" autocomplete="off">
<div id="usrselect">
<label>Select user <input type="text" name="username[]" id="userid" class="typeahead" required/></label>
</div>
<div class="button-section"> <input type="submit" name="List" /></div>
</form>
If you just want to get your quotes right: 如果您只想正确引用:
echo '<a href="https://www.example.org/something?userid='.$usernameid.'">'.$usernameid.'</a>';
or 要么
echo "<a href=\"https://www.example.org/something?userid={$usernameid}\">{$usernameid}</a>";
or 要么
echo sprintf('<a href="https://www.example.org/something?userid=%d">%d</a>', $usernameid, $usernameid);
The working solution, for those who might face the same problem: 对于可能面临相同问题的人,有效的解决方案:
<?php $urls = explode(',', $usernameid);
$nritems = count ($urls);
$positem = 0;
$displayuser = array("'", " ");
foreach($urls as $key => $usernameid)
{
echo '<a href="http://www.example.com/something?userId='.str_replace($displayuser, "", $usernameid).'">'.str_replace($displayuser, "", $usernameid).'</a>';
if (++$positem != $nritems) { echo ", ";}
}?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.