-1

i have a code like this

$count=15; // i manually initialising a value that not satisfying by folling condition
$low_limit=0;
$up_limit=10;
$num_pages=0;

(some loop) {

   if (($count >= $low_limit) && ($count <= $up_limit))
    {
        $num_pages=$numpages+1;
        echo $num_pages;
    }

   $low_limit=$up_limit+1;
   $up_limit=$up_limit+10;

} // loop ends

my logic was

$count is a variable // this value can be frequently changed

$low_limit and $up_limit are value ranges // 0-10 , 10-20 , 20-30 ,etc

$num_pages is a variable that return

1 if $low_limit= 0 and $up_limit= 10

2 if $low_limit= 11 and $up_limit= 20

3 if $low_limit= 21 and $up_limit= 30 and so on

here the $low_limit and $up_limit can be any number (but multiple of 10). it may be upto 50,000.

what will be in some loop .?

How i construct this program, i searched a lot, but i only find programs which checks a number between a range.

Any help would be greately appreciated.

Community
  • 1
  • 1
Shifana Mubi
  • 197
  • 1
  • 3
  • 17
  • Your code (condition) should work as expected. What do you want to achieve? What _program you want to construct_? – pavel Apr 22 '15 at 08:20
  • what will be the condition .? i know there shoud be a condition to incremant $low_limit and $up_limit .? but what is that condition.? – Shifana Mubi Apr 22 '15 at 08:54
  • in your code there is only `if (($count >= $low_limit) && ($count <= $up_limit))`, which is false with these variables. Num page stays zero, `$low_limit=10; $up_limit=20;`. Now, the script ends and do nothing more. You probably want to make something but I don't know what exactly. Try to update your question. – pavel Apr 22 '15 at 08:56
  • yes i know that, if the $count=5 then this condition was successful. but $count can be any number and it can be changed based upon no: of rows returns from mysql – Shifana Mubi Apr 22 '15 at 08:59
  • And what you need to find? Number of pages? Low- and up-limit based on `$count` value? Eg. `$count = 34`, and you need to get `numpage=4`, `$low_limit=30` and `$up_limit=40`? Or what you need to achieve? – pavel Apr 22 '15 at 09:01

1 Answers1

1

Due to comments above, it should works as expected:

$count = 34; // any number
$per_page = 10; // it's fixed number, but... 
$num_page = ceil($count / $per_page); // returns 4
$low_limit = ($num_page - 1) * $per_page; // returns 30
$up_limit = $num_page * $per_page; // returns 40

Between record number 30 and record number 40 are 11 records, not 10.

There are more ways how to fix that:
1. compare < ... <=: $low_limit < $count <= $up_limit
2. compare <= ... <: $low_limit <= $count < $up_limit
3. set limits 1-10, 11-20, 21-30, etc. (simply +1 to $low_limit on 4th line above)
4. set limits 0-9, 10-19, 20-29, etc. (simply -1 to $up_limit on 5th line above)

pavel
  • 26,538
  • 10
  • 45
  • 61