Sunday 17 August 2014

Display/Fetch Data from database in Codeigniter

Posted by Unknown  |  No comments

Controller:
                        Create  test_controller.php in your controller folder

                   class Test_controller extends CI_Controller
                                     {

                     public function __construct() {
parent::__construct();
$this->load->model('test_model');

                                                            }

                     function index()
                                                                   {
     $data['result']=$this->test_model->get_users();
             $this->load->view('test',$data);


                                                                   }
                                     }
--------------------------------------------------------------------------------------------------
Model:
                  Create test_model.php in your Model Folder

                          class Test_model extends CI_Model {

                                  function __construct()
                                           {
                    // Call the Model constructor
                      parent::__construct();
                                            }


                         function get_users()
                                            {
                     $this->db->select('*');
                     $this->db->from('tablename');
                     $data=$this->db->get();

                     if($data->num_rows()>0)
                            {
return $data->result();

                            } 
                                           }
                                                                           }

-----------------------------------------------------------------------------------------------

View:

                      Create test.php inside your view folder

                          <table width="200" border="1">
                                <tr>
                              <th>Sno</th>
                              <th>Name</th>
                             <th>Email</th>
                                 </tr>
                                <?php foreach($result as $row){ ?>

                                <tr>
                               <td><?php echo $row->sno; ?></td>
                               <td><?php echo $row->name; ?></td>
                                <td><?php echo $row->email; ?></td>
                                </tr>
  
                              <?php } ?>
   
                       </table>





       

03:24 Share:

0 comments:

Get updates in your email box
Complete the form below, and we'll send you the best coupons.

Deliver via FeedBurner
Proudly Powered by Blogger.
back to top