我在做一个简单的项目,基本上我正在做的是从管理面板创建一个简单的页面,然后使用 iframe 在另一个页面中显示它...
因此在显示页面名称的过程中,我设法显示了数据库中的所有页面名称。但是,当我尝试给他们一个链接时,当用户点击它们时,它会将他们重定向到特定页面。因此,如果有一个名为:new york 的页面,他点击它,它会转到:/pages/newYork.php ...
问题:当我给他们一个链接时,所有页面只给出了 1 个页面链接。示例:如果我将城市命名为:纽约、曼谷和阿姆斯特丹,则所有这些页面的链接仅适用于纽约。
这是我的代码:
<?php
require_once '../connect.php';
session_start();
if(isset($_SESSION['loggedinAdmin'])){
}else{
header("Location: index.php");
}
// To display content:
$realDisplay = '';
$sql = mysql_query("SELECT * FROM cities");
while($row = mysql_fetch_assoc($sql)){
$displayName = $row['name'];
$realDisplay .= $displayName.'<br/>';
}
echo "<u align='center'>Page names:";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Display Pages</title>
<style>
a{
text-decoration:none;
font-size:30px;
color:skyblue;
}
</style>
</head>
<body>
<div style="color:red;">
<a href="<?php echo 'pages/'.$displayName.'.php'?>">
<?php echo $realDisplay;?>
</a>
</div>
</body>
</html>
有什么解决办法吗?另外,如果有更好的方法,那就太好了。
提前感谢大家:)
最佳答案
首先,将循环移动到实际要显示数据的页面。
其次,通过将内容放在它们实际所属的位置来保持代码的清洁和可读性。 IE。 <u align='center'>Page names:
不属于你放的地方。它进入体内。
第三,不要忘记正确关闭标签。 <u align='center'>Page names:
没有结束标签。
最后,不要过于复杂化。同一数据行不需要两个不同的变量。
<?php
require_once '../connect.php';
session_start();
if(isset($_SESSION['loggedinAdmin'])){
}else{
header("Location: index.php");
}
// To display content:
//$realDisplay = '';
$sql = mysql_query("SELECT * FROM cities");
//while($row = mysql_fetch_assoc($sql)){
//$displayName = $row['name'];
//$realDisplay .= $displayName.'<br/>';
//}
//echo "<u align='center'>Page names:";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Display Pages</title>
<style>
a{
text-decoration:none;
font-size:30px;
color:skyblue;
}
</style>
</head>
<body>
<div align="center"><u>Page names:</u></div>
<div style="color:red;">
<?php while($row = mysql_fetch_assoc($sql)){
$displayName =$row['name']
?>
<a href="<?php echo 'pages/'.$displayName.'.php'?>"><?php echo $displayName;?></a><br>
<?php } ?>
</div>
</body>
</html>
关于php - 如何给每个元素一个特定的 url - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32419551/