IF statement within WHILE not working

Posted by Ds.109 on Stack Overflow See other posts from Stack Overflow or by Ds.109
Published on 2013-10-27T02:44:56Z Indexed on 2013/10/27 3:54 UTC
Read the original article Hit count: 79

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql