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

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
public function filterLinks($param = 'form')
        if ($param == 'form') {
            $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') {
            if ($this->paypal_model->filterPref()) {
            } else {

Model Code

Note: the value $filterArray is created in the class outwith the constructor
class Paypal_Model extends Model
    public $filterArray;
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){
            '<p class="error">You must display more than one result.</p>');
             '<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.


[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.

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.

[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.

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.