我有一个名为links的字段,它接受以逗号分隔的网址列表,例如'www.bbc.co.uk,www.itv.co.uk'
当我输出字段的内容时,我将其包装在如下代码中:
<li><a href='http://<?php echo htmlout($name['links']); ?>'><?php echo htmlout($name['links']); ?></a></li>
添加单个链接时可以正常工作,但如果有多个链接,则会导致问题。
例如,如果我有两个链接,一个是www.bbc.co.uk,一个是www.itv.co.uk,然后我将鼠标悬停在链接上(它是一个而不是两个链接),它显示为:
http://www.ts.co.uk%2C%20www.bbc.co.uk/
现在我认为因为它们被逗号分开,所以可能有一些方法可以将它用作分割URL的钩子,但我不确定这是否可行。
感谢任何帮助。感谢
以下编辑
下面添加了一些代码,因为我已经有了一个数组。我可以在数组中有一个数组吗?我应该在这种情况下吗?我应该在哪里删除建议的代码?
INDEX.PHP(控制器)
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$sql = "SELECT player.id, player.name AS name, age, position, height, weight, GROUP_CONCAT(distinct previousclubs.name) previousclubs,
satscore, gpa, GROUP_CONCAT(distinct link) link, email
FROM player INNER JOIN playerpreviousclubs
ON player.id = playerid
INNER JOIN previousclubs
ON previousclubid = previousclubs.id
INNER JOIN links
ON links.playerid = player.id
WHERE email = '$username'";
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching details: ' . $e->getMessage();
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$names[] = array(
'id' => $row['id'],
'name' => $row['name'],
'age' => $row['age'],
'position' => $row['position'],
'height' => $row['height'],
'weight' => $row['weight'],
'previousclubs' => $row['previousclubs'],
'satscore' => $row['satscore'],
'gpa' => $row['gpa'],
'links' => $row['link'],
'email' => $row['email']
);
}
include 'profiles.html.php';
我的模板目前看起来像这样:
PROFILES.HTML.PHP
<p>Welcome <?php echo $row['name'] ?> . Below are your profile details:</p>
<?php foreach ($names as $name): ?>
<form action="" method="post">
<ol>
<li class="listleft">Name:</li>
<li><?php echo htmlout($name['name']); ?></li>
<li class="listleft">Age:</li>
<li><?php echo htmlout($name['age']); ?></li>
<li class="listleft">Position:</li>
<li><?php echo htmlout($name['position']); ?></li>
<li class="listleft">Height:</li>
<li><?php echo htmlout($name['height']); ?></li>
<li class="listleft">Weight:</li>
<li><?php echo htmlout($name['weight']); ?></li>
<li class="listleft">Previous Clubs:</li>
<li><?php echo htmlout($name['previousclubs']); ?></li>
<li class="listleft">GPA:</li>
<li><?php echo htmlout($name['satscore']); ?></li>
<li class="listleft">SAT Score:</li>
<li><?php echo htmlout($name['gpa']); ?></li>
<li class="listleft">Links:</li>
<li><a href='http://<?php echo htmlout($name['links']); ?>'><?php echo htmlout($name['links']); ?></a></li>
</ol>
</form>
<?php endforeach; ?>
答案 0 :(得分:2)
$links = explode(',', $links);
foreach($links as $link) {
echo '<li><a href="'.$link.'">'.$link.'</a></li>';
}
答案 1 :(得分:2)
你需要explode,然后循环播放它们。
$links = explode(',', $name['links']);
然后在你看来:
foreach ($links as $link) {
echo '<li><a href="http://'.$link.'">'.$link.'</a></li>';
}
答案 2 :(得分:1)
您可以使用explode
函数轻松拆分字符串:
// split string by comma, returns array
$linksArray = explode(',', $links);
foreach($linksArray as $link) {
echo $link; // print each link etc
}
答案 3 :(得分:0)
你最好将explode字符串放入数组中,然后循环数组以输出数据。就个人而言,我发现数组比字符串更容易使用。例如:
$links = explode(',', $links);
foreach($links as $link) {
?>
<li><a href="http://<?php echo htmlout($link); ?>"><?php echo htmlout($link); ?></a></li>
<?php
}