Jump to content

Posting search results


graham23s

Recommended Posts

Hi Guys,

 

i am a bit stuck on this part of coding, when people search ALL categories i leave the option value as: <option value="0">

 

so if 0 is passed to the GET all categories should be searched instead of just category 1,2 or 3 etc

 

here is what i have so far:

 

<?php
 ## variables #######################################################################
 $keywords = urldecode($_GET['keywords']);

 ## new search ######################################################################
 echo '<br /><h4>Search Results</h4><br />';

 ## if no search was done ###########################################################
 if(empty($keywords)) {

 echo '<b><font color="red">Error: </font>Sorry You Never Entered Anything To Search For!</b><br /><br />';
 include("includes/footer.php");
 exit;

 }

 ## put the gets in variables #######################################################
 $category_id = $_GET['cat_id'];

	## Pagination start #############################################################
	echo "<center>";
	// If current page number, use it 
	// if not, set one! 

	if(!isset($_GET['page'])){ 
		$page = 1; 
	} else { 
		$page = $_GET['page']; 
	} 

	// Define the number of results per page 
	$max_results = 50; 		

	// Figure out the limit for the query based 
	// on the current page number. 
	$from = (($page * $max_results) - $max_results);  

	## Pagination start #############################################################

 ## create the query using our keywords #############################################
  $search_query = "SELECT * ";
 $search_query .= "FROM `uploaded_files` ";
 $search_query .= "WHERE (`file_name` LIKE '%$keywords%' OR `file_name` LIKE '%$keywords%')";

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

	$quick_results = $_GET['results'];

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

	$sort_order = 'ASC';

	} else {

	$sort_order = 'DESC';

	}

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

 echo $search_query;

 ## the resultset ###################################################################
 $query_result = mysql_query($search_query) or die (mysql_error());

 $how_many = mysql_num_rows($query_result);

 ## if no results back dont make the table ##########################################
 if($how_many == 0) {

 echo '<b><font color="red">Error: </font>Sorry We Have Found No Results With That Search Query!</b><br /><br />';
 include("includes/footer.php");
 exit;	 

 }

 ## a table for the results #########################################################
 echo '<table width="750" border="1" bordercolor="#000000" cellpadding="0" cellspacing="0" />
	   <tr>
	   <th bgcolor="#004E98" /><font color="#ffffff">Category</font></th><th bgcolor="#004E98" /><font color="#ffffff">NZB Name</font></th><th bgcolor="#004E98" /><font color="#ffffff">Added</font></th>
	   </tr>';
 ## while loop results ##############################################################
 ## while loop results ##############################################################
 while ($row = mysql_fetch_array($query_result)) {

 ## the variables ###################################################################
 $id = $row['id'];
 $file_name = $row['file_name'];
 $date_added = $row['date_added'];
 $cat_id = $row['cat_id'];

 // link the 2 tables.../////////////////////////////////////////////////////////////
 $query_1 = "SELECT * FROM `uploaded_files` WHERE `cat_id`='$cat_id'";
 $result_1 = mysql_query($query_1) or die (mysql_error());		
 $cat_id_2 = mysql_fetch_array($result_1) or die (mysql_error());		   
 $gif = $cat_id_2['cat_id'];

 // get the .gif...//////////////////////////////////////////////////////////
 $query_2 = "SELECT * FROM `categories` WHERE `id`='$gif'";
 $result_2 = mysql_query($query_2) or die (mysql_error());			 
 $rows = mysql_fetch_array($result_2) or die (mysql_error());
 $display_gif = $rows['image'];

 echo '<td width="5%" bgcolor="#004E98"><img src="category_pics/'.$display_gif.'" /></a></td><td align="left"> <a href="file_details.php?id='.$id.'" />'.$file_name.'</a></td><td>'.$date_added.'</td></tr>'; 

 }
 ## while loop results ##############################################################
 ## while loop results ##############################################################  

 ## end the table ###################################################################
 echo '</table><br />';

	 ## Pagination end ###############################################################

	// Figure out the total number of results in DB: 
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM `uploaded_files` WHERE `cat_id`='$cat_id'"),0); 

	// Figure out the total number of pages. Always round up using ceil() 
	$total_pages = ceil($total_results / $max_results); 

	// Build Previous Link 
	if($page > 1){ 
		$prev = ($page - 1); 
		echo " <a href=\"".$_SERVER['PHP_SELF']."?keywords=$keywords&page=$prev\"><<< </a> "; 
	} 

	for($i = 1; $i <= $total_pages; $i++){ 
		if(($page) == $i){ 
			echo "[<b>$i</b>] "; 
			} else { 
				echo "<a href=\"".$_SERVER['PHP_SELF']."?keywords=$keywords&page=$i\">$i</a> "; 
		} 
	} 

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

	## Pagination end ###############################################################
?>

 

This is what i have so far the pagination results are wrong, they always seem to give back the wrong results, and help on this would be great pagination always = headaches for me lol

 

thanks guys

 

Graham

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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