Displaying results hyperlink numbers like a search engine

吃可爱长大的小学妹 提交于 2019-12-25 05:25:21

问题


Does anyone know of a good resource on how to create the hyperlink numbers at the bottom of a results page as search engines do to load the next number of results?

The page would load the first 10 results. And then if you click on the number, it loads corresponding results in that 10 number range.

Example:

0-10 -> show no numbers

11-20 -> show 1, 2

21-30 -> 1, 2, 3

up to 50 anything more than 50 does 1,2,3,4,5.....67 [last number].

My thoughts so far (I'm doing this in PHP/mysqli but the logic is more important than the code):

$total = mysqli_num_rows($result) //total number of reqults from sql query
if ($total>10) {
$last = intval($total/10) + 1 //get the last number of the results

if($last <= 5){
for ($i, $i<$last, $i++){
//print the numbers as hyperlinks
} else {
  //print 1 through 5 ... then $last
}
}

This though is static from only 1-5...last number while the search engines have it so if you click on the number, it remembers that number and bases the new logic on it. So if I click on the 5 in my formula, it should change to something like:

[previous] 3,4,5,6,7....67 [next]

And then I would just pass the number to the page itself again and limit the results based on what number was passed. Any suggestions also on the best way to pass the info?


回答1:


You are looking for a pagination script. Visit this link The page is in Arabic but forth post is of pagination and you can download source for english or arabic version of pagination




回答2:


Basically, you need two values to create a pagination, a limit and a offset.

1.The limit is the amount of items your are displaying at the same time.
2.The offset is from where you started your query.

So, let's say you have 5 items in each page and 25 items total.

In your query, you have to limit 5,0 (the amount of items and the position the query will start).

Now, if you divide 5(limit)/25(total) and you'll get 5 (amount of pages).

Now in page 0 (the start) you can get the offset by multiplying the page number with the limit, so 0 (page) * 5 (limit) gives you 0 (in the first page you start from the offset 0).

Now in the 3rd page, you multiply 3(page) * 5 (limit) it gives you 15, which means in page 3 (or four if you take into account that you actually started at page 0) you will display from offset 16 to 20.

Finally in page 4 (which to your users will be page 5 because they started at page 1, not page 0) you will display from offset 21 to 25 which are all the items in your query.



来源:https://stackoverflow.com/questions/13044647/displaying-results-hyperlink-numbers-like-a-search-engine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!