• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search results saved to a session for pagination

#1
[eluser]whiteae[/eluser]
I am having trouble with my pagination links due to multiple ways to look up items. I know I need the results in a session so that the links remain the same when clicked on. Here is my code.. It has 3 ways to display information. If no parameters, it displays all items, if parameters entered, then it displays items based on results, and lastly if the search input feature is used and the form is submitted, it displays the items based on that result. I'm new to this, so that might not be the best way to do things. I've had to change a lot without knowing exactly what to do at the beginning, so that's how I did it. Anyway I just need to know how I should go about this with saving the data according to how the user searched. Thanks in advance

Controller
Code:
function index ($brandAAIAID = '', $partNumQueryString = '')
   {    
       //If parameters passed through URI
       if($brandAAIAID != '')
       {
           //$session_data = array(
            //'partNumLookup' => $partNumQueryString,
            //'brandAAIAID' => $brandAAIAID
           // );
           //$this->session->set_userdata($session_data);

            $data['columns'] = $this->item_list->column_names();
            $data['brandName'] = $this->brand_label_model->getBrand();
            $config['per_page'] = 30;


            if($partNumQueryString == '')
                {
                    $numRows =$this->search_model->getRowsBrandAAIAID($brandAAIAID);
                    $results = $this->search_model->searchBrandAAIAID($brandAAIAID, $config['per_page'],$this->uri->segment(3));
                    $data['items'] = $results;
                }
            else
            {
               $numRows =$this->search_model->getLookupRowsBoth($partNumQueryString, $brandAAIAID);
               $results = $this->search_model->searchLookupBoth($partNumQueryString, $brandAAIAID, $config['per_page'],$this->uri->segment(3));
               $data['items'] = $results;
            }

            // Load the model and perform the search
            $config['base_url'] = base_url() . "index.php/items/index";
            $config['total_rows'] = $numRows;
            $config['num_links'] = 5;
            $this->pagination->initialize($config);
            $data['links'] = $this->pagination->create_links();
            $data['brand']= urldecode($brandAAIAID);
            $data['partNum'] = urldecode($partNumQueryString);
            //load view
        $this->load->view('itemHome', $data);

       }//end if

        //Else if search results submitted.
       elseif($this->input->post('search'))
       {            
            //Save data to a session
           //$session_data = array(
               // 'partNum' => $search_terms,
               // 'brandLabel' => $brand_name,
               // 'per_page' => 30
           // );
          // $this->session->set_userdata($session_data);

           //Form validation
            $this->form_validation->set_rules('q', 'Search Criteria', 'trim|strip_tags');

            //Variables
            $search_terms = $this->input->get_post('q');
            $brand_name = $this->input->get_post('brandLabel');
            $config['per_page'] = 30;

            if($search_terms == '')
            {
                $numRows =$this->search_model->getRowsBrand($brand_name);
                $results = $this->search_model->searchByBrand($brand_name, $config['per_page'],$this->uri->segment(3));
                $data['items'] = $results;
            }
            else
            {
                $numRows =$this->search_model->getRowsBoth($search_terms, $brand_name);
                $results = $this->search_model->search($search_terms, $brand_name, $config['per_page'],$this->uri->segment(3));
                $data['items'] = $results;
            }//end else

             //Pagination
            $config['base_url'] = base_url() . "index.php/items/index";
            $config['total_rows'] = $numRows;
            $data['columns'] = $this->item_list->column_names();
            $data['brandName'] = $this->brand_label_model->getBrand();
            $config['num_links'] = 5;
            $this->pagination->initialize($config);
            $data['links'] = $this->pagination->create_links();
            //load view
        $this->load->view('itemHome', $data);
        }//end if
        //Else display all items on page.
        else
        {
            $numRows =$this->item_list->get_rows();
            $config['base_url'] = base_url() . "index.php/items/index";
            $config['total_rows'] = $numRows;
            $config['per_page'] = 30;
            $config['num_links'] = 5;
            $this->pagination->initialize($config);
            $data['links'] = $this->pagination->create_links();
            $data['columns'] = $this->item_list->column_names();
            $data['brandName'] = $this->brand_label_model->getBrand();
            //To paginate items
            $data['items'] = $this->item_list->get_items($config['per_page'],$this->uri->segment(3));
            //load view
        $this->load->view('itemHome', $data);
        }//end else

        
   }//end function

#2
[eluser]whiteae[/eluser]
I need to figure this out today if anyone can help. Any feedback or insight would be great. Thanks.

