CodeIgniter Forums
Forms and Pagination Quick fix - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Forms and Pagination Quick fix (/showthread.php?tid=36959)



Forms and Pagination Quick fix - El Forum - 12-21-2010

[eluser]Richievc[/eluser]
Hi all after a few ways of trying to add form post functionility to the Pagination I found that this is the best for me.

So here we go Smile

First you want to extend the Pagination Class

First Method
Code:
function get_query_segments() {
  # TAKE OUT SYSTEM VARS
  $segments = "";
  $curr_vars = "";
  $curr_vars = $curr_vars.",hash_id,email,resultpage";
  if($_POST) {
    foreach($_POST as $key=>$val) {
      if($key=="search"||$key=="PHPSESSID"||$key=="ci_session") { } else {
    $segments.= "/".$key.":".$val;
    }
     }
  } else {
    foreach($this->CI->uri->segments as $key=>$val) {
    if($key<4) { } else {
          $segments.= "/".$val;
    }
    }
  }
     return $segments; // this is your new created segment string
}

This Method add a Value pairs to the segments ie. class/method/20/name:richie/

Next: due to limit space I'll post only the changes of this method
extending the create_links() Method
Find this and add the 0 This ensure the 3 segment is occupied
Code:
$n = ($i == 0) ? '0' : $i; || old n = ($i == 0) ? '' : $i;

For the links change to this it add the segment pairs to the end of each link
Code:
$this->base_url.$n.$this->get_query_segments()

Next we need to extend the Form_validation Class we added this method
Code:
function check_segments($fields) {
   foreach($this->CI->uri->segments as $key=>$val) {
      if($key<4) { } else {
        $ex_field = explode(":",$val);
    if($ex_field[0]==$fields) {
           if(empty($ex_field[1])) return false;
           else return true;
    }
     }
  }
}
Check to see if the segments value pair is there and if the second key is not empty return ture or false
Method 2
Code:
function segments($fields) {
   if(@$_POST[$fields]) {
    return @$_POST[$fields];
   } else {
    foreach($this->CI->uri->segments as $key=>$val) {
           if($key<4) { } else {
              $ex_field = explode(":",$val);
              if($ex_field[0]==$fields) {
                 if(empty($ex_field[1])) return;
              else return $ex_field[1];
              }
           }
       }
    }
}
return the value of the segment pair

method 3
Code:
function set_value($field = '', $default = '') {
            if($this->check_segments($field)===true) {
                return $this->segments($field);
            } else {
                if ( ! isset($this->_field_data[$field])) {
                    return $default;
                }
                return $this->_field_data[$field]['postdata'];
            }
        }
helps repopulate Form if post or clicked linked (Still having some trouble with this so any help is cool) Repops on click but not post
Query Usage
Code:
if(!empty($this->form_validation->segments('name'))) {
#do something
}

Please let me know what you think or if there is a better way thx