I have a column in my database’s messages table. It is called “status” and is basically updated each time a user has “read” a message, makes a message “favourite”. By default the columns tinyint value is set to “0”.
0 = unread
1 = read
2 = favourite
I have some jquery and css going on in my inbox page which enables a user to be able to select all message checkboxes or none if they have been selected.
Right above my inbox messages are these links “all – none – read – unread – favourite…” Which are basically options for what messages a user can have automatically checked/selected.
What I want to do
I would like to have my jquery grab the values from my databases messages table.. status column and some how uses those to determine if a message has been read.. is unread or has been made a favourite.
With PHP I have done something similar already with a simple if statement.
<?php foreach ($query as $row): ?>
<tr>
<td width="5%"><input name="message" id="messages" type="checkbox" value=""></td>
<td width="5%">fav*</td>
<td><?php if ($row['status'] == 0) { echo "Unread"; } elseif ($row['status'] == 1) {echo "Read";} elseif ($row['status'] == 3) { echo "Replied";}?></td>
<td><?php echo $row['from_user']; ?></td>
<td><?php echo $row['subject'] . " - " . $row['message']; ?></td>
<td><?php if ($row['date_sent'] == date('Y-m-d')) { echo $row['time_sent']; } else echo $row['date_sent']; ?></td>
</tr>
<?php endforeach; ?>
JQuery
<script type="text/javascript">
$('#links').delegate('a', 'click', function(ev) {
// reset all checkboxes
$('input:checkbox').attr('checked', false);
// get info, what is the user choice
whichMessages = $(this).attr('id');
// do our main work - select checkboxes
switch (whichMessages) {
case 'all':
$('input:checkbox').attr('checked', true);
break;
case 'read':
$('input:checkbox.read').attr('checked', true);
break;
case 'unread':
$('input:checkbox.unread').attr('checked', true);
break;
case 'fav':
$('input:checkbox.fav').attr('checked', true);
break;
};
// add some user-frendly markup
$('#links a').removeClass('active');
$(this).addClass('active');
// and standard action to prevent standard link click event
ev.preventDefault();
});
</script>
HTML
<p id="links">
<a href="#" id="all" class="pseudo">all</a>,
<a href="#" id="none" class="pseudo active">none</a>,
<a href="#" id="read" class="pseudo">read</a>,
<a href="#" id="unread" class="pseudo">unread</a>,
<a href="#" id="fav" class="pseudo">favourite</a>
Copy the if statement, or better yet use the
switchstatement to print the correct css class in the checkbox