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: "" . $trimmed . "" 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> ");
}
$numofpages = ceil($totalrows / $limit);
for($i = 1; $i <= $numofpages; $i++){
if($page == $i){
echo($i." ");
}
else{
echo"<a href=\"search2.php?page=$i\">$i</a> ";
}
}
if($page < $numofpages){
$pagenext = ($page + 1);
echo "<a href=\"search2.php?page=$pagenext\">NEXT</a>";
}
?>
</body>
</html>