Jump to content

Pagination problems with search engine


lucidium

Recommended Posts

Hi, I'm very new to PHP and have been working on a small project that is a search engine connected to a mysql database.  I can't seem to display the results from the search across multiple pages, when I click the Next button it just brings me to a blank page with no content except for my search form on top.  I have a feeling that my problem lies in how I'm passing variables from one page to another as I realize that the URL of page 2 doesn't have the users search anymore.  What do I need to change to get multiple pages working?

<?php
if(isset($_GET['submit'])){
	if(isset($_GET['name'])){
		if(preg_match("/^[  a-zA-Z0-9]+/", $_GET['name'])){
			$name=$_GET['name'];

			// Connect to database
			$db=mysql_connect("localhost", "root", "1234") or die ('Cannot connect: ' . mysql_error());
			// Select database
			$mydb=mysql_select_db("mydb");

			// Check page num, if no page num set to 1
			if(isset($_GET['pagenum'])){
				$pagenum = $_GET['pagenum'];
			}
			else{
				$pagenum = 1;
			}

			// Query the database table
			$sql="SELECT  id, ProductName, Price FROM testdb WHERE ProductName LIKE '%" . $name ."%'";

			// Run query 
			$result=mysql_query($sql);

			// Check to see if any results were found, if not, tell user
			$resultnum = mysql_num_rows($result);

			// Number of results per page
			$page_rows = 4;

			// Page number of previous page
			$last = ceil($resultnum/$page_rows);

			// Make sure page number isn't below 1 or above maximum
			if($pagenum < 1){
				$pagenum = 1;
			}
			elseif($pagenum > $last){
				$pagenum = $last;
			}

			// Set range to display in query
			$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

			$sql2 = "SELECT  id, ProductName, Price FROM testdb WHERE ProductName LIKE '%" . $name ."%' $max";

			$data_p = mysql_query($sql2);

			if($resultnum == 0){
				echo "<p class='p1'>No results found for query: " . $name . "</p>";
			}

			else{		

				echo "<table class='sortable'>";
				echo "<thead>";
				echo "<tr>";
				echo "<th>" . "Model Number" . "</th>";
				echo "<th>" . "Product" . "</th>";
				echo "<th>" . "Price" . "</th>";
				echo "</tr>";
				echo "</thead>";

				// Loop through results
				$bg = '#9D9D9D';
				while($row=mysql_fetch_array($data_p)){
					$bg = ($bg=='#9D9D9D' ? '#C7C7C7' : '#9D9D9D');
					$ProductName=$row['ProductName'];
					$Price=$row['Price'];
					$ID=$row['id'];

					// Display results of array
					echo "<tr bgcolor='" .$bg . "'>";
					echo "<td width='30%', align='center'>" . " " . "</td>";
					echo "<td width='60%', align='center'>" . "<a href=\"search.php?id=$ID\">" . $ProductName . "</a></td>";
					echo "<td width='10%', align='center'>" . $Price . "</td>\n";
					echo "</tr>";
					echo "<tr><td></td></tr>";
				}
				echo "</table>";

				echo "<p>";
				echo " --Page $pagenum of $last-- </p>";

				if($pagenum == 1){
				}
				else{
					echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
					echo " ";
					$previous = $pagenum-1;
					echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
				}

				if ($pagenum == $last){
				}
				else{
					$next = $pagenum+1;
					echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
					echo " ";
					echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
				}
			}
		}
		else{
			echo "<p class='p2'>Please enter a query.</p>";
		}
	}
}
?>

 

The search form looks like this...

<form  method="get" action="search.php"  id="searchform">
      <input  type="text" name="name" size = "42">
      <input  type="submit" name="submit" value="Search" STYLE="font-family: Arial, Helvetica, sans-serif; font-weight: bold;">
    </form>

 

Thanks.

 

 

Link to comment
Share on other sites

you could add the search term as part of the querystring on the links to pages 2,3,4 etc

 

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&name=$name'> <-Previous</a> ";

 

might need url encoding though if its not just one word or has non alpha characters

Link to comment
Share on other sites

you could add the search term as part of the querystring on the links to pages 2,3,4 etc

 

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&name=$name'> <-Previous</a> ";

 

might need url encoding though if its not just one word or has non alpha characters

 

This worked beautifully, thanks a ton.  What is this URL encoding you mention though, as some searches may be model numbers and others will likely be more than one word.

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.