1

I am trying to print number vertically and it must be in group

here is my code

$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>";
}

out of above

enter image description here

but i want output like for the first column

enter image description here

and next group number will start from where first group number end. in this case next group start from 25

please ask if any doubt

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Bhavik Patel
  • 789
  • 6
  • 20

4 Answers4

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>";
}

This works:

enter image description here

Also, you can change number of digits, columns or size of column

rNix
  • 2,457
  • 1
  • 22
  • 28
  • Problem with this : put $nums=31, 30 is missing and it should print up to 31. this is printing to 32. by the way this is good . i am voting up for logic – Bhavik Patel Nov 08 '13 at 12:57
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";
    }
}

This code will print the number in the requested format. You need to modify according to your need.

Gaurav
  • 28,447
  • 8
  • 50
  • 80
0

may be i am mad , made a simple alter .... try this

$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>";
}

Thanks !

internals-in
  • 4,798
  • 2
  • 21
  • 38
0

This May work

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

}
vijaykumar
  • 4,658
  • 6
  • 37
  • 54