Help to flip $_GET values with ASC/DESC (pagination)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Help to flip $_GET values with ASC/DESC (pagination)

Post by mcccy005 »

Alrighty, what I have is a set of results, and each column has a title which is a URL back to the current web page; except the paramaters parsed back to the current web page are dependent upon the current column.
So, the paramaters parsed back to each page are "column" (ie. which column the results are currently ordered based on); order (ie. "ASC" or "DESC" - whether the results in the particular column are ordered based on ascending or descending order); num_results (number of results to be displayed per page); and current_page (the current page of the list of results...eg. 1 of 3 or whatever)

So if $_GET['column']==0 and $_GET['order']=="ASC"; then flip it so that where $_GET[column]==0, then $_GET['order']=="DESC" and for all other columns; automatically default the 'order' paramater to "ASC".

The thing is; this works fine for column 0 - each time I click on the title; it flips from "ASC" to "DESC" and vice versa. However it doesnt work with columns 1 onwards. (BTW. column 0 is the first column; column 1 is the second etc.....just like an array).
Here is the code (sorry its a bit lengthy); but any advice would be appreciated.....you can only change so many things and look at the same code for so many hours before you have to pass it on for someone else to look at!!
Thanks

PS. $_GET['column'] changes with each column as it should....I just cant get ASC/DESC to flip properly!

Code: Select all

