0

This is my code

user_controller.php

                  public function get_users(){  

                     //load the database  
                     $this->load->database();  

                     //load the model  
                     $this->load->model('user_model');  

                     //load the method of model  
                     $this->user_model->select();  
                     return //the data in view  
                     $this->load->view('user_view', $data);

                  }

user_model.php

         function select(){
         $query = $this->db->select('barcodeout','barcodein','emp_id','emp_name','amount','time');
         $this->db->from('porters');
         $query = $this->db->get();
         return $query->result();
}

user_view.php

          <table>
          <tr>
          <td><strong>Ticket Out</strong></td>
          <td><strong>Ticket In</strong></td>
          <td><strong>Emp ID</strong></td>
          <td><strong>Emp Name</strong></td>
          <td><strong>Amount</strong></td>
          <td><strong>Time</strong></td></tr>

           <?php foreach($query->result() as $employee){ ?>
           <tr>
             <td><?=$employee->barcodeout;?></td>
             <td><?=$employee->barcodein;?></td>
             <td><?=$employee->emp_id;?></td>
             <td><?=$employee->emp_name;?></td>
             <td><?=$employee->amount;?></td>
             <td><?=$employee->time;?></td>
            </tr>     
              <?php }?>  
          </table>
         </div> 
        </div>

I have tried many things with this, all I get is 'undefined variable' at the end, suggest me with all three MVC codes to get the database records, it will really appreciated, Thanks.

2
  • 1
    post the exact error you are getting Commented Dec 6, 2016 at 7:50
  • As a side note, in your controller you do not need to use "return" before your $this->load->view. Just load the view along with the $data. You should also use a conditional statement for the model return. If($this->user_model->select()){$data['employees'] = $query->result()} Commented Dec 6, 2016 at 14:16

1 Answer 1

1

Controller

public function get_users(){  

    //load the database  
    $this->load->database();  

    //load the model  
    $this->load->model('user_model');  

    // You need to pass the model data into view
    $data['employees'] = $this->user_model->select(); 

    //the data in view   
    return $this->load->view('user_view', $data);

}

View

<table>
   <tr>
      <td><strong>Ticket Out</strong></td>
      <td><strong>Ticket In</strong></td>
      <td><strong>Emp ID</strong></td>
      <td><strong>Emp Name</strong></td>
      <td><strong>Amount</strong></td>
      <td><strong>Time</strong></td></tr>

      <?php foreach($employees as $employee){ ?>
           <tr>
              <td><?=$employee->barcodeout;?></td>
              <td><?=$employee->barcodein;?></td>
              <td><?=$employee->emp_id;?></td>
              <td><?=$employee->emp_name;?></td>
              <td><?=$employee->amount;?></td>
              <td><?=$employee->time;?></td>
           </tr>     
       <?php }?>  
</table>

You cannot call $query->result() from your view page. Its undefined there. And never call model methods from your view. In your model you should have database related methods, in controller all the business logics and your view should contain only what you pass from your controller. Hope this helps. :)

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

3 Comments

Thanks Das, all was working fine but now not my form is not working when I submit it not at all giving me the reaction, Even when type something into the text fields it should clear the text fields as well but its not at responding ,if i'm making one more test form in that, it is still not working, Please help with this also, it would be great appreciations.Thanks
I didn't get what you are trying to say. Please update your question with proper error messages
Sayantan!, Appreciate your response man, the question was, my form wasn't working. Later I came to know that, I didn't load the form helper, its working fine now. Thanks again.

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.