Jump to content

Recommended Posts

So I am sure you know how when you click a header on a column of data it sorts from largest to smallest? Then you want to click the column header again and sort the opposite direction, smallest to largest.

 

I am using MySQL obviously, and currenly I have my data sorted from largest numbers to smallest. However if you click the column header again, I want it to sort the opposite direction.

 

Here is the code I have that sorts DESC

 

$page_name="1st_downs.php"; //  If you use this code with a different page ( or file ) name then change this 

@$column_name=$_GET['column_name']; // Read the column name from query string. 


$start=$_GET['start'];								// To take care global variable if OFF
if(!($start > 0)) {                         // This variable is set to zero for the first page
$start = 0;
}

$eu = ($start - 0); 
$limit = 100;                                 // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 


/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2=" SELECT * FROM 1st  ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////

/////////// Now let us print the table headers ////////////////
echo '<div id="main" class="text">';

echo '<table>';
echo 	"<tr>";
echo 	"<td><a href='$page_name?column_name=quarterback'>Quarterback</a></td>";
echo 	"<td><a href='$page_name?column_name=completions'>Completions</a></td>";
echo 	"<td><a href='$page_name?column_name=attempts'>Attempts</a></td>";
echo 	"<td><a href='$page_name?column_name=comp_percent'>Comp%</a></td>";
echo 	"<td><a href='$page_name?column_name=yards'>Yards</a></td>";
echo 	"<td><a href='$page_name?column_name=ypa'>YPA</a></td>";
echo 	"<td><a href='$page_name?column_name=touchdowns'>Touchdowns</a></td>";
echo 	"<td><a href='$page_name?column_name=td_percent'>TD%</a></td>";
echo 	"<td><a href='$page_name?column_name=interceptions'>Interceptions</a></td>";
echo 	"<td><a href='$page_name?column_name=int_percent'>INT%</a></td>";
echo 	"<td><a href='$page_name?column_name=rating'>rating</a></td>";
echo	"</tr>";

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM 1st";

if(isset($column_name) and strlen($column_name)>0){
$query = $query . " order by $column_name";
}

$data=mysql_query($query);
echo mysql_error();

//////////////// Now we will display the returned records in side the rows of the table/////////
while($row = mysql_fetch_array($data))
{
echo '<tr><td>' .$row['quarterback'] .'</td>';
echo '<td>' .$row['completions'] .'</td>';
echo '<td>' .$row['attempts'] .'</td>';
echo '<td>' .$row['comp_percent'] .'%</td>';
echo '<td>' .$row['yards'] .'</td>';
echo '<td>' .$row['ypa'] .'</td>';
echo '<td>' .$row['touchdowns'] .'</td>';
echo '<td>' .$row['td_percent'] .'%</td>';
echo '<td>' .$row['interceptions'] .'</td>';
echo '<td>' .$row['int_percent'] .'%</td>';
echo '<td>' .$row['rating'] .'</td></tr>';
}
echo "</table>";

 

Thanks for any help..

Link to comment
https://forums.phpfreaks.com/topic/89572-sort-desc-then-asc/
Share on other sites

Its quite simple. You need to make your sort link rotate between desc and asc and pass this back to your query. eg;

 

<?php

  if (isset($_GET['sort'])) {
    $sql = "SELECT * FROM foo ORDER BY " . strtoupper(mysql_real_escape_string($_GET['sort']));
    if ($_GET['sort'] == 'desc') {
      $sort = 'asc';
    } else {
      $sort = 'desc';
    }
  } else {
    $sql = "SELECT * FROM foo ORDER BY DESC";
  }

  // execute your query.

  // display link.
  echo "<a href='?sort=$sort'>$sort</a>";

?>

Link to comment
https://forums.phpfreaks.com/topic/89572-sort-desc-then-asc/#findComment-458872
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.