Hi Pig!
Well, i think i have a pretty good idea what the problem is,
you see, when the database is searched with a limit of then, those 10 items are displayed, and the numbered links are displayed as well. The authenticate.php script runs through the database everytime one of the numbered links is clicked. In other words, the database is searched everytime a numbered link is clicked.
I was confused by the offset, the limit. The limit is used when the database is searched right? So after the first 10 items, the offset is 10, and another search is performed from offset 10 in the database. Probably i need to use the offset on the resultset, but how?
Ok, here's the code:
<?php
/////////////// Start the session ///////////////
session_start();
session_name("ribkasid");
session_register();
/////////////////// Variables ///////////////////
if (!isset($start)) {
$start=0;
}
if (!isset($page)) {
$page=1;
}
$limiet=10;
////////////////////////////////////////////////
if ($imagepath=="database/images") {
$imagepath="database/images";
}
else {
$imagepath="database/photos";
}
include "ConnectDB.php";
$start=($page-1)*$limiet;
/////////////// Get results from the Database ///////////////
$SQL_statement_total = "SELECT * FROM maintable WHERE description LIKE '%$keyword%'";
$resultset_total = mysql_query($SQL_statement_total);
$num_rows_total = mysql_num_rows($resultset_total);
////// Check if a category was chosen ////////
if (!isset($catego)) {
$SQL_statement = "SELECT * FROM maintable WHERE description LIKE '%$keyword%' LIMIT " . $start . "," . $limiet;
} else {
/// If so.... ///
$getcatnumber = "SELECT * from cat where cat='$catego'";
$getcatnumberresult = mysql_query($getcatnumber);
$catnumber_result = mysql_fetch_array($getcatnumberresult);
$detectnumber=$catnumber_result['cat_id'];
$SQL_statement = "SELECT * FROM maintable WHERE description LIKE '%$keyword%' AND cat_id='$detectnumber' LIMIT " . $start . "," . $limiet;
}
$resultset = mysql_query($SQL_statement);
$num_rows = mysql_num_rows($resultset);
//// If results are less than 1, do something ////
if ($num_rows<1) {
include ("notfound.htm");
}
else {
//// Otherwise, setup variables and display stuff ////
include ("search_logo.htm");
echo $catego;
echo $categoid;
while ($data = mysql_fetch_array($resultset)){
$imagefile=$data['filename'];
$catnum=$data['cat_id'];
$idnum=$data['id'];
$cat_statement = "SELECT * from cat WHERE cat_id = '$catnum'";
$cat_result = mysql_query($cat_statement);
$cat_name = mysql_fetch_array($cat_result);
$sizenum=$data['size_id'];
$size_statement = "SELECT * from size WHERE size_id = '$sizenum'";
$size_result = mysql_query($size_statement);
$size_name = mysql_fetch_array($size_result);
$resnum=$data['res_id'];
$res_statement = "SELECT * from res WHERE res_id = '$resnum'";
$res_result = mysql_query($res_statement);
$res_name = mysql_fetch_array($res_result);
$pricenum=$data['price_id'];
$price_statement = "SELECT * from price WHERE price_id = '$pricenum'";
$price_result = mysql_query($price_statement);
$price_name = mysql_fetch_array($price_result);
$image_total="";
$imagetotal = "<img src = $imagepath/$imagefile>";
include ("template.htm");
}
}
//// Set the navigation below the page ////
include ("template_nav.htm");
?>
// This is template_nav.htm //
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="100%" border="0">
<tr>
<td>
<?
$server = "xxxxxxx";
$username = "xxxxxx";
$password = "xxxxxxxx";
$database = "xxxxxxxx";
$table = "xxxxxxxxx";
// Connect To MySQL Server
//@mysql_connect($server, $username, $password) or die("Couldn't Connect to Database");
// Select Database
//@mysql_select_db($database) or die("Couldn't Select Database");
// set number of results to display per page
$pagelimit = "10";
// run query (change yourtable to the name of your table)
// $strSQL = mysql_query("SELECT * FROM $table");
// count number of matches
// $totalrows = mysql_num_rows($strSQL);
// determine how many pages there will be by using ceil() and dividing total rows by pagelimit
$pagenums = ceil ($num_rows_total/$pagelimit);
// if no value for page, page = 1
if ($page==''){
$page='1';
}
// create a start value
$start = ($page-1) * $pagelimit;
// blank matches found
echo "<b>" . $num_rows_total . " matches found</b><br>\n";
// Showing Results 1 to 1 (or if you're page limit were 5) 1 to 5, etc.
$starting_no = $start + 1;
if ($num_rows_total - $start < $pagelimit) {
$end_count = $totalrows;
} elseif ($num_rows_total - $start >= $pagelimit) {
$end_count = $start + $pagelimit;
}
echo "Results $starting_no to $end_count shown.<br>\n";
// create dynamic next, previous, and page links
/ lets say you're set to show 5 results per page and your script comes out with 7 results.
this will allow your script to say next2 if you're on the first page and previous5 if you're on the second page. /
if ($num_rows_total - $end_count > $pagelimit) {
$var2 = $pagelimit;
} elseif ($num_rows_total - $end_count <= $pagelimit) {
$var2 = $num_rows_total - $end_count;
}
$space = " ";
// previous link (make sure to change yourpage.php to the name of your page)
if ($page>1) {
echo " <a href='authenticate.php?page=".($page-1)."&keyword=".$keyword."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
}
// dynamic page number links (make sure to change yourpage.php to the name of your page)
for ($i=1; $i<=$pagenums; $i++) {
if ($i!=$page) {
echo " <a href='authenticate.php?page=".$i."&keyword=".$keyword."' class=main>$i</a>";
}
else {
echo " <b class='red'>$i</b>";
}
}
// next link (make sure to change yourpage.php to the name of your page)
if ($page<$pagenums) {
echo "" . $space . $space . $space . $space . " <a href='authenticate.php?page=".($page+1)."&keyword=".$keyword."' class=main>Next " . $var2 . "</a> ";
}
/* output your data wherever you'd like.
BUT
in order for this all to work, before outputting your data, you have to run the query over using MySQL's LIMIT. This will limit how many results are actually displayed on the page. */
$strSQL = mysql_query("SELECT * FROM $maintable LIMIT $start,$pagelimit");
// LIMIT 0,10 will start at 0 and display 10 results
// LIMIT 10,5 will start at 10 and display 5 results
?>
</td>
<td align="center"><form name="form1" method="post" action="authenticate.php">
<font size="2" face="Arial, Helvetica, sans-serif">Search again</font>
<label>
<input type="text" name="keyword">
</label>
</form></td>
<td> </td>
</tr>
</table>
</body>
</html>
Template_nav.htm is a piece of code i got from a link on this forum
Let me know what i'm doing wrong here ok?
It's the search through the database i think...
Thankx!
Fish