• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtering Database Results

#1
[eluser]mikeshima[/eluser]
Hi guys,

I'm trying to create a filter screen to include a where statement depending on the users form input but I'm struggling to set an arrays value within the model - it sounds very trivial I feel stupid asking.

Controller Code
Code:
public function filterLinks($param = 'form')
    {    
        if ($param == 'form') {
            $this->load->helper('form');
            $this->load->model('paypal_model');
            
            $data = array('paypallinks' => $this->paypal_model->filterResults());
            $template = array(
                'title' => 'Filter Paypal Links',
                'content' => $this->load->view('paypal', $data, TRUE)
            );
            
            $this->load->view('template', $template);
        }

        elseif ($param == 'submit') {
            $this->load->model('paypal_model');
                    
            if ($this->paypal_model->filterPref()) {
                redirect('paypal/filterLinks/form');
            } else {
                redirect('paypal');
            }
        }
    }

Model Code


Note: the value $filterArray is created in the class outwith the constructor
Code:
class Paypal_Model extends Model
{                        
    public $filterArray;
Code:
public function filterPref(){    
        $this->filterArray = array(
            'sortStatuses' => trim($this->input->post('sortStatuses')),
            'sortStaff' => trim($this->input->post('sortStaff')),
            'sortClient' => trim($this->input->post('sortClient')),
            'sortDisplay' => trim($this->input->post('sortDisplay'))
        );
        
        if ($this->filterArray['sortDisplay'] < 1){
            $this->session->set_flashdata('message',
            '<p class="error">You must display more than one result.</p>');
            
        }else{        
            $this->session->set_flashdata('message',
             '<p class="success">Links have been sorted.</p>');                    
            return true;
        }
        return false;
    }
    
    public function filterResults(){
        if($this->filterArray['sortDisplay'] > 1)
        {
            $this->db->select('linkmanager.*, users.name as staffname');
            $this->db->join('users', 'users.id = linkmanager.linkby', 'left');
            $paypallinks = $this->db->get('linkmanager');
            return $paypallinks->result();        
        }
    }

The problem I'm having is that once the user hits submit on the form it doesn't store the variable of that has been passed in, in the array filterArray. Yet strangely, the error messaging for when there is less than one result being displayed works fine. I'm having a very sore head from this code - any help would be appreciated.

Thanks

#2
[eluser]Georgi Budinov[/eluser]
In order to use post data from the model perhaps try using the $_POST array or get instance to the controller and use the input class as you use it now.

But it is better practice to catch the array from the controller and pass it to the model.

#3
[eluser]mikeshima[/eluser]
Hi Georgi,

Thanks for the quick reply, how do I catch the array from the controller? I was under the impression you could only do it from the model.

#4
[eluser]Georgi Budinov[/eluser]
Read this - http://ellislab.com/codeigniter/user-gui...input.html

Currently you are catching the post variables the right way but from the model as I can see. Should be the controller.

#5
[eluser]mikeshima[/eluser]
Thanks a lot Georgi, after some hacking I got it to work Smile

Thanks again.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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