• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GET function does not work.

#1
Hey,
i have this code in my model file, which does not work for those conditions. Could you suggest any corrections?!

The url in this situation is pets?petStatus=2&petType=1 etc..etc..


My other question is "How can i get each url parameter individually, with the same results even if the order is different?!",
e.g if the above url was like pets?petType=1&petStatus=2 etc..etc..

Thank you again!

Code:
public function filter_pets($pet_entry_id = 0, $pet_entry_slug = FALSE){

     if($this->input->get('petStatus') || $this->input->get('petType') ){

       if ( $this->input->get('petStatus') ){
         $pet_data = array(
           'pet_status' => $this->input->get('petStatus')
         );
       } elseif ( $this->input->get('petType') ){
         $pet_data = array(
           'category_id' => $this->input->get('petType')
         );
       } elseif ( $this->input->get('petStatus') || $this->input->get('petType')){
         $pet_data = array(
           'pet_status' => $this->input->get('petStatus'),
           'category_id' => $this->input->get('petType')
         );
       }

       $query = $this->db->get_where('pets', $pet_data);
       return $query->result_array();
     } else {

     $query = $this->db->get('pets');
     return $query->result_array();
   }

 }

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#2
@HarrysR,

Your 1st condition could return null and still execute. Maybe what you want is...
if( !is_null($this->input->get('petStatus')) || !is_null($this->input->get('petType')) )
or you can check to see if it is set...
if(isset($this->input->get('petStatus')) || isset($this->input->get('petType')) )


Also, you could just output the input object and view it's contents.

As for your second question it doesn't matter what order the variables are in as long as you refer to them by their get id it will always be correct.
Reply

#3
(08-14-2018, 04:14 PM)@php_rocs Wrote: Your 1st condition could return null and still execute. Maybe what you want is...
 if( !is_null($this->input->get('petStatus')) || !is_null($this->input->get('petType')) )
or you can check to see if it is set...
 if(isset($this->input->get('petStatus')) || isset($this->input->get('petType')) )


Also, you could just output the input object and view it's contents.

As for your second question it doesn't matter what order the variables are in as long as you refer to them by their get id it will always be correct.

Well.. still nothing.. the problem persists.. 

I tried the "isset" way i get an error that i can't use "isset"..
I tried the !is_null as you can see below and the results are still the same. if i have ?petType=1&petStatus=2 in url, the data do not get filtered.

Pet_model.php
Code:
public function filter_pets($pet_entry_id = 0, $pet_entry_slug = FALSE){

     $this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');
     $this->db->join('ws_zone', 'ws_zone.zone_id = pets.pet_city');
     $this->db->join('users', 'users.user_id = pets.user_id');
     $this->db->join('contact_info', 'contact_info.contact_info_id = pets.contact_info_id');

     if(!is_null($this->input->get('petStatus')) || !is_null($this->input->get('petType')) ){

       if ( $this->input->get('petStatus') ){
         $pet_data = array(
           'pet_status' => $this->input->get('petStatus')
         );
       } elseif ( $this->input->get('petType') ){
         $pet_data = array(
           'category_id' => $this->input->get('petType')
         );
       } elseif ( !is_null($this->input->get('petStatus')) && !is_null($this->input->get('petType')) ){
         $pet_data = array(
           'pet_status' => $this->input->get('petStatus'),
           'category_id' => $this->input->get('petType')
         );
       }

       $query = $this->db->get_where('pets', $pet_data);
       return $query->result_array();
     } else {

     $query = $this->db->get('pets');
     return $query->result_array();
   }

 }



Pets.php (controller)
Code:
public function index(){

   $data['pets'] = $this->pet_model->filter_pets();

   $this->load->view('templates/header', $data);
   $this->load->view('pets/index', $data);
   $this->load->view('templates/footer', $data);
 }

JQuery
Code:
 $('#filters select').change(function(){
     let filters = [];
     $('#filters select').each(function(){
         const string = $(this).attr('id');
         const value = $(this).val();
         if(value != '') {
           filters.push(string + '=' + value);
         }
     });
     window.location.search = filters.join('&');
 });


