Search code examples
phpcodeignitergoto

How sort data in view page in codeigniter


I am getting the data in a view page. Now I want to search it with some data coming form a text box in the same view page.

What should I do? Can I use ajax or jQuery?

This is my view page

<li class="">Doctors</li>
</ol>
<div class="list col-xs-8">
    <div class="col-md-8 col-md-offset-2">
    <?php echo $this->session->flashdata('msg'); ?>
    </div>
    <ul>
        <input type="text" name="search_data" placeholder="Search ..." class="form-control"  id="search_data" onkeyup="ajaxSearch();"style=" height: 50px; width: 200px !important;">           
        <?php  
                if(isset($doc)):  
                    foreach ($doc as $row): 
            ?>
        <li>
            <div class="imgt"><img src="<?php echo base_url("./resources/images/");  if($row->dr_img) echo $row->dr_img;
         else echo "no-img.jpg"; ?>" height="90px" width="82px">
            </div>
            <div class="text">
                <h3><b>Dr. <?php echo  $row->dr_name;?></b><br></h3>
                <p><?php echo $row->spec_specialise; ?><br><?php echo  $row->district;?><br><?php echo  $row->state;?></p>
            </div>
            <div class="text"></div>
            <div class="link">
                 <a href="<?php echo site_url('User/doctor_profile_view/'.$row->id); ?>"><i class="ace-icon fa fa-eye sym"></i>View</a></div>
        </li>
        <?php endforeach; 
        endif;
        ?>
    </ul>
    <div class="space"></div>
</div>
</div>
<div class="pdt_rightt">
    <center>
    </center>
</div>

Solution

  • In the title of your post you say you want to "sort" the data, but in the description you say you want to "search" it.

    I would recommend doing this without Ajax to begin with to get an understanding of the fundamentals.

    Your View could look like this:

    <?php echo form_open( current_url() ); ?>
      <h2>Search</h2>
      <input type="text" name="search" placeholder="Enter search word" />
      <button>Search</button>
    <?php echo form_close(); ?>
    
    <?php if (isset($docs)) : ?>
    <p>
      <a href="<?php echo current_url(); ?>?sort=dr_name&amp;search=<?php echo html_escape( urlencode( $search ) ); ?>">
        Sort by name
      </a>
    </p>
    <ul>
      <?php foreach ($docs as $row) : ?>
      <li><?php echo html_escape( $row->dr_name ); ?></li>
      <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    

    Then your Controller would be like this:

    public function index() {
      $search = $this->input->get_post('search');
      $sort = $this->input->get('sort');
      $docs = $this->doc_model->get_docs( $search, $sort );
    
      $data = array(
        'search' => $search,
        'sort'   => $sort,
        'docs'   => $docs
      );
      $this->load->view('docs', $data);
    }
    

    Your Model will then need to fetch all Doctors if $search is null, or query the Doctors' names by $search if it's not null. Likewise if $sort is null then you'd order by a default field (date created for example).

    If you have lots of results then you'd also want to look at CI's Pagination class to span the results over several pages.

    You can achieve all of this using Ajax but if you have lots of results you are still going to need to call your Controller and Model, similar to the above, to do the heavy lifting.