2011-03-12 81 views
1

我有下面的代碼:使一個HTML/PHP鏈接

$result = mysql_query("SELECT link, notes FROM links WHERE username='will';"); 
$html .= "<ul>"; 
while ($row = mysql_fetch_array($result)) { //loop 
    extract($row); 
    $html .= "<li>{$link} - {$notes}</li>"; 
    } 

我需要在那裏說:{$link}位,成爲打開一個新的窗口中點擊的鏈接。我將如何做到這一點?

當我把它周圍的標籤,你得到這個錯誤:

的錯誤是:解析錯誤:語法錯誤,意想不到的/data/www/vhosts/themacsplash.com/httpdocs/ClipBoy/will「{」 .PHP在線18

線18 $html .= "<li>{$link} - {$notes}</li>";

+0

是你寫的嗎,還是你第一次看到它? – 2011-03-12 16:02:10

+0

我寫了代碼.. – 2011-03-12 16:03:08

+0

你有基本的HTML理解嗎?爲什麼你不能在這個頁面上添加適當的標籤到現有的HTML? – 2011-03-12 16:04:07

回答

4

一般來說,你作出這樣的鏈接:<a href="URL">link title</a>。所以你的情況是這樣的:

$html .= "<li><a href=\"{$link}\" target=\"_blank\">{$link}</a> - {$notes}</li>"; 
+2

你沒看到代碼中有什麼不尋常的東西嗎? – 2011-03-12 16:01:29

+0

我得到這個錯誤:解析錯誤:語法錯誤,意想不到的'{'在第18行的/data/www/vhosts/themacsplash.com/httpdocs/ClipBoy/will.php – 2011-03-12 16:02:31

+0

哦,對不起,我沒有逃脫引號。更新了代碼。 (不敢相信我沒注意到......) – poke 2011-03-12 16:05:39

0

如果你的$鏈接包含其形式爲「http://www.example.com/」的網址,使用:

$html .= "<li><a href=\"{$link}\">{$notes}</a></li>"; 
+4

我簡直不敢相信。相同的錯誤 – 2011-03-12 16:04:36

+0

不工作錯誤解析錯誤:語法錯誤,意外的'{'在第18行的/data/www/vhosts/themacsplash.com/httpdocs/ClipBoy/will.php – 2011-03-12 16:05:19

+0

我剛剛通過轉義內部報價。對不起,語法錯誤 – Damien 2015-11-16 14:04:19

0

首先創建一個正確的代碼,使錯誤HANDELING,高於設定變量在引號之外。

$qry = "SELECT link, notes FROM links WHERE username='will'"; 
$mysqlqry = mysql_query($qry); 
if($mysqlqry){ 
    if(mysql_num_rows($mysqlqry) > 0){ 

     $html .= "<ul>"; 
     while($row = mysql_fetch_array($result)) { //loop 
      extract($row); 
      $html .= "<li><a href=". htmlentities($link) .">". $notes ."</a></li>"; 
     } 
    } 
}