I have a script that is 95% functional but Im getting hung up on 2 issues.
on some result sets where I have 900 or more items in the database the list of 123 links at the bottom of the page is far too much for nice display, what I need is perhaps
[prev] 1 2 3 4... [next] in short I need to limit the number of links displayed at any given time, ala google or even like here in the forum. I have checked the articles but those scripts are a bit outdated and buggy, not to mention that this peticular problem is not addressed. currently I have previous and next buttons and a page number display, but if over 10 pages show, it looks like crap.
Also on the first page, the first and last page links are disabled, on later pages the current page link is disabled (like it should be), why is that last page link getting disabled on the first page of results?
to see the script in action go to www.archivia.com/browse.html
Here is the code (it has been hacked together from many scripts and suggestions):
<html> <head><title>Search Results</title></head>
<?
/ connection information /
$hostname = "site.com";
$username = "myuserid";
$password = "********";$dbName = "mydb";
/ make connection to database /
MYSQL_CONNECT($hostname, $username, $password) OR DIE( "Unable to connectto database");
@mysql_select_db("$dbName") or die( "Unable to select database");
/ define user input vars and vars for no data entered /
if ($title == "") {$title = '%';}
if ($author == "") {$author = '%';}
if ($keywords == "") {$keywords = '%';}
/ sql statement /
$sql = "SELECT * FROM $table WHERE title LIKE '%$title%' AND author LIKE '$author%' AND keywords LIKE '%$keywords%' ORDER BY author, title";
$vara = $author;
$vart = $title;
$kw = $keywords;
$tab = $table;
$limit = 20; // rows to return
$numresults=mysql_query($sql);
$numrows=mysql_num_rows($numresults);
// next determine if page has been passed to script, if not use 1
if (empty($page)) {
$page = 1;
}
$page = $limit * ($page-1);
// get results
$result = mysql_query("$sql limit $page,$limit");
// now you can display the results returned
WHILE ($data=MYSQL_FETCH_ARRAY($result))
$number = mysql_num_rows($result);
if (!$number) print(" \n");
/ Print the relevant information /
$i = 0;
PRINT "<font face=\"Verdana, Arial, Helvetica, sans-serif\"><b>
$numrows books found, displaying 20 records at a time. </b></font><p>";
PRINT "<font color=red face=\"Verdana, Arial, Helvetica, sans-serif\">To order call 212-439-9194, fax 212-744-1626 or email info@archivia.com</font><p>";
PRINT "<table width=100% cellpadding=5 cellspacing=5>";
PRINT "<TR bgcolor=black> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\" color=white><b>description</b></font></td></tr>";
WHILE ($i < $number):
$author = mysql_result($result, $i,"author");
$title = mysql_result($result,$i,"title");
$bookid = mysql_result($result,$i,"bookid");
$price = mysql_result($result,$i,"price");
$description = mysql_result($result,$i,"description");
$image = mysql_result($result,$i,"image"); {
PRINT "<table valign=top width=100% cellspacing=5 cellpadding=5><tr>
<td width=15%> $image</td>
<td width=85%><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"-1\"><i> $author</i></font><br> <font color=blue face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"3\"><b> $title</b></font><br><br><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"-1\"> $description</font><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"2\"><b>(#$bookid) </font><br><font color=blue face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"2\"> \$$price</b></font></td>
</tr>
<br><hr><br>
</table>";
}
$i++;ENDWHILE;
PRINT "</table>";
echo "<BR><hr><br><br>";
echo "<center>";
$numofpages = $numrows/$limit;
if($page >= 1) {
$pageprev= $page/$limit;
echo "<A HREF=\"$PHP_SELF?page=$pageprev&table=$tab&author=$vara&title=$vart&keywords=$kw&sort=author%2C+title\"><img src='previous.gif' border='0'> </A>";
/ if page is not equal to one, prev goes to $page - 1 /
}
for($i= 1; $i <= $numofpages; $i++) {
$newoffset = $limit($i-1);
if ($newoffset == $page) {
print " <font size='+1'><b> $i </b></font>\n";
}
else
{
PRINT "<img src='spacer.jpg' border='0'><A HREF=\"$PHP_SELF?page=$i&table=$tab&author=$vara&title=$vart&keywords=$kw&sort=author%2C+title\"><font size='+1'> $i</font> </A><img src='spacer.jpg' border='0'>";
/ make number navigation /
}
}
if($page <1) {
print " <font size='+1'><b> $i </b></font>\n";
}
elseif($numrows%$limit != 0) {
PRINT "<A HREF=\"$PHP_SELF?page=$i&table=$tab&author=$vara&title=$vart&keywords=$kw&sort=author%2C+title\"><font size='+1'> $i</font> </A>";
/ if there is a remainder, add another page */
}
if($numrows-$page > 10){
$pagenext = ($page/$limit)+2;
echo "<A HREF=\"$PHP_SELF?page=$pagenext&table=$tab&author=$vara&title=$vart&keywords=$kw&sort=author%2C+title\"> <img src='next.gif' border='0'> </A>";
/ if the totalrows - $limit $page is > 0 (meaning there is a remainder), leave the next button. */
}
echo "</center>";
?>
</html>