I have updated my code so that it might be a little better to understand what I'm trying to do.

Code:
function index ($brandNameQueryString = '', $partNumQueryString = '')
   {    
       //Save parameters into session
           $session_data = array(
                'brandLabel' => urldecode($brandNameQueryString),
                'partNum' => $partNumQueryString
           );
           $this->session->set_userdata($session_data);

            //Save per page configuration for pagination.
            $config['per_page'] = 30;

            //if search results submitted.
           if($this->input->post('search'))
           {
               //Form validation
                $this->form_validation->set_rules('q', 'Search Criteria', 'trim|strip_tags');

                //Variables
                $search_terms = $this->input->get_post('q');
                $brand_name = $this->input->get_post('brandLabel');
                $config['per_page'] = 30;

                if(isset($brand_name) && $search_terms == '')
                {
                    $numRows =$this->search_model->getRowsBrand($brand_name);
                    $results = $this->search_model->searchByBrand($brand_name, $config['per_page'],$this->uri->segment(3));
                    $data['items'] = $results;
                }//end if
                elseif(isset($brand_name) && isset($search_terms))
                {
                    $numRows = $this->search_model->getRowsBoth($search_terms, $brand_name);
                    $results = $this->search_model->search($search_terms, $brand_name, $config['per_page'],$this->uri->segment(3));
                    $data['items'] = $results;
                }//end elseif
           }//end if
            elseif($this->session->userdata('brandLabel') != '')
            {
                //If only brand name supplied, search by brand, else search by both brand name and part number.
                if($this->session->userdata('partNum') == '')
                {
                    $numRows =$this->search_model->getRowsBrand($this->session->userdata('brandLabel'));
                    $results = $this->search_model->searchByBrand($this->session->userdata('brandLabel'), $config['per_page'],$this->uri->segment(3));
                    $data['items'] = $results;
                }
                else
                {
                   $numRows =$this->search_model->getRowsBoth($this->session->userdata('partNum'), $this->session->userdata('brandLabel'));
                   $results = $this->search_model->search($this->session->userdata('partNum'), $this->session->userdata('brandLabel'), $config['per_page'],$this->uri->segment(3));
                   $data['items'] = $results;
                }
            }//end elseif
            else
            {
                $numRows =$this->item_list->get_rows();
                $data['items'] = $this->item_list->get_items($config['per_page'],$this->uri->segment(3));
            }//end else
          
             //Pagination
            $config['base_url'] = base_url() . "index.php/items/index";
            $config['total_rows'] = $numRows;
            $data['columns'] = $this->item_list->column_names();
            $data['brandName'] = $this->brand_label_model->getBrand();
            $config['num_links'] = 5;
            $this->pagination->initialize($config);
            $data['links'] = $this->pagination->create_links();
            //load view
            $this->load->view('itemHome', $data);
              
   }//end function

#3
[eluser]pickupman[/eluser]
Looks like you are pretty close. I few recommendations that I have found when doing something similar. Don't be afraid to put the logic into different methods. I found myself early on trying to lump several page functions all into one massive method. Even putting them into private methods and calling them from the index() method can clean things up a bit. This may seem weird now, but when you come back to code you wrote a year ago, you will wonder what all the if() statements are.

1. Change base_url to:
Code:
//Pagination
$config['base_url'] = site_url("items/index"); // Make life easier on yourself in the future

2. You need to save the search term in your session. That way when you click a paginated link, and go to the next page, the code will skip that code block. You need to check if the search term exists in session or the posted value. With your code logic, this is why the search should be its own method. You can't determine if the user is still searching or viewing all rows.

#4
[eluser]Unknown[/eluser]
Like this i am having 6 values are coming from view to controller function,after that based on values i am doing search like this
//code starts//if($A != null && $b == null && $c == null && $d == null && $e == null && $f == null )
{
if($A == 'Any'){
$this->load->My_Model->seach_itemswithany();}
else{$this->load->My_Model->seach_itemsall($any)}
}
Like this i am getting the values from db,total 240 if and elseif loops i written for the differrent pages,so how can i put pagination for this based on conditions?
HELP ME OUT!

#5
[eluser]pickupman[/eluser]
@Anudeep
Check out some of the methods in the [url="http://codeigniter.com/nightly_user_guide/libraries/uri.html"]URI Class[/url] for help building the uri's for your pagination links. You will need to create a url with the segments of your search along with the pagination offset.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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