Link to home
Start Free TrialLog in
Avatar of Jack Andrews
Jack AndrewsFlag for United States of America

asked on

How to fix PHP sort issues in a table

Goal: Toggle sorting a column of dates in descendingascending order including the entire row in a table. The in-page PHP currently sorts some columns. We need to add sorting for all columns in the table. I tried applying the same input tag format to two columns that were not set to sort. Specifically, the Commissions and Referrals columns will not sort when applying the same input tag format as other columns that sort correctly. Both buttons throw the following error: Unknown column 'Agent_Commission' in 'order clause'
agent_page8-test.php
Avatar of lenamtl
lenamtl
Flag of Canada image

Hi,
Are you using Datatables or other similar JS script for the table?
If so, you will need to add the sort value in the JS script as it will overide the sort from the DB query.
(I usually don't use any ORDER BY in the mysql query)

Aslo some table script can break the sort if you are using data-attribute
For example Datatables is having specific sort ordering that won't work if there is a data-attribute.

Check if you have any JS error in console (in browser right-click inspect).
Avatar of Jack Andrews

ASKER

The person who wrote the PHP passed away. I am not a coder, but this is the only JS linked to the page. I see nothing about tables:
// JavaScript Document
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

userAgent = window.navigator.userAgent;
browserVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);
mustInitImg = true;

function initImgID() {
	di = document.images;
	if (mustInitImg && di) 
	{
		for (var i=0; i<di.length; i++) 
		{
			if (!di[i].id) di[i].id=di[i].name; 
		}
		mustInitImg = false;
	}
}

function findElement(n,ly) {
	d = document;
	if (browserVers < 4)		return d[n];
	if ((browserVers >= 6) && (d.getElementById)) {initImgID; return(d.getElementById(n))}; 
	var cd = ly ? ly.document : d;
	var elem = cd[n];
	if (!elem) {
		for (var i=0;i<cd.layers.length;i++) {
			elem = findElement(n,cd.layers[i]);
			if (elem) return elem;
		}
	}
	return elem;
}

function changeImages() {
	d = document;
	if (d.images) {
		var img;
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			img = null;
			if (d.layers) {img = findElement(changeImages.arguments[i],0);}
			else {img = d.images[changeImages.arguments[i]];}
			if (img) {img.src = changeImages.arguments[i+1];}
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		over_aboutus_03 = newImage(/*URL*/'/images/aboutus-over_03.gif');
		over_quote_04 = newImage(/*URL*/'/images/getquote-over_04.gif');
		over_services_05 = newImage(/*URL*/'/images/services-over_05.gif');
		over_contact_06 = newImage(/*URL*/'/images/contact-over_06.gif');
		over_faq_07 = newImage(/*URL*/'/images/faq-over_07.gif');
		preloadFlag = true;
	}
}

Open in new window


I posted the test page here, which may be a little different from what I uploaded: https://www.abmoving.com/Agents/agent_page8-test.php
Let's look at:
<form id="form1" name="form1" method="post" action="">

Open in new window

and
							<td width="50">Select</td>
							<td width="200"><input id="idProperty" name="SortProperty" class="mainForm" type="submit" value="Property" /></td>
							<td width="150"><input id="idAgent" name="SortAgent" class="mainForm" type="submit" value="Agent" /></td>
							<td width="150"><input id="idEmail" name="SortEmail" class="mainForm" type="submit" value="Email" /></td>
							<td width="75"> <input id="idCommission" name="SortCommission" class="mainForm" type="submit" value="Commission" /></td>
							<td width="75"> Payments </td>
							<td width="75"> <input id="idReferral" name="SortReferral" class="mainForm" type="submit" value="Referral" /></td>

Open in new window


This code means that when a user clicks on any of the [submit] buttons to sort, the page is posting the "name" and "value" as parameters to itself. The PHP code (server-side) that is invisible to the client-side/view-source, takes the name/value data which is posted, and applies the sort to the data.

You will need to post the actual PHP code, enclosed in <?php ?> from the source code, to see where/how the sorting is taking place.
You will need to post the actual PHP code, enclosed in <?php ?> from the source code, to see where/how the sorting is taking place.

So are you saying nothing is currently sorting because the action="" is empty? There are several <?php ?> tags. Which php code are you referring to?

Do I create a separate PHP file such as sort.php containing the <?php ?> script in the source of the page now, load it to the server and place its path into the action=""> ?

I'm not sure I even know how to ask the question here. Forgive the ignorance.
To clarify, the sorting is completed by the code within the <?php ?> tags which uses the name and value (post data) of the particular submit button that was clicked.

Therefore, most importantly, we will need to see the code within the <?php ?> tags to help you with this question (please omit any passwords or personal data).

Having a blank action attribute just means that modern browsers use the same page to post data to. This brings up another point: I recommend naming the file you are processing with (if self-processing, use the file name of the page explicitly in action).

e.g. in your example file agent_page8-test.php:
<form id="form1" name="form1" method="post" action="agent_page8-test.php">

Open in new window



