0

I have a controller: messages a method in that controller: favourite_checked() a variable that stores messages_id from my messages table in my database

This URL is produced

mysite.com/messages/favourite_checked/1 mysite.com/controller/method/message_id

When this URL is successfully called it connects to my model and updates a column in my messages table called "favourite". The 0 in that column is updated to 1.

0 = boxed unchecked (not favourite inbox message) - Greyed out image 1= boxed checked (user selected specific message as favourite message) - Coloured image

Anyway I some how need to have this URL loaded in my (ischecked) function in my javascript. The problem is it needs to also load the correct message_id the specific row that is being clicked on.

message_id = id... from my messages table so that is grabbed from the db and is added on to the end of the URL and is basically what my controller method stored in a variable and passes to a model as an argument and used in an sql query in a model to make sure correct row in database is updated.

In the following code I do the same kind of thing for my message inbox links. Works perfectly.

 <?php foreach ($query as $row):  ?>
  <tr>
    <?php switch($row['status']){
        case 0: $status = "unread"; break;            
        case 1: $status = "read"; break;
        case 2: $status = "replied"; break;
        case 3: $status = "fav"; break;
        default: $status = '';           
    }?>

    <td width="5%"><input name="message" id="messages" type="checkbox" value="" class="<?php echo $status; ?>"/></td>
    <td width="5%"><input name="sunny" id="" type="checkbox" value="" class="favourite" checked="checked" /></td>
    <td><?php echo $status; ?></td>
    <td><?php echo $row['from_user']; ?></td>
    <td><div id="test"><a href="<?php echo site_url("messages/read_message/".$row['id']); ?>"><?php echo $row['subject'] . " " . $row['message']; ?></a></a></div></td>
    <td><?php if ($row['date_sent'] == date('Y-m-d')) { echo $row['time_sent']; } else echo $row['date_sent']; ?></td>
  </tr>
<?php endforeach; ?> 

I just some how need to have the URL above with the id grabbed from my database load after box is checked so query can run and update the specific column in my database

and do the same thing when the box is unchecked again.

I would also need a foreach loop inside the javascript function because each message in their inbox would need to made available in order for them to be able to make messages favourite messages.

Here's my javascript

// favourite check box
    $('input.favourite:checkbox').simpleImageCheck({
  image: '<?php echo base_url()?>images/messages/check.png',
  imageChecked: '<?php echo base_url()?>images/messages/unchecked.png',
  afterCheck: function(isChecked) {
    if (isChecked) {



  //query to db from php to update favourite number to 1
  $.post('');

    }
//    else (!isChecked)
//        {
//            //query to db from php to update favourite number to 0
//              $.post('');
//        }
  }
});

Help appreciated thanks in advance.

1 Answer 1

1

You can save message id as data attribute in html. E.g.,

<tr data-message-id="<?php echo $id_goes_here; ?>">

instead of simple

<tr>

Then, in your event handler, you can get message id like this

$(this).closest('tr').attr('data-message-id')

I assume this points to an element clicked (or any DOM element inside that row)

PS With a recent jquery version (1.4.4, I think), you can also replace .attr('data-message-id') with .data('message-id').

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

Comments

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.