1

I have a views column in my database. And I want to update the column with +1 when a hyperlink is clicked to visit a certain page. Am working with jQuery, Ajax and codeigniter. I an not able to do the correct thing so as to trigger update in the database. Please assist me with what am missing.

Below are my codes:

HTML

<a href="<?php echo base_url(); ?>site/details/<?php echo $value->hid; ?>" name="hid" id="clicker">

JS

$(document).ready(function(){

      $("a#clicker").click(function(){

        $.ajax({
          type: "POST",
          url: "<?php base_url(); ?>site/traffic",
          success: function(data){
            alert("I got a view");
            console.log(data);
          }
        });
      });
});

Controller

public function traffic(){
    $id = $this->input->post("hid");
    $this->My_model->updateView($id);
}

Model

public function updateView($id = NULL, $data){
    $this->db->set('view', 'view+1');
    $this->db->where('id', $id);
    $this->db->update($this->table, $data);
}

3 Answers 3

1

You're near to solve your problem.

First, in your JQuery Ajax code, I don't see the data parameter, which sets the Post data to be sent. In your case, you need to send to the PHP script the "hid" parameter, so you have to specify it this way:

<a href="<?php echo base_url(); ?>site/details/<?php echo $value->hid; ?>" name="hid" id="clicker" data-hid="<?php echo $value->hid; ?>">

Notice that I've added the "hid" to the attribute "data-hid" of the link. Now, we can retrieve the "hid" of the user clicked link in JQuery in order to send it to the PHP script.

$("a#clicker").click(function(e){
    var hidClicked = e.currentTarget.data("hid"); // retrieve the hid by data attr.
    $.ajax({
        type: "POST",
          url: "<?php base_url(); ?>site/traffic",
          data: { hid : hidClicked }, // pass it as POST parameter
          success: function(data){
            alert("I got a view");
            console.log(data);
         }
     });
 });

Now in your PHP code, you can retrieve the "hid" of the clicked element by

public function traffic(){
    $id = $this->input->post("hid"); // this will return the hid POST parameter
    $this->My_model->updateView($id);
}

Then, I don't know why are you declaring the "$data" parameter in the updateView method. If you want to update the database, you only need the "hid" of the clicked element. So the method will be

public function updateView($id = NULL){
    $this->db->set('view', 'view+1');
    $this->db->where('id', $id);
    $this->db->update("NAME_OF_YOUR_TABLE");
}

That would be my solution, maybe there is one better.

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

3 Comments

Thank you Dhaniram and manuman94. But am yet to solve my problem. Evertything remains the same. I can pick the appropriate id but am unable to update the table column. Someone suggested maybe I do a select for that column first, then increment the value. Honestly, I don't know how to achieve that, I tried a few tricks and was getting errors all over the place.
I eventually got it working without the jquery side. All I did was put the update query inside the details model function. And viola, it works now. The jQuery did grab the required data though. Perhaps I'd exploit it's usage in other areas of my project. However, I'd vote manuman94's suggestion up. It worked best, his Ajax part especially. Thanks everyone.
That's a pitty, but well, you got it working. You're welcome, thanks for the thumbs up!
1

You are not passing the second required parameter $data to updateView, which will cause an exception. Furthermore, $data is not needed in this instance as you are already setting the parameters to update inside $this->db->set().

1 Comment

AOA plx someone help me i need the whole program of insert select update delete . view file model file controller file first show the insert page in which we insert data to the database after inserting select the all data from the databass
0

You are not passing the second required parameter $data to updateView, which will cause an exception.

Furthermore, $data is not needed in this instance as you are already setting the parameters to update inside $this->db->set().

Example from CodeIgniter 3 Documentation:

$this->db->set('field', 'field+1');
$this->db->where('id', 2);
$this->db->update('mytable'); // gives UPDATE `mytable` SET `field` = 'field+1' WHERE `id` = 2

Updating Data - CodeIgniter 3 Docs

1 Comment

Ok @Matt, thanks. But that's exactly what I have on my PC. My issue is in the Ajax call. That's where I need help.

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.