简体   繁体   English

WHILE中的IF语句不起作用

[英]IF statement within WHILE not working

I am working on a basic messaging system. 我正在开发一个基本的邮件系统。 This is to get all the messages and to make the row of the table that has an unread message Green. 这是为了获取所有消息并使表中未读消息的行变为绿色。 In the table, there is a column called 'msgread'. 表格中有一列叫做“ msgread”。 this is set to '0' by default. 默认情况下将其设置为“ 0”。 Therefore it should make any row with the msgread = 0 -> green. 因此,它应该使任何一行的msgread = 0->绿色。 this is only working for the first row of the table with the code i have - i verified that it is always getting a 0 value, however it only works the first time through in the while statement .. 这仅适用于具有代码的表的第一行-我已验证它始终获得0值,但是它仅在while语句中才有效。

require('./connect.php');

$getmessages = "SELECT * FROM messages WHERE toperson = '" . $userid . "'";

echo $getmessages;

$messages = mysql_query($getmessages);

if(mysql_num_rows($messages) != 0) {

    $table = "<table><tr><th>From</th><th>Subject</th><th>Message</th></tr>";

    while($results = mysql_fetch_array($messages)) {

        if(strlen($results[message]) < 30){
            $message = $results[message];
        }
        else {
            $message = substr($results[message], 0 ,30) . "...";
        }

        if($results[msgread] == 0){

            $table .= "<tr style='background:#9CFFB6'>";
            $table .= "<td>" . $results[from] . "</td><td>" . $results[subject] . "</td><td><a href='viewmessage.php?id=" . $results[message_id] ."'>" . $message . "</a></td></tr>";
        }
        else {
            $table .= "<tr>";
            $table .= "<td>" . $results[from] . "</td><td>" . $results[subject] . "</td><td><a href='viewmessage.php?id=" . $results[message_id] ."'>" . $message . "</a></td></tr>";

        }
    }
    echo $table ."</table>";
}
else {
    echo "No Messages Found";   
}

There's all the code, including grabbing the info from the database. 这里有所有代码,包括从数据库中获取信息。 Thanks. 谢谢。

if(strlen($results[message]) < 30){

the message probably should be quoted: 该消息可能应该被引用:

if(strlen($results['message']) < 30){

There are quite a few other similar issues 还有很多其他类似的问题

i tested your code an the only mistake i found was the lack of quoatation marks in the indices array $results. 我测试了您的代码,发现的唯一错误是索引数组$ results中缺少定界标记。 You are using this $result[message_id] when the most appropriate would be $result['message_id'] . 当最合适的是$result['message_id']时,您正在使用此$result[message_id] $result['message_id'] The rest works as expected, the records with msgread equal to 0 stayed with the green line. 其余的按预期方式工作,msgread等于0的记录保留在绿线中。

Your code looks a little nasty and is not easy to read. 您的代码看起来有些讨厌,不容易阅读。

  • You should use mysqli_fetch_assoc() . 您应该使用mysqli_fetch_assoc()
  • Always end style with a ; 总是以结束风格;
  • use quotation on associative array 在关联数组上使用引号
  • more logic choice of var names 变量名称的更多逻辑选择
  • where does $userid come from? $userid来自哪里? is the content safe? 内容安全吗?

Here is quickly cleaned version of your code : 这是您的代码的快速清除版本:

$query = "SELECT * FROM messages WHERE toperson = '" . $userid . "'";

if($results = mysqli_query($query)) {

    if(mysqli_num_rows($results) != 0) {

        $table = "<table><tr><th>From</th><th>Subject</th><th>Message</th></tr>";

        while($data = mysqli_fetch_assoc($results)) {

            if(strlen($data['message']) > 30){
                $data['message'] = substr($data['message'], 0 ,30) . "...";
            }

            $table .= "<tr";

            if($data['msgread'] == 0){
                $table .= " style='background:#9CFFB6;'";
            }

            $table .= ">";

            $table .= "<td>" . $data['from'] . "</td><td>" . $data['subject'] . "</td><td><a href='viewmessage.php?id=" . $data['message_id'] ."'>" . $data['message'] . "</a></td></tr>";

        }

        echo $table ."</table>";

    } else {

        echo "No Messages Found";   

    }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM