开发者

JQuery grab mysql data? or use PHP as a middle man between the database and jquery?

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</开发者_如何学Ca>


Don't use jQuery to connect to your database as the user can be able to inject his own queries and get unwanted access to your database. However, use your php script as a middle-man to return the values you want into a JSON object maybe then fetch it using ajax.


Copy the if statement, or better yet use the switch statement to print the correct css class in the checkbox

<?php foreach ($query as $row):  ?>
  <tr>
 <?php switch($row['status']){
  case 0: $status = "Unread"; break;
  case 1: $status = "Read"; break;
  case 3: $status = "Replied"; break;
  default: $status = '';
 }?>   
 <td width="5%"><input name="message" id="messages" type="checkbox" value="" class=<?php echo $status;?>"></td>
    <td width="5%">fav*</td>
    <td><?php echo $status;?></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; ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