Question:
Sorting PHP MySQL table by links?
christopher wood
2010-03-31 06:47:07 UTC
I am currently learning php, and have built a website. I have a page that grabs my MySQL table that I have created for books and it lists them. I want to be able to sort my table by title, author or status. I would love some help on it. My page that I'm working on is:http://www.jaztitch.solidwebhost.com/books.php
Here's the query and result coding: $query = "SELECT * FROM Books ORDER BY " . $sort;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo ''.$row["title"]." "."".$row["author"]." "."".$row['number']."".$row["status"];
echo"";
}
Four answers:
Web Programmer
2010-03-31 08:31:26 UTC
In your table heading, make your Title, Author and Status linked like this:

(I am assuming your table column names are title, author and sort, if not use the column values)



Title



Author



Status



Then in your code, add this:





// default sort to some column

$sort = 'title';



// if sort has been passed in query string

if ($_GET['sort']) $sort = $_GET['sort'];



$query = "SELECT * FROM Books ORDER BY " . $sort;



$result = mysql_query($query) or die(mysql_error());



while($row = mysql_fetch_array($result)){

echo ''.$row["title"]." ".""…

echo"";

}
?
2016-12-15 14:47:51 UTC
Sorting In Php Mysql
anonymous
2016-03-03 08:34:55 UTC
Could it be something to do with, you've closed your database when your halfway through using it, move mysql_close(); to the end xx
anonymous
2014-11-06 21:13:27 UTC
difficult issue. query in yahoo. that will help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...