2015-09-06 20 views
2

我工作在一個簡單的項目,基本上我正在做的是,我從管理面板創建一個簡單的頁面,然後顯示在另一個頁面使用iframe ...如何給每個元素他的具體網址 - PHP

因此,在顯示頁面名稱的過程中,我設法顯示數據庫中的所有頁面名稱。但是,當我嘗試給他們一個鏈接,所以當用戶點擊它們時,它會將它們重定向到特定頁面。所以,如果有一個網頁名爲:紐約,他點擊它,它會去:/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> 

如何解決這個有什麼想法?另外,如果有更好的方法來做到這一點,那就太好了。

預先感謝大家:)

+0

[用PHP創建鏈接列表]的可能的副本(http://stackoverflow.com/questions/11300658/making-a-list-of-links-with-php) –

回答

0

首先,將你的循環到你究竟要顯示的數據的頁面。

其次,將代碼放在實際所屬的位置,保持代碼清潔和易讀。即。 <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> 

快樂編碼!

+0

是的,我應該讓我的代碼更清潔:) 感謝您的回答!我不能投票,但它的作品:) –

+0

@JamalJubran如果這個答案對你有幫助和回答你的問題,請不要忘記[接受答案](http://meta.stackexchange.com/questions/ 5234 /如何-不接受-的回答工作)。 另請參閱[回答「接受」時的含義是什麼?](http://meta.stackexchange.com/help/accepted-answer)。 – Kuya

相關問題