Jump to content

Search form broke my sort


fwegan

Recommended Posts

::waves:: Hey everybody, I'm new to PHP and MySQL. All I know is what I've learned over the last month while building a website for the small nonprofit where I work.

One page had a sortable list, but as soon as I made it searchable, the sorting broke. Each works fine on its own, just not together. I'm totally stumped, but it's probably something really easy and stupid that I'm just not seeing, right?

Sort stuff:
[code]
switch ($_POST['sort_by']) {
case "Name":
$sortby = "org_name";
break;
case "Location":
$sortby = "org_location";
break;
default:
$sortby = "org_name";
break;
}[/code]

Search stuff:
[code]$interests_search = $_GET['interests'];
$num_interests_search = count($interests_search);

for ($j = 0; $j < ($num_interests_search - 1); $j++) {
$search_term .= "(org_interests LIKE '%".strtolower($interests_search[$j])."%') OR ";
}
if ($j == ($num_interests_search -1)) {
$search_term .= "(org_interests LIKE '%".strtolower($interests_search[$j])."%')";
}
if ($search_term != "") {
$search_term .= " AND (inactive != 'true')";
}
[/code]

The select statement:
[code]if ($search_term == "") {
$sql = "SELECT * FROM members WHERE inactive != 'true' ORDER BY $sortby";
}
else {
$sql = "SELECT * FROM members WHERE $search_term ORDER BY $sortby";
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/31273-search-form-broke-my-sort/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.