Jump to content

[SOLVED] Sorting Tables by different columns


almystersv

Recommended Posts

Hi Guys,

 

I have this table in place that looks at the previous messages that have been left by other users.

 

At the moment is automatically sorted by date. But I was wondering if it was possible to allow the user to click on the header of each column which would then sort the table by that column.

 

For example... If it had the headings Date, username, Input Type etc.. then if they were to click on the heading Input Type it would sort all the records in that table by the input type!

 

Here is my code for the message page...

 

<?php
session_start();
if (isset($_SESSION['username']) == false){
	header("Location: login.php");
	exit();
}
require "connect.php";
?>
<!DOCTYPE HTML PUBLIC "//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>BIS Portal</title>
</head>
<body>
<?php 
include ("header.php"); 
?>
<p>Messages</p>
</h2>
<table align="right" border="1" name="Messages Table" id="1" tabindex="1" color ="#1E1EC8" width="100%">
<tr>
         <th width="8%" align="left" ><h3>Message Number</h3></th>
	 <th width="21%" align="left" ><h3>Title</h3></th>
         <th width="42%" align="left" ><h3>Content</h3></th>
         <th width="15%" align="left" ><h3>Date</h3></th>
         <th width="14%" align="left" ><h3>User</h3></th>
</tr>
<?php
$query = "SELECT * FROM message, employee WHERE message.empID = employee.empID ORDER BY messageID";
$result = mysql_query($query, $connection)
or die ("MySQL Error: ".mysql_error());
while($row = mysql_fetch_array($result))
{ 
?>
<tr>
        <td align="center"><?php echo $row['messageID']?></td>
	<td><?php echo $row['msgTitle']?></td>
	<td><?php echo $row['msgBody']?></td>
	<td><?php echo $row['msgDate']?></td>
	<td><?php echo $row['fName']?> <?php echo $row['sName']?></td>
</tr>
<?php } ?>
</table>
</h4>
</body>
</html>

 

Cheers  :o

Link to comment
Share on other sites

You get the variables into the array by way of the URL

 

say your page address is www.mysite.com/page.php

 

add a ? then a variable name say var, then = and then the value you want to assign that variable...

 

So www.mysite.com/page.php?var=user would be a case of this...

 

now, in your code $orderedby = $_GET['var'];

 

$orderedby will now contain the value "user" or whatever is following the = sign in your url.

 

So, essentially you will want to build the column headers as links.  so for each link it will be the same except for the variable at the end.

 

your query will then use that variable in it's order by section to determine what to order the query by.

 

so where you have "ORDER BY messageID"  you would make it "ORDER BY $orderedby" and the value of your variable will change the way your data is sorted.

Link to comment
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.