Jump to content

pagination using sort by "price" etc from Database


nade93

Recommended Posts

This is a good tutorial, even if you don't know about classes, by the end of this one you'll get a good idea at least;

http://www.talkphp.com/advanced-php-programming/1660-advance-pagination-class.html

 

What you would do is add a "Filter" method, and "Sort By" Method. These methods would be called before the setData() method call.

This would set the relevant "Member Properties" (Class Variables) so that setData can implement them in the SQL string.

 

eg: Modified setData Function;

            function setData($table){
                    
                  $this->table = $table;
                  
                  // For multiple field sorts/orders.. (They will be sorted right to left, most dominant sort field will be the first in the array.)
                  if(is_array($this->order) && count($this->order) != 0){
                                    $orders = array();
                                    
                                    
                                    // Loop each order, $order = array([0]=>array([0]=>'fieldname',[1]=>'asc/desc'),[1]=>array([0]=>'fieldname2',[1]=>'asc/desc'));
                                    foreach($this->order as $item){ // $item would be array([0]=>'fieldname',[1]=>'asc/desc')
                                                      $orders[] = "`".$item[0]."` ".$item[1];
                                    }
                                    $order = implode(", ", $orders);
                  }
                  
                  $this->sql = "SELECT * FROM ".$this->table." ".$sort." ".$order." LIMIT ".$this->limits.",".$this->max_r."";
                  $this->sql = mysql_query($this->sql) or die(mysql_error());
                  $this->total = "SELECT * FROM ".$this->table."";
                  $this->totalres =  mysql_query($this->total) or die(mysql_error());
                  $this->count_all = mysql_num_rows($this->totalres);
                  $this->totalpages = ceil($this->count_all / $this->max_r);
            } 

 

You will have to make the actually filter function that will make the order array (or add to it so you can call the method numerous times).

 

I'll leave that to you, but do this in order:

1. Read through that tutorial, implement that exact code to your current project.

2. Come and look at this code, and write it, dont copy/paste - fi you write it you may understand it easier.

3. Try to create your own filter method, Personally i would call it addFilter() but that's totally up to you. If you et stuck give don't hesitate to ask.

 

-cb-

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.