将标记添加到数据库值

时间:2014-12-28 12:02:09

标签: php mysql pdo

我从数据库中获取了一些值,我想将其拆分并在其中添加html标记。

$sql = "SELECT
            ip_adres,
        GROUP_CONCAT(DISTINCT nickname2 ORDER BY nickname2) nickname
        FROM
            users
        GROUP BY ip_adres
        HAVING COUNT(*) > 1";
$users = $db->prepare($sql);
$users->execute();

foreach($users as $u){
    $nick = $u['nickname'];
    $ip = $u['ip_adres'];
    echo "$nick $ip";
}

$nick输出就像value1,value2现在我想要添加到两个值a href,所以输出必须像 <a href=test.php?url=value>value1</a>, <a href=test.php?url=value2>value2</a>所以每个昵称都是可点击的,并且ref到昵称页面我该怎么做?

2 个答案:

答案 0 :(得分:1)

尝试

<?php

if($s = $db->query('SELECT
            ip_adres,
        GROUP_CONCAT(DISTINCT nickname2 ORDER BY nickname2) namen
        FROM
                users
        GROUP BY ip_adres
        HAVING COUNT(*) > 1')) {
    while ($row = $s->fetch(PDO::FETCH_ASSOC)) { 
        $c = '';
        $nicks = explode(',', $row['namen']);
        foreach($nicks as $nick) {
            echo "
            $c <a href=$nick>$nick</a> 
            ";
            $c = ', ';
        }
        echo ' IP-Adres (' . $row['ip_adres'] . ')<br>';
    }
}

?>

答案 1 :(得分:0)

您可以使用:

foreach($users as $u){
    $nick = $u['nickname'];
    $ip = $u['ip_adres'];
    echo "<a href='test.php?url=".$nick."'>value1</a>";
    echo "<a href='test.php?url=".$ip."'>value2</a>";
}

或者,这也适用:

foreach($users as $u){
    $nick = $u['nickname'];
    $ip = $u['ip_adres'];
    echo "<a href='test.php?url=$nick'>value1</a>";
    echo "<a href='test.php?url=$ip'>value2</a>";
}