Stephen68 Posted November 23, 2007 Share Posted November 23, 2007 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 More sharing options...
GingerRobot Posted November 23, 2007 Share Posted November 23, 2007 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 More sharing options...
Stephen68 Posted November 24, 2007 Author Share Posted November 24, 2007 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 More sharing options...
Barand Posted November 24, 2007 Share Posted November 24, 2007 Not necessary <?php $numrows = 1; $rowsperpage = 20; $pages = ceil($numrows/$rowsperpage); echo $pages; // gives 1 ?> Link to comment https://forums.phpfreaks.com/topic/78572-solved-math-help/#findComment-398018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.