0

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.

10
  • its probably interpreting $results[message] - the message part, as a definition, which would be NULL if not defined Commented Oct 27, 2013 at 3:42
  • @BarryChapman i changed everything to $results['messages'] ... (added quotes) .. still no luck Commented Oct 27, 2013 at 3:56
  • Use mysql_fetch_assoc() instead of mysql_fetch_array() Commented Oct 27, 2013 at 3:58
  • @BarryChapman did that (what's the difference by the way?) .. still only highlighting the first time, then subsequent times it doesn't .. Commented Oct 27, 2013 at 4:02
  • mysql_fetch_assoc will parse like an associative array instead of a numerically indexed array Commented Oct 27, 2013 at 4:03

3 Answers 3

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

the message probably should be quoted:

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

There are quite a few other similar issues

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

thanks, now i'm very perplexed, because even after adding quotes, the green background only occurs the first time, the consequent messages don't work :(
How many reviews are registered in the database? Who's with msgread equal to 0?
1

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";   

    }  
}

2 Comments

Please try to keep formatting suggestions and general tips (when not attempting to answer the question) as edits to the question or comments rather than in an answer..
@asifrc It might be the reason for his problem. I have seen the lack of quotation marks on associative arrays and also the lack of ; in styles giving unpredictable behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.