How to display records using database in php using codeigniter framework ?

In the Codeigniter display records/fetch using database with MYSQL and foreach, This tutorial is intended to introduce you to the CodeIgniter framework and the basic principles of MVC architecture. It will show you how a basic CodeIgniter application is constructed in step-by-step fashion.

Static pages

Note: This tutorial assumes you’ve downloaded CodeIgniter and installed the framework in your development environment.



The first thing you’re going to do is set up a controller to handle static pages. A controller is simply a class that helps delegate work. It is the glue of your web application.

For example, when a call is made to:

    http://example.com/home/view/

We might imagine that there is a controller named “home”. The method being called on home would be “latest”. The home method’s job could be to grab 10 post items, and render them on the page. Very often in MVC, you’ll see URL patterns that match:

    http://example.com/[controller-class]/[controller-method]/[arguments]

As URL schemes become more complex, this may change. But for now, this is all we will need to know.

I have a setup name as erp you can rename as per your project

Create a file at application/controllers/Home.php with the following code.



<?php

// First we created controller to call

class Home extends CI_Controller {


 //Here we can call model and helper class

     public function __construct()
        {
                parent::__construct();
                $this->load->model('emp_model');
                $this->load->helper('url_helper');
        }
 
//default function

    public function  index() {
   

        $page = 'home';   //page name we create /views/pages/home.php
   
$data['title'] = 'List of Employee';   //data to pass value

         //here we make condition to get a value

        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        //here we are getting a value from database

        $data['employee'] = $this->emp_model->get_emp();

//we created header and footer.php

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer');      
     
       
    }
     
    public function view($page = 'home')
        {
                       
               
        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);        
        
            
        }
         public function detail($page = 'detail')
        {
                     echo "test"; 
               
         if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
         {
                // Whoops, we don't have a page for that!
                show_404();
         }
 
      }
}


view

we need to create a view file in application/view/pages/home.php 



<!--now let's display the content here and passign title from controller-->

<div style="margin-left: 43%;">

 
<h1><?php echo $title; ?></h1>

    <table style="border:1px solid #000;text-align:center;">
    <thead>
       <th style="border:1px solid #000">Name : </th>
       <th style="border:1px solid #000">Email : </th>
        <th style="border:1px solid #000">Address : </th>
     
     </thead>
    <tbody>
     
      <!--here we get a value from table employee  display name and email of employee   and now lets run in the browser  add address to display  colunm name as address-->
     
    <?php foreach ($employee as $emp_item): ?>

      <tr >
       <td style="border:1px solid #000" ><?=ucfirst($emp_item['name']);?></td> <br/>
      <td style="border:1px solid #000"><?=$emp_item['email'];?></td> <br/>
            <td style="border:1px solid #000"><?=$emp_item['address'];?></td>
 
       
    </tr>  

   <?php endforeach; ?>
 
    </tbody>
    </table>      

</div>


Create a Database table



CREATE TABLE IF NOT EXISTS `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) NOT NULL,
  `email` text NOT NULL,
  `address` varchar(128) NOT NULL,
  `slug` varchar(128) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `slug` (`address`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;



db config  go to the application/config/database.php add your hostname, username, password and database name



$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);


model : now need to create a model file go to application/models/emp_model.php


<?php

// here class name first character capital

class Emp_model extends CI_Model {

        //load database
     
        public function __construct()
        {
                $this->load->database();
        }
     
        // this function for get a database value
     
        public function get_emp($slug = FALSE)
         {
         if ($slug === FALSE)
         {
                 //here our query to retrieve database
             
                $query = $this->db->get('employee');
                return $query->result_array();
         }

        $query = $this->db->get_where('employee', array('slug' => $slug));
        return $query->row_array();
        }
     
}



header.php

now need to create a views file go to application/views/templates/header.php



<html>
        <head>
                <title>CodeIgniter Static Pages Tutorial</title>
        </head>
        <body>



footer.php

now need to create a view file go to application/views/templates/footer.php



<em>&copy; 2018</em>
        </body>
</html>


No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...