Hello,

I've been racking my brain all week with this code. I'm displaying a help desk table, and I want the column headers to be clickable, which will ultimately sort the that column both ascending and descending.

I'm sure I'm just missing a step or so, but cannot seem to put my finger on it. I don't want to use arrays, nor any sort of javascript, there has to be a way to get this done in PHP.

THANK YOU in advance!

<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
$today = date("Y-m-d");
$today2 = date("D M j g:00 A T Y"); 
$currency = '$';

if ($_GET['direction']== "ASC")
	{
	$sortby = "DESC";
	}
else
	{
	$sortby = "ASC"; 
	}

$query = '
	SELECT it_jobs.srno,
		   it_jobs.jobid,
		   it_jobs.descr,
		   it_jobs.status,
		   it_jobs.submitteddate,
		   it_jobs.closeddate,
		   it_jobs.category,
		   it_category.category,
		   it_category.catgroup,
		   it_jobs.pri,
		   it_jobs.submittedby,
		   it_jobs.resp1
	  FROM it_jobs
 LEFT JOIN it_category ON it_jobs.category = it_category.catid
 LEFT JOIN it_comments ON it_jobs.jobid = it_comments.jobid
  GROUP BY it_jobs.jobid ORDER BY "$sortby"
			';

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

	echo "<table width='99%' cellspacing='3' cellpadding='3'>
			<tr>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=it_jobs.srno&direction=$sortby'>Request No.</a></td>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=descr&direction=$sortby'>Issue Title</a></td>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=submittedby&direction=$sortby'>Submitter</a></td>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=submittedby&direction=$sortby'>IT Assignee</a></td>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=submittedby&direction=$sortby'>Issue Type</a></td>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=submittedby&direction=$sortby'>Category</a></td>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=submittedby&direction=$sortby'>Date Created</a></td>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=submittedby&direction=$sortby'>Priority</a></td>
				<td class='title' align='left'><a href='itServiceTickets3.php?sortbycolumn=submittedby&direction=$sortby'>Status</a></td>
			</tr>";
				
	while ($row = mysql_fetch_assoc($result))
		{
		$jobnumber = $row['srno'];
		$jobdescription = $row['descr'];
		$jobstatus = $row['status'];
		$submitteddate = $row['submitteddate'];
		$start = substr($submitteddate,5,2) . '/' . substr($submitteddate,8,2) . '/' . substr($submitteddate,2,2);
		$closeddate = $row['closeddate'];
		$category = $row['category'];
		$catgroup = $row['catgroup'];
		$priority = $row['pri'];
		$submittedby = $row['submittedby'];
		$categoryid = $row['resp1'];
		
		echo "
			<tr>
				<td width='10%'>" . $jobnumber . "</td>
				<td width='10%'>" . $jobdescription . "</td>
				<td width='10%'>" . $submittedby . "</td>
				<td width='10%'>    it assignee  </td>
				<td width='10%'>" . $catgroup . "</td>
				<td width='10%'>" . $category . "</td>
				<td width='10%'>" . $start . "</td>
				<td width='10%'>" . $priority . "</td>
				<td width='10%'>" . $jobstatus . "</td>
			</tr>";			
		}
		
		echo "</table>";
	?>

</body>
</html>

Hello,

I see that your query says

"ORDER BY '$sortby'" - you aren't defining a sort column here; just either ASC or DESC.

You'll need to have a variable for a sort column also:

$column=$_GET["sortbycolumn"];
//then alter your (very complex) query to be like this
....ORDER BY '$column' '$sortby'"

Try that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.