Click here to Skip to main content
15,905,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, Below is my code. Once click the Title, Address bar of the browser will be changed to books.php?choice=title. My problem is I need to run sort this table from title, so I want to add sort title php code inside this single php file. I need like header = books.php?choice=title then run the code else below code. Please help me.


XML
<?php
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM authors a, books b, publish p where p.authorid=a.authorid and p.bookid=b.bookid");

echo "<table border='1'>
<tr><th colspan='7'><a href=''>Add Book</a> |
<a href=''>Add Author</a></td></tr>
<tr>
<th><a href='books.php?choice=title'>Title</a></th>
<th><a href=''>Author Name</a></th>
<th><a href=''>Genre</a></th>
<th><a href=''>Country</a></th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['fname'] . "</td>";
  echo "<td>" . $row['genre'] . "</td>";
  echo "<td>" . $row['country'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>
Posted

1 solution

Here are two options to consider:

1. You can use $_GET['choice'] to get the sorting value, then pass that to your SQL statement.

ex. (assuming you have good filtering on $_GET):
PHP
$sort = $_GET['choice'];

$result = mysqli_query($con,"SELECT * FROM authors a, books b, publish p where p.authorid=a.authorid and p.bookid=b.bookid ORDER BY $sort");


I would recommend moving your HTML output to a separate file and using AJAX to run your query and create the table, but if you have to have it all in one file, this should still work.

2. If you can use JavaScript, use a script to sort your table. I use jQuery in my projects, and have found the Tablesorter jQuery plugin[^] to be very helpful for these kinds of tables.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900