<?php
$rolenumber=$_SESSION["rolenumber"];
$result_messageid=$dbh->query("select distinct(messageid) from message where recepient='$rolenumber' or sender='$rolenumber' order by autoid desc");
$row_messageid=$result_messageid->fetchObject();
$count_messageid=$result_messageid->rowCount();


if($count_messageid>0){
do{
$result_message=$dbh->query("select * from message where messageid='$row_messageid->messageid' order by autoid desc");
$row_message=$result_message->fetchObject();

$result2 = $dbh->query("select * from users where rolenumber='$row_message->sender' ");
$row2 = $result2->fetchObject();
$innitials=substr($row2->lastname,0,1).$innitials=substr($row2->firstname,0,1);

if($row_message->dateread=="" and $row_message->sender!=$rolenumber){$color="lightred";}else{$color="grey";}
?>
<li onclick="message('<?php echo $row_message->messageid; ?>')" class='list-group-item list-group-item-primary' style='cursor:pointer;'><?php echo "<span style='color:white; font-size:20px; padding:7px; background-color:lightblue; width:18%; border-bottom-right-radius: 20px;'>
            <b>".$innitials."</b></span> "; if(strlen($row_message->message)>12){echo substr($row_message->message." ...",3,24);}else{echo $row_message->message;}  echo " <span class='alertnote' style='background-color:".$color.";float:right;font-size:12px;'><i>".$row_message->dater."</i></span></li>";

}while($row_messageid=$result_messageid->fetchObject());
}else{
    echo "<li class='list-group-item list-group-item-primary'>So far, you have NO message. </li>";
}

echo "
<br>
</div>
</ul>
";
?>