user2610240
user2610240

Reputation: 33

how to pass parameters from views to model

i have a id from my view

code in my view

$jcid=$row->id;

and i have passed to my model as

        function getall12()
        {

           $id = $this->input->get('$jcid');
           $this->db->select();  
           $this->db->from('jobs');  
           $this->db->where('job_cat =','$id');  
           $query4=$this->db->get();
           return $query4->result();   
           return $query4->num_rows();

        } 

is this the right way to do so

then how should i load this to my controller and back to my view

Thanks in advance.

amit

Upvotes: 0

Views: 1445

Answers (4)

rfpdl
rfpdl

Reputation: 964

Currently I am using this method to get it done:

  1. View Code

    function detail(id)
    {
    jConfirm("Check Detail?","Confirm",function(r){
       if(r)
           {
               $.post("<?php echo site_url('info/detail'); ?>",
                {
                    id      : id
    
                },function(data){
                  alert(data);                
                });
           }
    });
    }
    
  2. Controller Code

    public function detail()
    {
    $this->load->model('your_model_name');
    $jcid = $this->input->post('id);
    $result = $this->your_model_name->getall12($jcid);
    echo $result;
    }
    
  3. View Code

    function getall12($id)
    {
    
    
       $this->db->select();  
       $this->db->from('jobs');  
       $this->db->where('job_cat =','$id');  
       $query4=$this->db->get();
       return $query4->result();   
       return $query4->num_rows();
    
    } 
    

Upvotes: 0

Christian Giupponi
Christian Giupponi

Reputation: 7618

Your approach is wrong, MVC works Controller<-->Model-->view so you have tou pass data from controller to model then to the view.
If you need to pass a value you first need to get it in your controller then pass it to the model.

For example in you view (in a form?) you have to do something like:

echo form_open('your_controller');
<input type="text" name="jcid" value="<?php echo $row->id; ?>">
echo form_close();

and retrive the value in your controller with:

$jcid = $this->input->get('jcid');

and now pass it to the model:

$result = $this->your_model->getall12($jcid);

and in your model:

function getall12($id)
        {


           $this->db->select();  
           $this->db->from('jobs');  
           $this->db->where('job_cat =','$id');  
           $query4=$this->db->get();
           return $query4->result();   
           return $query4->num_rows();

        } 

Upvotes: 1

SasaT
SasaT

Reputation: 731

Christian Giupponi is write about everything. I just want to add there is another way of passing values between view and controller by using URI like this.

in your view:

echo form_open('/controller/method/id');

OR it also works with anchor:

echo anchor('/controller/method/id', 'Save ID');

and then in your controller:

$id = $this->uri->segment(3);

Upvotes: 0

Miomir Dancevic
Miomir Dancevic

Reputation: 6852

Why you are not do on controller

$data['jcid']=$row->id;

But of course do a foreach loop

Then you have in conttroller and just pass to view

Upvotes: 0

Related Questions