Hi Guys

I've got a MySQL product database on a website, and I wrote a php script that searches the database and returns the results in an html table with no problems at all.

My problem comes when trying to display x results (I've been trying 5) per page. I've been trawling the net for days adapting various scripts that I've found.

Below is my best effort so far (it displays the correct number of results in the table, but the next page and previous page links aren't quite right).

I have been trying with $_SERVER to link back to the page, without success

Hope you can help!

<html>
<body>

<form name="form" action="search2.php" method="get">
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>

<?php

$var = @$_GET['q'];
$trimmed = trim($var);

$limit=5;

if(empty($page)){
$page = 1;
}

if ($trimmed=="")
	{
	echo "You didn't enter anything to search. <br /><br />
		Our most popular products are.."; 
		exit;
	}
$con = mysql_connect("localhost","username","password");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("csproducts", $con);

$limitvalue = ($page - 1) * $limit;

$query = "SELECT * FROM productlist WHERE partno LIKE '%$trimmed%' || fullname LIKE '%$trimmed%' || description LIKE '%$trimmed%' || category LIKE '%$trimmed%' ORDER BY partno LIMIT $limitvalue, $limit";

$sql=mysql_query($query);
$totalrows=mysql_num_rows($sql);

$result = mysql_query($query) or die("Couldn't execute query");

if ($totalrows==0) {

echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned no results</p>";

}

if($totalrows>0){

echo "<table border='1' width='100%'>
<tr align='center'>
<th width='20%'>Image</th>
<th width='20%'>Part Number</th>
<th width='20%'>Part Name</th>
<th width='20%'>Description</th>
<th width='20%'>Category</th>
</tr>";

while($row = mysql_fetch_array($result)){

$image=$row['image'];
$partno=$row['partno'];
$fullname=$row['fullname'];
$description=$row['description'];
$category=$row['category'];

echo "<tr align='center'>";
  echo "<td width='20%'> <img src ='$image'> </td>";
  echo "<td width='20%'> $partno </td>"; 
  echo "<td width='20%'> $fullname </td>";
  echo "<td width='20%'> $description </td>";
  echo "<td width='20%'> $category </td>";
  echo "</tr>";

}
}


if($page > 1){
$pageprev = $page-1;
echo("<a href=\"search2.php?page=$pageprev\">PREV</a>&nbsp;");
}

$numofpages = ceil($totalrows / $limit);

for($i = 1; $i <= $numofpages; $i++){
if($page == $i){
echo($i."&nbsp;");
}
else{
echo"<a href=\"search2.php?page=$i\">$i</a>&nbsp;";
}
}
if($page < $numofpages){
$pagenext = ($page + 1);
echo "<a href=\"search2.php?page=$pagenext\">NEXT</a>";

}
?>
</body>
</html>

Recommended Answers

All 3 Replies

Try this:

<?php

$var = @$_GET['q'];
$trimmed = trim($var);
$page = $_GET['n'];

$limit=5;

if(empty($page)){
$page = 1;
}

if($page==1) { $newpage = 1; ?>

<html>
<body>

<form name="form" action="search2.php" method="get">
  <input type="text" name="q" />
	<input type="hidden" name="n" value="<? echo $newpage; ?>">
  <input type="submit" name="Submit" value="Search" />
</form>

<?
 } else { $newpage+=1; }


if ($trimmed=="")
	{
	echo "You didn't enter anything to search. <br /><br />
		Our most popular products are.."; 
		exit;
	}
$con = mysql_connect("localhost","username","password");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("csproducts", $con);

$limitvalue = $page-1;
$newlimit = $limitvalue+5;

$query = "SELECT * FROM productlist WHERE partno LIKE '%$trimmed%' || fullname LIKE '%$trimmed%' || description LIKE '%$trimmed%' || category LIKE '%$trimmed%' ORDER BY partno LIMIT $limitvalue, $newlimit";

$sql=mysql_query($query);
$totalrows=mysql_num_rows($sql);

$result = mysql_query($query) or die("Couldn't execute query");

if ($totalrows==0) {

echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned no results</p>";

}

if($totalrows>0){

echo "<table border='1' width='100%'>
<tr align='center'>
<th width='20%'>Image</th>
<th width='20%'>Part Number</th>
<th width='20%'>Part Name</th>
<th width='20%'>Description</th>
<th width='20%'>Category</th>
</tr>";

while($row = mysql_fetch_array($result)){

$image=$row['image'];
$partno=$row['partno'];
$fullname=$row['fullname'];
$description=$row['description'];
$category=$row['category'];

echo "<tr align='center'>";
  echo "<td width='20%'> <img src ='$image'> </td>";
  echo "<td width='20%'> $partno </td>"; 
  echo "<td width='20%'> $fullname </td>";
  echo "<td width='20%'> $description </td>";
  echo "<td width='20%'> $category </td>";
  echo "</tr>";

}
}


if($page > 1){
	$pageprev = $page-1;
	echo("<a href=\"search2.php?page=$pageprev\">PREV</a>&nbsp;");
}

$numofpages = ceil($totalrows / $limit);

for($i = 1; $i <= $numofpages; $i++){
	if($page == $i){
		echo($i."&nbsp;");
	} else{
		echo"<a href=\"search2.php?page=$i\">$i</a>&nbsp;";
	}
}

if($page < $numofpages){
$pagenext = ($page + 1);
echo "<a href=\"search2.php?page=$pagenext\">NEXT</a>";

}
?>
</body>
</html>

Thanks very much for getting back to me. Now, initially I get a PHP Parse error: syntax error, unexpected $end in "doc_path" on line 112, but that is easily fixed by putting <?php instead of <? only in those 2 places.

Then, the error PHP Notice: Undefined index: n in "doc_path on" line 5.

I sorted this out by putting in the following code:

if (isset($_GET['n']))
{
 $page = $_GET['n'];        
}

However the links for the next search results don't even appear.

I'm going to try and mess around with the code a little more - hopefully the 'hidden' property of the form will be the answer to my problem...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.