如何为特定用户名指定范围

时间:2014-12-16 07:42:56

标签: php html append

已找到解决方案!感谢@northkildonan和@Doug Leary。


解决方案代码;

if ($result->num_rows > 0) {
 // output data of each row
 while($row = $result->fetch_assoc()) {
     echo "<li class='list-group-item'>" . $row["user_name"];
if ($row["user_name"] == "TacoLover22") {
     echo "<span class='badge'>Dev</span>";
}
echo "</li>";
 }

##

我有一个用户名列表,我有一个代码检查用户名是否存在,如果它存在,我想要一个div附加到它。 Here's an image explaining this

但是,我的代码有效,但它没有将该div分配给选定的用户名。

这是PHP代码;

<?php 

if (
    $row["user_name"] === "TacoLover22");{
    echo  "<span class='badge'>Dev</span>";
    }
?>

这是抓取所有用户名的其他php代码;

<?php
$servername = "####";
$username = "####";
$password = "########";
$dbname = "########";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT user_name FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<li class='list-group-item'>" . $row["user_name"];
     }
} else {
     echo "0 results";
}

$conn->close();
?>  

可以找到此代码的结果here.

编辑:这是建议的完整代码,但似乎仍无效。现在没有出现跨度。

// output data of each row
while($row = $result->fetch_assoc()) {
echo "<li class='list-group-item'>" . $row["user_name"];
if ($row["user_name"] === "TacoLover22") {
     echo "<span class='badge'>Dev</span>";
}
echo "</li>";
}

感谢所有人和任何帮助。

3 个答案:

答案 0 :(得分:3)

我认为这样做会:

// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<li class='list-group-item'>" . $row["user_name"];
    if ($row["user_name"] === "TacoLover22") {
         echo "<span class='badge'>Dev</span>";
    }
    echo "</li>";
}

答案 1 :(得分:1)

你必须将<span>附加到你的mysql-fetch-loop中(注意:你在谈论div,但你使用的是span,这在html中是不同的。)

while($row = $result->fetch_assoc()) {
    echo "<li class='list-group-item'>" . $row["user_name"];
    if ($row["user_name"] == "TacoLover22")
    {
        echo  "<span class='badge'>Dev</span>";
    }
}

解释:您正在循环中迭代mysql查询的结果集。变量$row是指针指向结果各行的指针 在你的while循环完成后,你的“指针”将始终显示在mysql查询的最后一行结果中。所以$row["user_name"]将始终设置为数据库中找到的最后一个用户名。这就是为什么你必须在while循环中访问它。


如何处理多个用户(根据要求):
我决定在这里使用switch而不是if elseif语句,因为它更好地阅读并且更少编写imho:

while($row = $result->fetch_assoc()) {
    echo "<li class='list-group-item'>" . $row["user_name"];

    switch($row["user_name"])
    {
        case "TacoLover22":
            echo "<span class='badge'>Dev</span>";
        break;
        case "AnotherUser":
            echo "<span class='badge'>User</span>";
        break;
    }
}

答案 2 :(得分:0)

假设你正在使用sqli扩展 user_username列是特殊用户列表,否则您将需要2个表,1个表用于存储用户,1个辅助表用于存储特殊用户

//query
$sql = "SELECT user_name FROM users";
//send to mysql, get result 
$sendquery = $conn->query($sql);
//fetch results as array
while ($result=$sendquery->fetch_array) {
  echo 'username:' . $result['user_name'] . '<span class='badge'>Dev</span>';
}

但是你的问题很混乱,如果你有不同类型的用户,你应该使用2个表

TABLE USERS user_id | name | dev_id | email | password | ...
TABLE DEVS dev_id | name | level | access | ....

SQL query, using LEFT JOIN

SELECT 
    usern.name as username, dev.name as devname
FROM USERS as user
LEFT JOIN DEVS as dev
USING dev_id

php output use $result['devname']

$结果[ ''] 如果您只需要显示“TacoLover22”,如果这是您需要添加内容的唯一人,请在while循环中添加IF

//query
$sql = "SELECT user_name FROM users";
//send to mysql, get result 
$sendquery = $conn->query($sql);
//fetch results as array
while ($result=$sendquery->fetch_array) {
    if ($result['user_name'] == "TacoLover22") {
        echo 'username:' . $result['user_name'] . '<span class='badge'>Dev</span>';
}
    else {
        echo $result['user_name'];
    }
}