How to Search Record from database using Codeigniter

A simple program that I wrote before in PHP(Codeigniter) and MySQL to search a record from the database. The code is very easy to understand and use in your projects related to PHP(Codeigniter) and MySQL

HTML Code

<div class="row">
 <div class="col-lg-12">
  <div class="ibox float-e-margins">
    <div class="ibox-title"><h5>Search Students</h5></div>
        <div class="ibox-content m-b-sm border-bottom">
          <form role="form" class="" action="" method="get">
          <div class="row">
            <div class="col-sm-4">
              <div class="form-group">
                <label class="control-label" for="order_id">Sub Course Name</label>
                <select name="sub_course_id" value="" class="form-control" >
                
                <option value="">Select Sub Course</option>
                <?php
                foreach ($sub_course as $course)
                { 
                    $selected = '';
                    if(!empty($this->input->get('sub_course_id')))
                    {
                        if($course->sub_course_id == $this->input->get('sub_course_id'))
                        {
                            $selected = 'selected';
                        }
                    }
                    ?>
                <option <?=$selected?>  value="<?=$course->sub_course_id?>"><?=$course->sub_course_name?></option>
               <?php }
                ?>
                </select>
              </div>
            </div>
            
           </div>
           <button class="btn btn-info" type="submit" onclick = "this.form.submit();"><i class="fa fa-search"></i> Search</button>
           <button class="btn btn-white" type="reset" onClick="location.href='<?php echo base_url().$this->config->item ( 'panel_folder' );?>/sub_course_block/sub_course_block_list'">Reset</button>
          </form>
        </div>
    </div>
  </div>
</div>

Controller Function

public function sub_course_block_list() {
        $this->load->library ( 'pagination' );
        $this->load->model ( 'panel/panel_sub_course_block_model', '', TRUE );
        $data['sub_course'] = $this->db->get('medical_sub_course')->result();

        $total = $this->panel_sub_course_block_model->count_sub_course_blocks ();
        $per_page = $this->config->item ( 'adm_perpage' );

        $data ['sub_course_block_list'] = $this->panel_sub_course_block_model->get_sub_course_block_list ( $per_page, $this->uri->segment ( 4 ) );
        $base_url = base_url () . $this->config->item ( 'panel_folder' ) . '/sub_course_block/sub_course_block_list';

        $config ['base_url'] = $base_url;
        $config ['total_rows'] = $total;
        $config ['per_page'] = $per_page;
        $config ['uri_segment'] = '4';
        $this->pagination->initialize ( $config );

        $this->template->set_template ( "user" );
        $this->template->write ( 'title', 'Sub Course Block List | ' . $this->config->item ( 'site_name' ) );
        $this->template->write ( 'subtitle', 'Sub Course Block List' );
        $this->template->write_view ( 'content', 'panel/sub_course_block/sub_course_block_list', $data );
        $this->template->render ();
}

View

<table class="table table-striped">
  <thead>
    <tr>
      <th>Sub Course Name</th>
      <th>Block ID</th>
      <th>Sub Course Block Name</th>
    </tr>
  </thead>
  <tbody>
    <?php 
      foreach ($sub_course_block_list as $sub_course_block) {
   ?>
    <tr>
      <td><?php echo $sub_course_block->sub_course_name;?></td>
      <td><?php echo $sub_course_block->block_id;?></td>
      <td><?php echo $sub_course_block->sub_course_block_name;?></td>
      <?php }
      ?>
  </tbody>
</table>
<?php echo $this->pagination->create_links ();?>

Model function

function get_sub_course_block_list($limit = NULL, $offset = NULL) {
    $this->db->select ( 'sub_course_block_id,sub_course_name,block_id,sub_course_block_name,sub_course_block_status' );
    $this->db->from ( 'sub_course_block' );
    $this->db->join ( 'sub_course', 'sub_course.sub_course_id = sub_course_block.sub_course_id' , 'left' );
    if($this->input->get('sub_course_id'))
    {
        $this->db->where( 'sub_course.sub_course_id', $this->input->get('sub_course_id') );
    }

    $this->db->limit ( $limit, $offset );
    $this->db->order_by ( "sub_course_block_id", "desc" );
    $this->db->order_by ( "block_id", "desc" );
    $query = $this->db->get ();
    return $query->result ();
}
function count_sub_course_blocks() {
        $this->db->from ( 'sub_course_block' );
        $this->db->join ( 'sub_course', 'sub_course.sub_course_id = sub_course_block.sub_course_id' , 'left' );
        if($this->input->get('sub_course_id'))
        {
            $this->db->where( 'sub_course.sub_course_id', $this->input->get('sub_course_id') );
        }
        return $this->db->count_all_results ();
}

Leave a Reply

Your email address will not be published. Required fields are marked *