How would you go about splitting a long list of topics on page into multiple pges with Pages: 1, 2, 3, ... sort of thing.
A bit like this forum
Printable View
How would you go about splitting a long list of topics on page into multiple pges with Pages: 1, 2, 3, ... sort of thing.
A bit like this forum
Well, it just depends on what you are dealing with.
If you are grabbing results from a MySQL database like vB, then you could just use the following code.
--------------------------------
If (!($select = mysql_query("SELECT ID FROM table"))) {
echo("Error querying database.");
}
$count = mysql_num_rows($select);
// The number you divide with is the number you want on each page
$num_pages1 = $count/10;
$num_pages = settype($num_pages1, "integer");
If ($num_pages1 <> $num_pages) {
$num_pages = $num_pages + 1;
}
// num_pages is now the total number of pages
For ($i=1; $i<=$num_pages; $i++) {
echo("<a href=\"$PHP_SEL?page=$i\">$i</a> ");
}
--------------------------------
That code should work, but I haven't tested it so I'm not 100% sure.
What that code does is query the mysql table and then counts how many results there are. Then it takes that number and divides it by how many results are going to be on each page, and that gives you a number. We then make that number into an integer so that we can know exactly how many pages there are. If the original number isn't an integer, then we know it wasn't a whole number so we need to add one to the page number and then we display each page number onto the page.
It will produce something like:
-------------------
1 2 3 4 5 etc...for however many pages there are
-------------------
Note that the code will only display the page numbers and not the results for each page. You have to use a SELECT statement with the text "LIMIT $start,$stop" at the very end of the query, with $start equalling the result number to start from and $stop equalling the result number to end with. Remember, the results always start from 0, so you could use "Select variables FROM table LIMIT 0,10" to display to results per page.
I hope this helps.
Chris Roane
Thank you very much Chris. This is exactly what I was looking for. You have given me a start.
I was just wondering what exactly does the settype() function do? is it necessary?
All the settype() function does is change the type of the variable. For more information on this function, you can go to http://www.php.net/manual/function.settype.php .
In the previous code I just gave you, we divided the total number of results by the amount on each page. So it counted 25 results, and if you had 10 results per page, it would get the value of 2.5. But, it can't stay like that because we need the total amount of pages to be a whole number, so we turn it into an integer which makes "2.5" into just "2", but that isn't true because in reality, we will need 3 pages and not just 2. We then see if the original number was a whole number and if it wasn't, we add "1" to the new "integer" number which then makes the total number of pages "3", which is correct.
Does that make sense?
Chris Roane
settype() definition from php.net:
--------------------------------------
Sets the type of variable var to type.
Possibles values of type are:
"integer"
"double"
"string"
"array"
"object"
---------------------------------------
I personally use ceil($NumberOfRecords/10). It works fine with me
Thanks Chris, You have been extremely helpful.
I really appreciate it