Name | mark |
Alex John | 55 |
Big John | 55 |
Krish Star | 60 |
John Mike | 60 |
Babby John | 69 |
John Deo | 75 |
Tade Row | 88 |
Gimmy | 88 |
Marry Toeey | 88 |
if(isset($by) and $by=="asc"){
$by="desc"; // Order is set to decreasing
}else{
$by="asc"; // Order is set to increasing
}
$query="SELECT * FROM `student` where class='Four' ORDER BY mark $by ";
$rt=mysql_query($query);
echo "<table border='0' width='300' cellspacing='0' cellpadding='0' align=center>";
echo "<tr><td><b>Name</b></td>
<td><b><a href='sql_order-change.php?by=$by'>mark</a></b></td></tr>";
while ( $nt=mysql_fetch_array($rt)){
echo "<tr><td>$nt[name]</td><td>$nt[mark]</td></tr>";
}
echo "</table>";
Note that the value of $by
we are setting before the query by an if - else logical condition and when ever user clicks the header link of the mark table the value of $by changes and the order by query also changes. The value of $by gets either 'asc' or 'desc'.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
Erkki | 15-03-2014 |
How do you get rid of double or triple and so on "by=asc" "by=desc" lines on url line in the browser? Whenever you implement it in your own code the url line just generates more variables "by=asc&by=desc" an so on. |
phraglets | 03-02-2015 |
do you have a sample of dynamic table that can display all the table in the database even if their field are different, with pagination |
smo | 06-02-2015 |
You can download the plus admin script . Or check this page to list tables |