Jump to content

Search results and pagination problem


graham23s

Recommended Posts

Hi Guys,

 

i'm having a wee bit of trouble with my search results and pagination i'm using the same pagination script i usually do with most pages, but this is proving to be a bit more difficult

 

heres my coding:

 

<?php
	// Define the number of results per page 
	$max_results = 2;

    // Check to see if there are submitted keywords...///////////////////////////////
    if(isset($_GET["keywords"]) && ($_GET["keywords"] != "")) {

	// Require a database connection...//////////////////////////////////////////////
	require("includes/db_connection.php");

	// Capture our search words and remove url encoding...///////////////////////////
	$keywords = urldecode($_GET["keywords"]);

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

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

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

	// Perform MySQL query on only the current page number's results 

	$sql = mysql_query("SELECT * FROM `uploaded_nzbs` LIMIT $from, $max_results"); 

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

	// Create our query, including our keywords...///////////////////////////////////
	$sql  = "SELECT id,file_name,cat_id,description,date_added ";
	$sql .= "FROM `uploaded_files` ";
	$sql .= "WHERE `file_name` LIKE '%$keywords%' OR `description` LIKE '%$keywords%'";
	$sql .= "ORDER BY `date_added` ASC ";

	// Create a result set from our query.../////////////////////////////////////////
	$rs = mysql_query($sql) or die(mysql_error());

	// number of results...//////////////////////////////////////////////////////////
	$num_res = mysql_num_rows($rs);

	// table...//////////////////////////////////////////////////////////////////////
        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">File Name</font></th><th bgcolor="#004E98" /><font color="#ffffff">Added</font></th>
              </tr>';
           

	// Print the number of results...////////////////////////////////////////////////
	echo "<center>($num_res) Results Found!</center><br /><br />";

	// Proceed to print out all results...,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
	while($row = mysql_fetch_array($rs)) {

	$id = $row['id'];
	$file_name = $row['file_name'];
	$date_added = $row['date_added'];
	$cat_id = $row['cat_id'];

	// link the 2 tables...//////////////////////////////////////////////////////////
	$query = "SELECT * FROM `uploaded_files` WHERE `cat_id` = '$cat_id'";
	$result = mysql_query($query);		
        $cat_id_2 = mysql_fetch_array($result);
        
        $gif = $cat_id_2['cat_id'];

	// get the .gif...//////////////////////////////////////////////////////////
    $query4 = "SELECT * FROM `categories` WHERE `id`='$gif'";
	$result4 = mysql_query($query4);			 
	$rows = mysql_fetch_array($result4);

	$display_gif = $rows['image'];

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


	}

	echo '</table><br />';

    } else {

	print "";

    }
        ## 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 `poster_name`='$member'"),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']."?page=$prev\"><<< </a> "; 
	} 

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

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

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

 

it's the most difficult thing in php i find.

 

thanks for any help guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/54982-search-results-and-pagination-problem/
Share on other sites

Double check this:

	$sql = mysql_query("SELECT * FROM `uploaded_nzbs` LIMIT $from, $max_results"); 

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

	// Create our query, including our keywords...///////////////////////////////////
	$sql  = "SELECT id,file_name,cat_id,description,date_added ";
	$sql .= "FROM `uploaded_files` ";
	$sql .= "WHERE `file_name` LIKE '%$keywords%' OR `description` LIKE '%$keywords%'";
	$sql .= "ORDER BY `date_added` ASC ";

 

You start to do a limit query, then you replace it with a string.

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.