public function __construct($titles, $db_column_names, $sql_query, $db_connection)
{	
	$this->titles=$titles;
	$this->db_column_names=$db_column_names;
	$this->sql_query=$sql_query;
		
	//Stores the URL of the page the links of the results point toward. (Ie. houseboats.php?housboat_id
	$url;
		
	/*====================================================================================================================================
		*  INITIATES VARIABLES (STORED IN $_GET[ ])  TO DEFAULT VALUES IF THEY HAVE NOT BEEN PREVIOUSLY SET
		*  ====================================================================================================================================*/			
	/* Stores the current page in terms of the total number of results. (Eg. page 2 (of 3) ) 	*/
	if(@!isset($_GET['current_page'])) 
		$current_page=1;
	else $current_page=$_GET['current_page'];
			
	/* Stores the number of results to be displayed per page */
	if(@!isset($_GET['num_results']))
		$num_results=15;
	else $num_results=$_GET['num_results'];
						
	/*stores the column number to be sorted */
	if(@!isset($_GET['column'])) 
		$column=0;
	else $column=$_GET['column'];
				 		
	/*stores whether the results are to be displayed in ascending or descending order */
	if(@!isset($_GET['order']))
		$order="ASC";
	else $order=$_GET['order'];
			
	/*Sets the LIMIT attributes of the SQL Query in order to limit selecting the appropriate
	results based on the current page to be displayed and the number of results per page to
	be displayed */
	$LIMIT=($num_results*$current_page) - $num_results;
	$LIMIT.=", ".$num_results;
		
	/*Sets the ORDER BY attributes of the SQL Query in order to determine which column and in
	which order (ASC or DESC) the results are displayed in */
	$ORDER_BY=$this->db_column_names[$column];
	$ORDER_BY.=" ".$order;
	/*Adds the ORDER BY and LIMIT attributes to the sql query */
	$this->sql_query.=" ORDER BY {$ORDER_BY}
			LIMIT {$LIMIT}";

		/*====================================================================================================================================
		*  CREATES A RESULT SET TO BE DISPLAYED TO THE CURRENT WEB PAGE WHICH IS BASED ON THE CURRENT RESULTS TO BE DISPLAYED;
		*  THE NUMBER OF RESULTS TO BE DISPLAYED PER PAGE; AND THE SQL QUERY WITH THE ACCOMPANYING DATABASE CONNECTION
		*  ====================================================================================================================================*/
					
	$result=$db_connection->query($this->sql_query); 
	$temp_cells_array=array( ); 
	$temp_fields_array=array( ); 
				
		/*Gets the data from each row of the results from the query and parses each row as a new result_field object into a result_set object
		The elements in the first column (ie. where $j==0) are ignored as this represents the houseboat_id which will be used to link 
		to the houseboats.php page and parse the houseboat_id via the $_GET method in the URL. */
	for($i=0; $i<=$result->num_rows; $i++) 
	{ 
		//Fetches the data from each row of $result. Skips the first row as this is the title row.
		if($i!=0)
		$row=$result->fetch_row( ); //Gets an index array of each row
		 /*Traverses throuch each column of $row, and creates a new result cell for each column and adds the results
		 to a temporary result_field array.
		Also gets the data type from the current colum*/
		 for($j=0; $j<sizeof($this->titles); $j++)
		 { 		
			/*If this is the very first row; then a title is created for each column.
			The URL of each title is set so that the current web page will be called and the new results will be
			ordered based on ascending order unless they have already been sorted in ascending order in which
			case they will be called based on descending order. */
			if($i==0)
			{				
				echo "COlumn=".$column." and J=".$j."<br>";
				/*If the results have been sorted on a particular column, and $j currently points to that column
				then flip the current order from "ASC" to "DESC" or vice versa. */
//PRETTY SURE ERROR IS JUST AFTER THIS LINE..........
 	if((int)$column==$j)
				{	
					if($order=="ASC")
						$this->order="DESC";
					else $this->order="ASC";
				}
				/*If the current column has not been currently sorted; then default the sort type (ie. $order)
				to "ASC" */
				else
				{
					$this->order="ASC";
	//COMMENT THIS OUT AND IT WORKS!!!!!!$this->column=$j;
				}
				
				//NOTE: TEST IF THERE IS A NEXT PAGE
				
				//Gets the URL of the current webpage
				$current_url=get_host_url( );					
					
				//Appends the variables 'order' and 'column' to the current URL
				$current_url.="?current_page=".$current_page.
							"&num_results=".$num_results.
							"&column=".$j.
							"&order=".$order;
									
				//Adds a new result_cell object to the temp_cells_array
				$temp_cells_array[$j]=new result_cell($this->titles[$j], $current_url);
		//Sets the variable $is_title to true; which is to be parsed to a result_field object specifying that this first row is a title field
				$is_title=true;
			}
			else
			{
				//Adds a new result_cell object to the temp_cells_array
				$temp_cells_array[$j]=new result_cell(stripslashes($row[$j]));
		//Sets the variable $is_title to false; which is to be parsed to a result_field object specifying that this first row is a title field
				$is_title=false;
				$url="housboats.php?id=".$row[0];
			}
		 }
			 
		/*Selects the background colour of each row so that each row alternates in colour */ 
		if($i%2==0) $row_color="#6699FF";
		else $row_color="#F0F8FF";
			
	/*Adds the array of data cells (ie. a row) to a temp_fields_object. If this is the first row; then $url is left blank is a URL is already
			allocated to each result_cell object in the title */
		if($i==0)
			$temp_fields_array[$i]=new result_field($temp_cells_array, null, $row_color, $is_title);
		else $temp_fields_array[$i]=new result_field($temp_cells_array, $url, $row_color, $is_title);				 
	}

/*Adds the array of result_field objects to a new result_set object; and then calls the display method
		of the result_set object to create and display a table of results. */
		$result_set=new result_set($temp_fields_array);
		$result_set->display( );
Last edited by mcccy005 on Mon Oct 23, 2006 1:44 am, edited 2 times in total.
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Ok; I've changed the variables $order, $column etc etc. so that they are outside of the declared constructor and I'm now accessing them all using $this->order etc etc.

So now the ASC/DESC works on the first column only (ie. where column==0) and it will change from ASC to DESC and vice versa as it should; but it doesnt work for any subsequent columns (ie. where column >= 1).
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

This was up a couple weeks ago; but then I though I solved it.
Anyways, it still has an error in it.

If I have 2 columns (eg. Name and Age), the first column (Name) will work fine - ASC and DESC will flip forever no worries. However, with the second column (Age), I can flip from ASC to DESC but for some reason it won't flip it back to ASC....but yet it works for the first column!!

ANy ideas??
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Has anyone got ANY ideas at all??
I have a feeling the error is in this piece of code:

Note also that $column is a string value which i got from $_GET['column'] so I'm trying to use the type cast (int) for column to see if it is equal to $j. Maybe theres something dodgy with this?? But it works for where $column==0 and $j==0; but not for when $column==1 and $j==1.

Code: Select all

if($i==0) 
                        {                             
                                echo "COlumn=".$column." and J=".$j."<br>"; 
                                /*If the results have been sorted on a particular column, and $j currently points to that column 
                                then flip the current order from "ASC" to "DESC" or vice versa. */ 

                                 if((int)$column==$j) 
                                {        
                                        if($order=="ASC") 
                                                $this->order="DESC"; 
                                        else $this->order="ASC"; 
                                } 
                                /*If the current column has not been currently sorted; then default the sort type (ie. $order) 
                                to "ASC" */ 
                                else 
                                { 
                                        $this->order="ASC"; 
         
                                }
Post Reply