Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function/ maths help

Status
Not open for further replies.

ckennerdale

Programmer
Dec 23, 2000
158
GB
I am writing code to give me a next, previous and last page option on a set of databse records- prety standard.

I have these variables

$records_per_page = 10
$total_num = mysql_num_rows($SQL2);
$total_num_page = $total_num/$records_per_page;
To link to the last page in the record set I have a line like this

echo &quot;<a href=\&quot;$PHP_SELF?action=list_records&cur_page=$total_num_page\&quot;>Last</a>&quot;;

So far so good.

However if the total amount of records do not divide equally by the records per page, my link to the Last page goes to an odd number.


How can I check if the value is not a round number, create it into a round number and pass the value back- so that if the last page only has 4 links on it, it would still be called say page 3 instead of page 2.5?

Thanks in advance Caspar Kennerdale
Senior Media Developer
 
Casper,

I think this should solve the problem. Instead of checking whether the value is a whole, or rounded number, just put the whole expression in to a round() statement.

$total_num_page = round( $total_num / $records_per_page );

This means that any result will be rounded to the nearest whole. However I think you will want to make sure that the number is rounded upwards, and NOT downwards, so that the last page ( incase of the decimal rounding down and giving you, e.g. 3 pages instead of the 3.4 pages you have) does not get missed out.

I think this should work then :

$total_num_page = round( ( $total_num / $records_per_page) + 0.4 );


Hope this helps

P.S. Check the syntax on the 'round()' statement as I'm doing this from memory Chris MacPherson
macpweb@hotmail.com
Bring on the new Browza's!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top