Jump to content

[SOLVED] Math help


Stephen68

Recommended Posts

I have paging through some results from a DB and everything is working find until there is only one record to show.

I'm thinking it has to do with the math I use to find max pages, I think it is round up and there for showing that there

is another page to show when there really is not

 

$numrows = mysql_query("SELECT COUNT(NID) FROM news");

$rowsPerPage = 20;

 

$maxPage = ceil($numrows/$rowsPerPage);

 

is the code I'm suing to find $maxPage, is there a better was to do this?

 

cheers!

Stephen

Link to comment
https://forums.phpfreaks.com/topic/78572-solved-math-help/
Share on other sites

Im surprised it's working at all, mysql_query() returns a result resource - not the actual result of the query. Try:

 

<?php
$sql = mysql_query("SELECT COUNT(NID) FROM news");
$numrows = mysql_result($sql,0);
$rowsPerPage = 20;
$maxPage = ceil($numrows/$rowsPerPage);
?>

 

And yes, you do need to be rounding up the number.

Link to comment
https://forums.phpfreaks.com/topic/78572-solved-math-help/#findComment-397574
Share on other sites

Ok I got that but the problem is when the search returns only one record.

 

$maxPage = ceil($numrows/$rowsPerPage);

 

This returns the value of 2 when only one record is found in the search. I'm assuming that it rounded

up and there for making it look like there are two pages of results. Is there a way to make this work

right maybe by an if statment or something

 

if ($numrows == 1) {
    $maxPage = 1;
}else{
    $maxPage = ceil($numrows/$rowsPerPage);
}

 

Maybe something like that?

Link to comment
https://forums.phpfreaks.com/topic/78572-solved-math-help/#findComment-397875
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.