2

This is my first foray into using ajax with a search input box. My ajax works fine however I do not know how to properly return the search data from the model and controller.

The controller: The "You have found twiiter ID's" works fine, however the $num does not come through

function search()
{         
    $this->form_validation->set_rules('twit', 'Search', 'required|trim|min_length[2]|max_length[25]');//check
    if ($this->form_validation->run() == TRUE) {
    $this->load->model('twit_model');
    $query = $this->twit_model->entity();
    if(!$query = $this->twit_model->entity())
    {  
    echo "The name does not exist";
        }else{     
        echo "<h3>You found $num Twitter ID's</h3>";    //this shows up in the view           
        echo "<li class=\"list1\">$twit_id - $name</li> ";   //the "-" shows up in the view        
        }               
    }
}

The model.

function entity()
{
    $twit = $this->input->post('twit');
    $this->db->select('id, name, state, party, twit_id, job');
    $this->db->like('name', $twit);
    $this->db->or_like('state', $twit);
    $this->db->or_like('party', $twit);
    $this->db->or_like('twit_id', $twit);
    $this->db->or_like('job', $twit);
    $query = $this->db->get('twit');
    $num = $query->num_rows();
    if($query->num_rows() > 0) {
         $data - array(
         $query,
         $num
         );
        return $data;
    }
  }

The basics of the controller and model work fine, I realize at the end of both I am not passing the data from the model to the controller correctly

The jquery

$(function() {
$('#display').hide();   
$('#name_error').hide(); 
    $('#submit').click(function() { // could be #form .submit

      var twit = $("#twitter_search").val();
        if (twit == "") {
          $("label#name_error").show();
          $("input#twitter_search").focus();
          return false;
        }  
var datastring = $('#form').serialize();         
$.ajax({
   url: "<?php echo site_url('twitter/search'); ?>",
   type: "POST",
   data: datastring, 
    success: function(msg) {
        $('#display').html(msg).show(3000);            
        }
    }); 
    return false;       

});
});

The jquery works just fine, alerts /firebug tell me I am passing the data to the controller. I just dont know how to write the passing of the variable data to the model->controller->view

Thanks for reading

2 Answers 2

3

It kinda just looks like you are using variables out of scope.

in your model you should change this:

$data - array(
     $query,
     $num
     );

to something like this:

$data = array('query' => $query, 'count' => $num, 'twit_id'=>$twit);

then in your controller change this:

echo "<h3>You found $num Twitter ID's</h3>";           
echo "<li class=\"list1\">$twit_id - $name</li> ";  

into something like this:

echo "<h3>You found ".$query['count']." Twitter ID's</h3>"; 
echo "<li class=\"list1\">".$query['twit_id']." - ".$query['query']."</li> ";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you icchanobot, thats very helpful. I appreciate it
1
$data - array(
     $query,
     $num
     );

did you make a typo here? should it be $data = array? besides, the variable $num inside the search function is neither b defined nor returned.

3 Comments

Yes that is a typo on my part. $num is $num = $query->num_rows(); in the model. But still have the same problem converting $query & $num to variables
yeah, $num only in the model, but it's not returned to the search function. So you can not just use the $num in your search function directly.
I can pass data through the controller to a view, but I have never converted "data" to a variable in the controller. How is that done?

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.