Skip to content
Advertisement

PHP echo/print value on for every 4th loop (start of count is 0)

How do i echo a value for each certain loop (4th) if my starting count number is 0? For example, the count is 0,1,2,3,4,6,7,8,9,10,11,12,13

I need it to echo/print on the following numbers: 3,8,12,16

Here’s my loop codes:

$Num_Rows = mssql_num_rows($query);
    
    $Per_Page = 16;   // Per Page
    
    $Page = $_GET["Page"];
    if(!$_GET["Page"]){
        $Page=1;
    }
    
    $Prev_Page = $Page-1;
    $Next_Page = $Page+1;
    
    $Page_Start = (($Per_Page*$Page)-$Per_Page);
    if($Num_Rows<=$Per_Page){
        $Num_Pages =1;
    } else if(($Num_Rows % $Per_Page)==0)    {
        $Num_Pages =($Num_Rows/$Per_Page) ;
    } else {
        $Num_Pages =($Num_Rows/$Per_Page)+1;
        $Num_Pages = (int)$Num_Pages;
    }
    $Page_End = $Per_Page * $Page;
    IF ($Page_End > $Num_Rows) {
        $Page_End = $Num_Rows;
    }
    $VALUE = 'last';
    echo '<ul>';
    for($i=$Page_Start;$i<$Page_End;$i++) {
        <li class="<?php if ($pages % 4 == 0) { echo $VALUE; }?>">Test</li>
    }
    echo '</ul>';

It kind of works but unfortunately, it starts printing the $VALUE on #0

So it prints on the following count/numbers: 0,4,8,12

Instead of the correct ones: 3,8,12,16

Advertisement

Answer

Try this:

$Num_Rows = mssql_num_rows($query);

    $Per_Page = 16;   // Per Page
    
    $Page = $_GET["Page"];
    if(!$_GET["Page"]){
        $Page=1;
    }
    
    $Prev_Page = $Page-1;
    $Next_Page = $Page+1;
    
    $Page_Start = (($Per_Page*$Page)-$Per_Page);
    if($Num_Rows<=$Per_Page){
        $Num_Pages =1;
    } else if(($Num_Rows % $Per_Page)==0)    {
        $Num_Pages =($Num_Rows/$Per_Page) ;
    } else {
        $Num_Pages =($Num_Rows/$Per_Page)+1;
        $Num_Pages = (int)$Num_Pages;
    }
    $Page_End = $Per_Page * $Page;
    IF ($Page_End > $Num_Rows) {
        $Page_End = $Num_Rows;
    }
    $VALUE = 'last';
    echo '<ul>';
    for($i=$Page_Start;$i<$Page_End;$i++) {
        <li class="<?php if (($pages+1) % 4 == 0) { echo $VALUE; }?>">Test</li>
    }
    echo '</ul>';
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement