0

Hi lets say we have this values :

$total = 317;
$x = 5;
$numpages = ceil($total / $x); 

i need to loop over $total value and echo result like this

i=1 , numpage = 1
i=2 , numpage = 1
i=3 , numpage = 1
i=4 , numpage = 1
i=5 , numpage = 1

i=6 , numpage = 2
i=7 , numpage = 2
i=8 , numpage = 2
i=9 , numpage = 2
i=10 , numpage = 2

until....

i=317 , numpage = 64

i don't want to use manual if statement like this

for ($i = 0; $i <= $total; $i++) {
if($i <= 5)
numpage = 1

if($i <= 10)
numpage = 2

}

becuase $x = 5; could change as i want i need formula that works with any x value thanks

0

5 Answers 5

1

You can do it like this:

for ($i = 0; $i <= $total; $i++) {
 $numpage = ceil($i/5);//$ was missing 

 }
Sign up to request clarification or add additional context in comments.

Comments

0

Here it is,

$total = 100; //Can be any number
$x = 5;   //Can be any number
for ($i = 1; $i <= $total; $i++) {
     if($i % $x != 0){
        $pageNo = floor($i / $x) + 1; 
     } 
     echo "Page $pageNo for value $i\n";
}

DEMO.

Comments

0

Try this:

$total  = 317;
$x      = 5;

for ($i = 0; $i <= ($total-1); $i++) 
{   
    $currentPage = floor($i / $x) + 1;
    echo 'i = '. ($i+1). ' , numpage = '.$currentPage.'<br />'; 
}

Comments

0
for ($i = 1; $i <= $total; $i++) {
 $numpage = ceil($i/5);
    print "i = $i, numpage = $numpage"."\n";
 }

2 Comments

Please explain what your code does. Just posting code is usually not a good answer on SO
The most important here is the function ceil man ceil and echo result ("\n" because i test this in console)
0

You can try the below code

$numpage = 0;
for ($i = 1; $i <= $total; $i++) {

    $numpage = (($i-1) % 5 == 0)?$numpage +1:$numpage;
    echo 'i = '.$i.' , numpage '. $numpage.'<br/>';

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.