Help to flip $_GET values with ASC/DESC (pagination)
Posted: Mon Oct 16, 2006 6:10 am
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!
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( );