垂直打印数字并对其进行分组

时间:2013-11-08 08:13:40

标签: php

我正在尝试垂直打印数字,它必须在组中

这是我的代码

$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
    echo "<div class='group'>";
    for ($i = 1; $i <= $rows; $i++) {
        for ($j = $i; $j <= 24; $j = $j + $rows) {
            $count++;
            if($count>$nums){
                break;
        }
            echo "<div class='fleft'>$count</div>";
        }
        echo "<div class='clear'></div>";
    }
    echo "</div>";
}

以上

enter image description here

但我希望输出像第一列

enter image description here

,下一个组号将从第一个组号结束的地方开始。在这种情况下,下一组从25开始

请询问是否有任何疑问

4 个答案:

答案 0 :(得分:2)

$nums = 105;
$rows = 8;
$colsize = 3;

$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
    echo "<div class='group'>";

    $modulo = 0;
    $correction = 0;
    $rest = $nums - $count;
    if ($rest < $groupsize) {
        $empty = $groupsize - $rest;
        $correction = floor($empty / $colsize);
        $modulo = $empty % $colsize;
    }
    for ($i = 1; $i <= $rows; $i++) {
        $colind = 0;
        for ($j = $i; $j <= $groupsize; $j = $j + $rows) { 
            $count++;
            if ($count > $nums) {
                break;
            }
            $val = $j + ($g * $groupsize);

            $val -= $colind * $correction;
            $modcor = $colind - ($colsize - $modulo);
            if ( $modcor > 0 ) {
                $val -= $modcor;
            }
            echo "<div class='fleft'>" . $val . "</div>";
            $colind++;
        }
        echo "<div class='clear'></div>";
    }
    echo "</div>";
}

这有效:

enter image description here

此外,您可以更改列的位数,列数或大小

答案 1 :(得分:0)

for($group = 0; $group < 3; $group++){
    for($row =1 ; $row <= 8; $row++){
        for($col = 0; $col <= 2; $col++){
            echo ($group*24)+ $row + 8 * $col; echo " ";
        }
        echo "\n";
    }
}

此代码将以请求的格式打印数字。您需要根据需要进行修改。

答案 2 :(得分:0)

可能是我疯了,做了一个简单的改变.... 试试这个

$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0;  //added a counter

for ($g = 0; $g <= $group; $g++) {

    echo "<div class='group'>";
    for ($i = 1; $i <= $rows; $i++) {
        $letCounter=0; //reset counter on each loop
        for ($j = $i; $j <= 24; $j = $j + $rows) 
        {
         $count++;
         if($count>$nums)
         {break;}
        //made an alter  in the below line , some math :)
         echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";  
        }
        echo "<div class='clear'></div>";
    }
    echo "</div>";
}

谢谢!

答案 3 :(得分:0)

今年五月的工作

$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{

        if($c%$group== 1)
        {
            echo "Group Start";
            $flag = false;
        }
        for ($i = 1; $i <= $rows; $i++) {
                $count++;
                echo "<div class='fleft'>$count</div>";

            echo "<div class='clear'></div>";

        }
        echo "Line End";
        if($c%$group == 2&& $flag)// Check here for your requirement        
            echo "Group End </br>";
        $flag = true;

}