PDA

View Full Version : Help with Navigation Links


maltrecho
06-25-2003, 08:02 PM
This is the code I'm using to build the navigation links in a result of a query in MySQL.
It comes from http://www.phpbuilder.com/columns/rod20000221.php3

What I would like to do, is the (...) as below:

<< previous |1|2|3|...|16|17|18| next >>

Any ideas?

----------THE CODE:---------

... bla bla bla ...

$keywords = $_GET['keywords'];

$offset = $_GET['offset'];
if (!isset($offset)) {
$offset = 0;
}

$limit = 10;

... $query = "SELECT SQL_CALC_FOUND_ROWS *, bla bla bla ...

... bla bla bla ...

$query = "SELECT FOUND_ROWS() AS total";

$total_results = mysql_db_query("database", $query, $connect);

$count = mysql_fetch_array($total_results);
$num_results = $count['total'];

... bla bla bla ...

if ($offset >= $limit) {
$newoffset = $offset-$limit;
echo ("<a href=\"$PHP_SELF?offset=$newoffset&keywords=$keywords\"><b><< Previous</b></a>&nbsp; | ");
} else {
echo ("<< Previous&nbsp; | ");
}

$pages = intval(ceil($num_results/$limit));

for ($i = 1; $i <= $pages; $i++) {
$newoffset = $limit*($i-1);
if ($newoffset == $offset) {
echo ("$i | ");
} else {
echo ("<a href=\"$PHP_SELF?offset=$newoffset&keywords=$keywords\"><b>$i</b></a> | ");
}
}

if ($num_results-$offset > $limit) {
$newoffset = $offset+$limit;
echo ("&nbsp;<a href=\"$PHP_SELF?offset=$newoffset&keywords=$keywords\"><b>Next >></b></a>");
} else {
echo ("&nbsp;Next >>");
}

... bla bla bla ...

THANKS TO EVERYBODY !!![URL]

Jason
06-25-2003, 10:14 PM
what do you need help with in those "bla bla blah" sections? and that query, make more sense with it...


Jason

mordred
06-25-2003, 11:55 PM
Try to narrow down your problem so we can get a better grasp where we should look for an error. What is the problem actually? What specific part does not work as expected, and what is it expected to do?

Apart from that, I can only recommend having a look at PEARs Pager/Pager_Sliding classes, though you need some OOP knowledge to implement them.
http://pear.php.net/package-info.php?pacid=32
http://pear.php.net/package-info.php?pacid=136

maltrecho
06-27-2003, 11:22 AM
Well, that is all the code needed to understand. Just one thing was missing (sorry), The query finishes with:

LIMIT $offstet, $limit";

But the rest of the code isn't needed. The user sends a search term (keywords) and the results show up. Everythig works fine, but I would like to cut the total pages (previous 1 2 3 4 5 6 7 8 9 10 next) with something like this (previous 1 2 3 ... 8 9 10 next) using this code.

The queries take two results: the total results that match with the keywords (SELECT SQL_CALC_FOUND_ROWS ), and the first ten rows matching the keywords ($limit).