简体   繁体   中英

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'. this is set to '0' by default. Therefore it should make any row with the msgread = 0 -> green. 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 ..

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. You are using this $result[message_id] when the most appropriate would be $result['message_id'] . The rest works as expected, the records with msgread equal to 0 stayed with the green line.

Your code looks a little nasty and is not easy to read.

  • You should use mysqli_fetch_assoc() .
  • Always end style with a ;
  • use quotation on associative array
  • more logic choice of var names
  • where does $userid come from? 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";   

    }  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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