Jump to content

if no category selected search all


graham23s

Recommended Posts

Hi Guys,

 

i have finished a search function it works great if you select a category brings back and paginates all the results, but in my search form i have at the top "Search All Categories" and the cat_id for this is 0

 

if you don't select a category it defaults to 0 and should search all results but it's not doing that heres my code for the query:

 

		// Create our query, including our keywords...///////////////////////////////////
	$sql  = "SELECT * ";
	$sql .= "FROM `uploaded_files` ";
	$sql .= "WHERE (`file_name` LIKE '%$keywords%' OR `file_name` LIKE '%$keywords%')";

	// search all cats...////////////////////////////////////////////////////////////
	if($cat_id == $no_cat) {

	    $sql .= " AND `cat_id`='$cat_id'";

	} else {

  	   $sql .= " AND `cat_id`='$cat_id'";

	}

	// search all cats...////////////////////////////////////////////////////////////

	$quick_results = $_GET['results'];

	if($_GET['results'] == 'Ascending') {

	$sort_order = 'ASC';

	} else {

	$sort_order = 'DESC';

	}

	$sql .= " ORDER BY `date_added` $sort_order LIMIT $from, $max_results";

 

can anyone see where i have went wrong?

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/56932-if-no-category-selected-search-all/
Share on other sites

Hi Mate,

 

still the same i have done this though:

 

        if($cat_id == 0) {

	   echo '';

	} else {

  	   $sql .= " AND `cat_id`='$cat_id'";

	}

 

this seems to work for a part anyway:

 

no category selected:

 

SELECT * FROM `uploaded_files` WHERE (`file_name` LIKE '%%' OR `file_name` LIKE '%%') ORDER BY `date_added` ASC LIMIT 0, 25

 

with a category selected:

 

SELECT * FROM `uploaded_files` WHERE (`file_name` LIKE '%%' OR `file_name` LIKE '%%') AND `cat_id`='1' ORDER BY `date_added` ASC LIMIT 0, 25

 

the only thing now is the pagination link the way it is now the link wants to search a category number (not 0) would i be better putting an if/else in the pgination links aswell:

 

		// Build Next Link 
	if($page < $total_pages){ 
		$next = ($page + 1); 
		echo "<a href=\"".$_SERVER['PHP_SELF']."?keywords=$keywords&results=$quick_results&cat_id=$cat_id&page=$next\"> >>></a>"; 
	} 

 

cheers

 

Graham

 

 

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.