Jump to content

Variable in database result?


AJLX

Recommended Posts

Hello Guys,

I have a column named message_status it can only have the values "read" or "unread" in it. I would like to show an image in the column next to it. If the message is "read" then I would like to display a green dot, and if it is unread I would like to display a red dot. I would also like to be able to have anything in the row that's "unread" to appear in bold. I completely stumped by this! I have written the following code, which doesn't seem to work, presumably because it is run after the table has been displayed? I am very new to this so please be gentle!

 

Regards,

 

AJLX

 


$result = mysql_query("SELECT * FROM messages where username ='$username'ORDER BY {$_GET['orderbycol']} $sort");

echo "<table border='1'>
<tr width='200'>
<th>View</th>
// code to display table removed
</tr>"; 
$green_dot = $message_status; 
$red_dot = $message_status;
$green = "";
$red = "";
$bold = "";
while ($row = mysql_fetch_array($result))
{
  echo  "<tr>";
  $id = $row['ID'];
  echo  "<td><a href='/view_message.php/?view_message=$id'>View</a></td>";
  echo "<td>"  .$green . $red."</td>";
  echo  "<td>" .$bold . $message_status = $row['message_status'] . "</td>";
  echo  "<td>" .$bold. $row['date_sent'] . "</td>";
  echo  "<td>" .$bold. $row['contact_name'] . "</td>";
  echo  "<td>" .$bold. $row['subject'] . "</td>";
  echo  "</tr>";
}
if ($green_dot = 'read'){
$green ='<img src=\"assets/red.jpg\"/>';
echo $green;
} else {
  $red ='<img src=\"assets/red.jpg\"/>';
  $bold = "<b>";
  echo $red;


 

Link to comment
Share on other sites

Something more like this should get the image you want to display. Your loop doesn't know about the bit after it. (if ($green_dot = 'read')...)

 

You could use a simliar conditional to drop bold tags if that's how you'd like to do it as well. Using CSS to hit the whole row might be a little tidier, though.

 

echo "<table border='1'>
<tr width='200'>
<th>View</th>
// code to display table removed
</tr>"; 
while ($row = mysql_fetch_array($result))
{
  echo  "<tr>";
  $id = $row['ID'];
  echo  "<td><a href='/view_message.php/?view_message=$id'>View</a></td>";
  if ($row['message_status'] == 'read') {
$img ='green.jpg';
  }
  else {
$img ='red.jpg';
  } 
  echo '<td><img src="assets/' . $img . '"/></td>';
  echo  "<td>" .$bold . $message_status = $row['message_status'] . "</td>";
  echo  "<td>" .$bold. $row['date_sent'] . "</td>";
  echo  "<td>" .$bold. $row['contact_name'] . "</td>";
  echo  "<td>" .$bold. $row['subject'] . "</td>";
  echo  "</tr>";
}

Link to comment
Share on other sites

I would also add that having two statuses with the values of "read" and "unread" is not ideal. Unless you plan to add additional statuses I would suggest the database column be called "read" and you should store a boolean value (i.e. true/false).

 

Anyway, building upon dropkick_pdx's suggestioin here is what I would propose.

 

1. Add CSS code to your page or to an external style sheet to create two classes "read" and "unread".

 

2. Name the images "read.jpg" and "unread.jpg"

 

3. Add the following logic to the top of your script (ionclude the db query of course):

$messagesHTML = '';
while ($row = mysql_fetch_assoc($result))
{
    $messagesHTML .= "  <tr class=\"{$row['message_status']}\">\n";
    $messagesHTML .= "    <td><img src=\"assets/{$row['message_status']}.jpg\" /></td>\n";
    $messagesHTML .= "    <td>{$row['message_status']}</td>\n";
    $messagesHTML .= "    <td>{$row['date_sent']}</td>\n";
    $messagesHTML .= "    <td>{$row['contact_name']}</td>\n";
    $messagesHTML .= "    <td>{$row['subject']}</td>\n";
    $messagesHTML .= "  <tr>\n";
}

 

4. Then in the body of the HTML code do this

<table border="1">
  <tr width="200">
    <th>View</th>
  </tr>
  <?php echo $messagesHTML; ?>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.