Having a blank action violates HTML5 (not HTML4) best practices; most modern browsers fallback to self (page the user is on) for posting data. If the action is left blank, the page is vulnerable to an iFrame ClickJacking attack.
Thanks. The code within the PHP tags is in the page itself. I did not realize you cannot see that code using a browser inspector. I am pasting it here.

<?php
// start the session
session_start();
if (array_key_exists('Authority', $_SESSION))
{
	$AID = $_SESSION['AID'];
	$LID = $_SESSION['LID'];
	$Authority = $_SESSION['Authority'];
	if ($Authority == 'Agent')
	{
		$_SESSION = array();
		session_destroy();
		header('Location: http://abmoving.com/Agents/agent_page2.php');
		exit;
	}
}
else
{
	$_SESSION = array();
	session_destroy();
	header('Location: http://abmoving.com/Agents/agent_page2.php');
	exit;
}
$SortOrder = 0;
if ($_POST)
{
	if (array_key_exists('Logout', $_POST))
	{
		$_SESSION = array();
		session_destroy();
		header('Location: http://abmoving.com/index.php');
		exit;
	}
	if (array_key_exists('SortOrder', $_SESSION))
	{
		$SortOrder = $_SESSION['SortOrder'];
	}
	else
	{
		$SortOrder = 0;
	}
	if (array_key_exists('SortProperty', $_POST))
	{
		$SortOrder = 1;
	}
	if (array_key_exists('SortAgent', $_POST))
	{
		$SortOrder = 2;
	}
	if (array_key_exists('SortEmail', $_POST))
	{
		$SortOrder = 3;
	}
	if (array_key_exists('SortCommission', $_POST))
	{
		$SortOrder = 4;
	}
	if (array_key_exists('SortPayments', $_POST))
	{
		$SortOrder = 5;
	}
	if (array_key_exists('SortActivity', $_POST))
	{
		$SortOrder = 6;
	}
	$_SESSION['SortOrder'] = $SortOrder;
	$SID = '';
	$ID = 0;
	foreach ($_POST as $key => $value)
	{
		$FID = substr($key,0,4);
		if ($FID == 'GoTo')
			$SID = substr($key,4);
	}		
	if ($FID == 'GoTo')
	{
		$_SESSION['AID'] = $SID;
		header("Location: http://abmoving.com/Agents/agent_page6.php");
		exit;
	}
	$_POST = array();
}
$Order = 'ORDER BY Authority, Contact_Last_Name, Contact_First_Name';
if ($SortOrder == 1) /* Sort by Property */
{
	$Order = 'ORDER BY Authority, Property_Name, Contact_Last_Name, Contact_First_Name';
}
if ($SortOrder == 2) /* Sort by Agent */
{
	$Order = 'ORDER BY Authority, Contact_Last_Name, Contact_First_Name';
}
if ($SortOrder == 3) /* Sort by EMail */
{
	$Order = 'ORDER BY Authority, Contact_Email, Contact_Last_Name, Contact_First_Name';
}
if ($SortOrder == 4) /* Sort by Commission */
{
	$Order = 'ORDER BY Authority, Agent_Commission, Contact_Last_Name, Contact_First_Name';
}
if ($SortOrder == 5) /* Sort by Payments */
{
	$Order = 'ORDER BY Authority, Contact_Last_Name, Contact_First_Name';
}
if ($SortOrder == 6) /* Sort by Activity */
{
	$Order = 'ORDER BY Authority, Contact_Last_Name, Contact_First_Name';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>
		AB Moving Company - Dallas Movers, Fort Worth Movers, Austin Movers
	</title>
<?php
	include('meta.inc.php');
?>	
<link href="style-agent.css" rel="stylesheet" type="text/css">
<!--[if IE 7]>
    <style type="text/css" media="screen">
	#form_outer { padding: 0px; }
	#form { padding: 10px; }
	#form table { margin: 0px; }
	</style>
<![endif]-->
<!--[if IE 6]>
    <style type="text/css" media="screen">
	#form_outer { padding: 0px; }
	#form { padding: 10px; }
	#form table { margin: 0px; }
	</style>
<![endif]-->
	
<script type="text/javascript" src="general.js"></script>

</head>

<body onLoad="preloadImages();" background="/images/whitestripebg.gif" bgcolor="6C6C6C" link="white" vlink="white">
<?php
	include('connection.inc.php');

		$sql0 = "SELECT ".
			"AID, ".
			"Account_ID, ".
			"Authority, ".
			"Property_Name, ".
			"Property_Address1, ".
			"Property_Address2, ".
			"Property_City, ".
			"Property_State, ".
			"Property_Zip, ".
			"Contact_First_Name, ".
			"Contact_Last_Name, ".
			"Contact_Address1, ".
			"Contact_Address2, ".
			"Contact_City, ".
			"Contact_State, ".
			"Contact_Zip, ".
			"Contact_Phone, ".
			"Contact_FAX, ".
			"Contact_Email ".
			"from accounts ".
			$Order;

		$result0 = mysql_query($sql0) or die(mysql_error());
?>
<div id="wrapper" align="center">
	<!-- begin form -->
	<form id="form1" name="form1" method="post" action="">
		<div id="body" align="center" text-align: center;>
				
				<br />
				<br /><br />
				<table id="detail_table" align="center" border="1" cellpadding="5" cellspacing="1" width="100%">
					<tbody>
						<tr style="color: #506B7B; font-weight: bold;">
							<td width="50">Select</td>
							<td width="200"><input id="idProperty" name="SortProperty" class="mainForm" type="submit" value="Property" /></td>
							<td width="150"><input id="idAgent" name="SortAgent" class="mainForm" type="submit" value="Agent" /></td>
							<td width="150"><input id="idEmail" name="SortEmail" class="mainForm" type="submit" value="Email" /></td>
							<td width="75"> <input id="idCommission" name="SortCommission" class="mainForm" type="submit" value="Commission" /></td>
							<td width="75"> Payments </td>
							<td width="75"> <input id="idReferral" name="SortReferral" class="mainForm" type="submit" value="Referral" /></td>
						</tr>
<?php
					while ($row0 = mysql_fetch_assoc($result0))
					{
						$Agent_Commission = 0;
						$Agent_Paid = 0;
						$AID = $row0['AID'];
						$Account_ID = $row0['Account_ID'];
						$LAuthority = $row0['Authority'];
						$Property_Name = $row0['Property_Name'];
						$Property_Address1 = $row0['Property_Address1'];
						$Property_Address2 = $row0['Property_Address2'];
						$Property_City = $row0['Property_City'];
						$Property_State = $row0['Property_State'];
						$Property_Zip = $row0['Property_Zip'];
						$Contact_First_Name = $row0['Contact_First_Name'];
						$Contact_Last_Name = $row0['Contact_Last_Name'];
						$Contact_Address1 = $row0['Contact_Address1'];
						$Contact_Address2 = $row0['Contact_Address2'];
						$Contact_City = $row0['Contact_City'];
						$Contact_State = $row0['Contact_State'];
						$Contact_Zip = $row0['Contact_Zip'];
						$Contact_Phone = $row0['Contact_Phone'];
						$Contact_FAX = $row0['Contact_FAX'];
						$Contact_Email = $row0['Contact_Email'];

						$sql1 = "SELECT ".
							"sum(Referral_Commission) as Agent_Commission, ".
							"sum(Referral_Paid) as Agent_Paid, ".
							"max(Referred) as Referral_Date ".
							"from referral ".
							"where AID = '".$AID."'";

						$Agent_Commission = '0.00';
						$Agent_Paid = '0.00';
						$Referral_Date = '&nbsp;';
						$result1 = mysql_query($sql1) or die(mysql_error());
						$row1 = mysql_fetch_assoc($result1);
						if ($row1['Agent_Commission'] != '')
						{
							$Agent_Commission = $row1['Agent_Commission'];
						}
						if ($row1['Agent_Paid'] != '')
						{
							$Agent_Paid = $row1['Agent_Paid'];
						}
						if ($row1['Referral_Date'] != '')
						{
							$Referral_Date = $row1['Referral_Date'];
						}
						else
						{
							$Referral_Date = '&nbsp;';
						}
						if ($Property_Name == '')
						{
							$Property_Name = 'n/f';
						}
						if ($Contact_Email == '')
						{
							$Contact_Email = 'n/f';
						}
						if ($LAuthority == 'All')
						{
							$Agent_Commission = 'Administrator';
							$Agent_Paid = 'Administrator';
						}
?>
						<tr>
							<td>
<?php
								echo '<input id="GoTo'.$AID.'" name="GoTo'.$AID.'" class="mainForm" value="Referrals" type="submit">';
?>
							</td>
							<td>
<?php
								echo $Property_Name;
?>
							</td>
							<td>
<?php
								echo $Contact_Last_Name.', '.$Contact_First_Name;
?>
							</td>
							<td>
<?php
								echo $Contact_Email.' ';
?>
							</td>
							<td align="right">
<?php
							If ($LAuthority == 'All')
							{
								echo 'Administrator';
							}
							else
							{
								echo $Agent_Commission;
							}
?>
							<td align="right">
<?php
							If ($LAuthority == 'All')
							{
								Echo 'Administrator';
							}
							else
							{
								echo $Agent_Paid;
							}
?>
							</td>
							</td>
							<td align="right">
<?php
								echo $Referral_Date;
?>
							</td>
						</tr>		
<?php
					}
?>
					</tbody>
				</table>
		</div> <!-- end of id=body -->
	</form><!-- end of form -->
</div> <!-- end of id=wrapper -->
</body>
</html>

Open in new window

SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I appreciate all that you have provided. I have passed it on to my developer, but we are just not experienced enough to implement it successfully. If I can hire you to do it, I'd be interested. Otherwise, I don't have the resources to fix it. Open to suggestions.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Recoding using datatables would be optimal; optionally, you can hire a dev to do this recode or fix.