【问题标题】:PHP echo/print value on for every 4th loop (start of count is 0)每 4 个循环打开 PHP 回显/打印值(计数开始为 0)
【发布时间】:2021-08-06 10:47:35
【问题描述】:

如果我的起始计数为 0,我如何为每个特定循环(第 4 个)回显一个值? 例如计数为 0,1,2,3,4,6,7,8,9,10,11,12 ,13

我需要它在以下数字上回显/打印:3,8,12,16

这是我的循环代码:

$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>';

它有点工作,但不幸的是,它开始在 #0

上打印 $VALUE

所以它打印在以下计数/数字上:0,4,8,12

而不是正确的:3,8,12,16

【问题讨论】:

    标签: php loops for-loop


    【解决方案1】:

    试试这个:

    $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>';
    

    【讨论】:

    • 哦,我忘了,对不起!完成 :D 再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2016-03-17
    相关资源
    最近更新 更多