PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Paging (Photography web hosting)
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Paging through a long set of records The grid of eight thumbnails fits quite comfortably in the gallery, but what if you have 28 or 48? The answer is to limit the number of results displayed on each page, and build a navigation system that lets you page back and forth through the results. You ve seen this technique countless times when using a search engine; now you re going to learn how to build it yourself. The task can be broken down into the following two stages: 1. Selecting a subset of records to display 2. Creating the navigation links to page through the subsets Both stages are relatively easy to implement, although it involves applying a little conditional logic. Keep a cool head, and you ll breeze through it. Selecting a subset of records Limiting the number of results on a page is simple. Add the LIMIT keyword to the SQL query like this: SELECT * FROM images LIMIT startPosition, maximum The LIMIT keyword can be followed by one or two numbers. If you use just one number, it sets the maximum number of records to be retrieved. That s useful, but it s not what we need to build a paging system. For that, you need to use two numbers: the first indicates which record to start from, and the second stipulates the maximum number of records to be retrieved. MySQL counts records from 0, so to display the first six images, you need the following SQL: SELECT * FROM images LIMIT 0, 6 To show the next set, the SQL needs to change to this: SELECT * FROM images LIMIT 6, 6 There are only eight records in the images table, but the second number is only a maximum, so this retrieves records 7 and 8. To build the navigation system, you need a way of generating these numbers. The second number never changes, so let s define a constant called SHOWMAX. Generating the first number (call it $startRecord) is pretty easy, too. Start numbering the pages from 0, and multiply the second number by the current page number. So, if you call the current page $curPage, the formula looks like this: $startRecord = $curPage * SHOWMAX; And for the SQL, it becomes this: SELECT * FROM images LIMIT $startRecord, SHOWMAX
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.