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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.