2013-02-13 129 views
0

我试图让它插入到我的sql数据库的'标记'列中的任何文本URL都将变成php表中的可点击链接。现在,我不得不手动将链接HTML放到数据库中,我不想这样做。从SQL将url转换为可点击的链接

这是我已经得到了代码:

<?php 
    mysql_connect(localhost, user, pw) or die(mysql_error()); 
    mysql_select_db(database) or die(mysql_error()); 
?> 

<table id=table class=display> 
<thead><tr><th>Owner</th><th>Soquili's Name</th><th>Sex</th><th>Generation</th><th>Parents</th><th>Kind</th><th>Theme</th><th>Colorist</th><th>Tag</th><th>Alt 1</th><th>Alt 2</th><th>Alt 3</th></tr></thead> 
<tbody> 
<?php 

    // Result Limit 
    $sql = "SELECT * FROM soquili limit 25"; 
    $result = mysql_query($sql)or die(mysql_error()); 


    while($row = mysql_fetch_array($result)){ 
    //Reference 


    $name  = $row['owner']; 
    $soq = $row['soq']; 
    $sex = $row['sex']; 
    $gen  = $row['gen']; 
    $parents = $row['parents']; 
    $kind = $row['kind']; 
    $theme = $row['theme']; 
    $colorist = $row['colorist']; 
    $tag = $row['tag']; 
    $alt1 = $row['alt1']; 
    $alt2 = $row['alt2']; 
    $alt3 = $row['alt3']; 

// looped row 
echo 
"<tr><td>".$name."</td> 
<td>".$soq."</td><td>".$sex."</td> 
<td>".$gen."</td> 
<td>".$parents."</td> 
<td>".$kind."</td> 
<td>".$theme."</td> 
<td>".$colorist."</td> 
<td><a href=\".$tag.\">Tag</a></td> 
<td>".$alt1."</td> 
<td>".$alt2."</td> 
<td>".$alt3."</td> 
</tr>"; 

} 
?> 
</tbody></table> 

我不明白为什么它不工作。

这是我的网站: http://rpwith.us/Test2/

而且我有一个测试项(搜索Weeese)与根本不会变成可点击的文本URL。有什么建议么?

+2

[**请不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 – 2013-02-13 01:45:10

+1

您知道...如果您使用双引号封装echo-ed位,则不需要连接变量。即:你可以这样写:echo“你好,很高兴认识你$ name”; – Dacto 2013-02-13 01:49:28

+0

上面的代码与您网站上的输出不匹配。 – mkaatman 2013-02-13 01:56:39

回答

0

带有a标记的行上的引号是错误的。

正确:

<td><a href=\"".$tag."\">Tag</a></td> 

说明:

\"用于插入文字报价,但你想有另一句名言来结束字符串字面量,从而使. $tag .被解析为PHP代码。

您做到这一点的方式是“。$标记”。部分是字符串文字的一部分;但是,当您使用双引号时,$tag会被PHP自动替换,,但点仍然是。结果:输出中有一对额外的点。

+0

谢谢,这个解释帮助了很多!我已经将它编辑到上面的代码中,但由于某种原因我仍然看到文本链接。 – Nikolai 2013-02-13 19:21:44