HTML file
Code:
  <div class="col-lg-2" id="filters">    
       <div class="form-group mt-3">
         <select class="form-control filter-selection" id="petStatus">
           <option value="">-- Status --</option>
           <option value="1">Lost</option>
           <option value="2">Found</option>
         </select>
       </div>

       <div class="form-group input-group">
         <div class="input-group-prepend">
           <span class="input-group-text"><i class="fas fa-paw"></i></span>
         </div>
         <select class="form-control filter-selection" id="petType">
           <option value="">-- Type --</option>
           <option value="1">Cat</option>
           <option value="2">Dog</option>
           <option value="3">Other</option>
         </select>
       </div>
</div>

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#4
@HarrysR,

Wait a minute. Let's back up. On your page with the drop down lists. Does the user submit a form or is jQuery doing an ajax call to a controller?
Reply

#5
@php_rocs,
In my way i ve created a model which gets data from url and i call it in index controller.
It is activated through Jquery on selected dropdowns.
To be honest i dont know if its the right way to do this. If you have any suggestions please feel free to inform me.
I have some dropdowns which will filter the results.

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#6
@HarrysR

That's not how CI works. Models can not receive data from a url. It can only be given data from a controller. I recommend that you do the CI tutorial that explains how the MVC architecture works ( https://codeigniter.com/user_guide/tutor...l#tutorial ).

CI is a MVC framework.
Reply

#7
@php_rocs, 

Yeah thats what i mean! I pass data through controller! The fact is that it works fine but not entirely as i want it to be. Even though it filters the data i cant make it pass more data.

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#8
@HarrysR,

How are you passing the data from the controller to the model? In your controller you are not passing the get data to the model. You are just calling the model. You should be doing something like this (within the controller) ...

$data['pets'] = $this->pet_model->filter_pets($this->input->get('petStatus'),$this->input->get('petType'));
Reply

#9
@php_rocs,

i did this and nothing happened. I'll try something else using ajax. If it works i'll inform you.

Have you ever faced that kind of issue?! I mean about filtering db data. If so, how did you solve it, since i might have a wrong approach of the solution.

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply

#10
@php_rocs,

FINALLY MADE IT!!!
It was simpler than i thought and it works like a charm!!

Pet_model
Code:
   public function get_filtered_pets($pet_status, $pet_type){

     $this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');
     $this->db->join('ws_zone', 'ws_zone.zone_id = pets.pet_city');
     $this->db->join('users', 'users.user_id = pets.user_id');
     $this->db->join('contact_info', 'contact_info.contact_info_id = pets.contact_info_id');

     if( !empty($pet_status) || !empty($pet_type) ) {
       if( isset($pet_status) ){
         $pet_data['pet_status'] = $pet_status;
       }

       if( isset($pet_type) ){
         $pet_data['category_id'] = $pet_type;
       }

       $query = $this->db->get_where('pets', $pet_data);
       return $query->result_array();
     } else {

     $query = $this->db->get('pets');
     return $query->result_array();
   }

 }

Pets controller
Code:
 public function index(){

   $pet_status = $this->input->get('petStatus');
   $pet_type = $this->input->get('petType');

   $data['pets'] = $this->pet_model->get_filtered_pets($pet_status, $pet_type);

   $this->load->view('templates/header', $data);
   $this->load->view('pets/index', $data);
   $this->load->view('templates/footer', $data);
 }

The only problem now is Jquery beacause i can't pass multiple values in url string but it replaces the old ones. If you have any solutions on that please feel free to tell me.
Code:
 $('#filters select').change(function(){
     let filters = [];
     $('#filters select').each(function(){
         const string = $(this).attr('id');
         const value = $(this).val();
         if(value !== '') filters.push(string + '=' + value);
     })
     window.location.search = filters.join('&')
 });

Any suggestions on this particular filtering function are welcomed!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.