Forum Moderators: coopster

Message Too Old, No Replies

Problem sorting custom search results

         

galahad2

6:35 pm on Feb 13, 2016 (gmt 0)

10+ Year Member



Hello, I'm trying to find a way to sort the results of a search in PHP without reloading the entire page. Below is the code for the results, based on whichever criteria the user fills in on a preceding form. The search itself works fine but I need the table of results to be sortable. At the moment if I make it sortable it reloads the whole page, so instead of outputting just the relevant records it outputs all the others as well. I tried adding a link in the table headers for sorting, i.e
 <a href="<?=$_SERVER['PHP_SELF']?>?sort=CandidateName">Type:</a>
but this just loads the entire recordset. Any ideas?


if($_POST['searchcandidates'])
{
$candidateid = $_POST['_CandidateID'];
$candidatename = $_POST['_CandidateName'];
$knownas = $_POST['_KnownAs'];
$professionalregno = $_POST['_ProfessionalRegNo'];
if ($candidatename) { $getcandidates = $mysqli->query("SELECT * FROM candidates WHERE CandidateName LIKE '%$candidatename%'"); }
elseif ($knownas) { $getcandidates = $mysqli->query("SELECT * FROM candidates WHERE KnownAs LIKE '%$knownas%'"); }
elseif ($professionalregno) { $getcandidates = $mysqli->query("SELECT * FROM candidates WHERE ProfessionalRegNo LIKE '%$professionalregno%'"); }
else echo '<p>No search criteria specified!</p>';
if ($getcandidates->num_rows == 0) {
echo '<p>There are no candidates matching your search. <a href="searchcandidates.php">Search again?</a></p>';
exit;
}
?>
<p>&nbsp;</p>
<table width="900" border="0" cellspacing="5" cellpadding="5">
<tr>
<th width="150" align="left">Candidate Name</th>
<th width="150" align="left">Known As</th>
<th width="200" align="left">Email</th>
<th width="125" align="left">Professional Reg. No.</th>
<th width="125" align="left">Date Added</th>
<th width="150" align="left">&nbsp;</th>
</tr>
<?php
// display the results returned
while ($row = $getcandidates->fetch_assoc()) {
$candidateid = $row["CandidateID"];
$candidatename = $row["CandidateName"];
$contactname = $row["ContactName"];
$email = $row["Email"];
$dateadded = $row["DateAdded"];
$formatteddate = date("F j, Y", strtotime($dateadded) );
$archived = $row["IsArchived"];

echo '<tr>';
echo '<td>' . $candidatename . '</td><td>' . $knownas . '</td><td>' . $email . '</td><td>' . $professionalregno . '</td><td>' . $formatteddate . '</td><td><a href="editcandidate.php?CandidateID=' . $candidateid . '" title="Edit" class="button">Edit</a>';

if ($checkuser == 'Admin') {
if ($archived == 'Y') { echo ''; }
else {
?>

<?php }
}

echo '</td></tr>';

$count++ ;
}
echo '</table>';
}

whitespace

11:29 pm on Feb 13, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



It sounds like you need some JavaScript to sort the resulting HTML table (ie. manipulate the DOM).