phpformscodeignitersearch

Accessing page directly by URL shows blank page in codeigniter search


I'm working on a project in which if I try to search query it shows results, but if I try to access same page through same URL it shows blank page.any help would be greatly appreciated. Thanks!

My controller

Here is my search function

public function search($keyword = "default", $city = "NULL")
    {

            $keyword = $this->input->get();

            $data['city_name']  =   $keyword['city'];
            $data['service_name']   =   str_replace('-',' ',$keyword['search']);
            //echo "<pre>";print_r($data); die('kk');

// get sponsor data by city

    $data['sponsor']    =   $this->Home_model->getSponsor($keyword['city']);

            $data['packercontent'] =    $this->Home_model->getPackerContent($keyword['city']);
            $data['packers']  = $this->Home_model->get_results($keyword);
            //echo "<pre>";print_r($keyword);die;
            $data['info']           =   $this->Home_model->getSiteInfo();

            $data['page']   =   '';
            $this->load->view('viw_packers', $data);


    }

My modal

     // get all packers according to city
     public function get_results($keyword = 'default')
      {

        $this->db->select('*');
        $this->db->like('service', str_replace('-',' ',$keyword['search']));
        $this->db->where('city', $keyword['city']);
        $query  = $this->db->get('pf_package');

        return $query->result();
      }

// get all sponsor according to city
      public function getSponsor($city)
      {
        $this->db->select('*');
        $this->db->where('sp_city',$city);
        $this->db->from('pf_sponsor');
        $query = $this->db->get();

        return $query->row();
      }

// get all packer content according to city
      public function getPackerContent($city)
      {
        $this->db->select('*');
        $this->db->where('city',$city);
        $this->db->from('pf_packers_content');
        $query = $this->db->get();

        return $query->row();
      }

My view file

  <form class="form-inline" id="myForm" method="get" action="">
          <div class="form-group row nomargin">
                                <div class="servicewrap col-lg-5 col-md-5 col-sm-5 col-xs-12 nopadding">
              <select class="form-one-class" name="search" id="service">
               <option value="moving-locally">Moving Locally</option>
               <option value="moving-overseas">Moving Overseas</option>
               <option value="corporate-relocation">Corporate Relocation</option>
               <option value="commercial-relocation">Commercial Relocation</option>
               <option value="packaging-services">Packaging Services</option>
               <option value="storage-services">Storage Services</option>
             </select>
                                </div><!-- /.servicewrap -->

                                <div class="citywrap col-lg-5 col-md-5 col-sm-5 col-xs-12 nopadding">
                                    <input type="text" class="form-control" id="city" name="city" autocomplete="off" placeholder="select city">
              <div id="category-suggestions">
                   <div class="suggestions col-xs-12" id="category-autoSuggestionsList">

                   </div>
              </div>
            </div><!-- /.citywrap -->
                                <div class="buttonwrap col-lg-2 col-md-2 col-sm-2 col-xs-12 nopadding">
                                    <button type="submit" class="btn btn-default" id="search-button" >search</button>
                                </div><!-- /.buttonwrap -->
                            </div>
                        </form>

Here is my js code

I call my search form action through js.

<script>
$(document).ready(function(){
  $("#search-button").click(function(e){
    var city = $("#city").val();
    var lowerCity = '-'+city.toLowerCase();
    document.getElementById("myForm").action = "<?php echo base_url('search/packers-and-movers-in'); ?>"+lowerCity;
  });
});

</script>

Solution

  • Here is your controller function

    public function search($keyword = "default", $city = NULL)
    {
    $data["sponcer"] = $this->your_model->searchByKeyword($keyword);
    $data["packerContent"] = $this->your_model->searchByKeyword($city);
    
    $this->load->view("yourview", $data);
    }
    

    This is just demo code, you need to frame your by your need. You don't really need $_GET to get the values from querystring.

    If you write above code then your url like : http://localhost/profilehome/search/packers-and-movers-in-jaipur/Jaipur

    check the documentation here : https://www.codeigniter.com/user_guide/general/urls.html