Nevermind this question...i added $i-- to the script and it is fixed!
This is a brain teaser that's been bugging me for several hours. I'm building a multicolumn table layout in PHP to lay out an answer sheet for a multiple choice test. I want to be able to feed it an arbitrary number of questions ($num_questions) and number of columns ($num_columns) and have it lay them out in columns with the minimum number of rows to fit them all. So when I have $num_questions=30 and $num_columns=5, it would produce a layout like this:
1 7 13 19 25
2 8 14 20 26
3 9 15 21 27
4 10 16 22 28
5 11 17 23 29
6 12 18 24 30
The problem is that tables must be rendered horizontally, so in this instance we would be rendering the questions in this order 1,7,13,19,25,2,8,14,20,26,...
So my question is, what is the algorithm that will calculate the current $question_number for a given position in the grid. The algorithm I've come up with so far is working for low numbers of questions and columns such as the example above, but I know there's a better way that will work for all possible combinations of test sizes and column specifications. Here's the algorithm I came up with so far:
<table border="1">
<?php
$num_questions=30;
$num_columns=5;
$column_count=0;
$row_count=1;
//calculate the number of rows we will need in our grid.
//we round up here in case there's any spillover
$num_rows=ceil($num_questions/$num_columns);
for($i=0;$i<$num_questions;$i++){
$column_count++;
if( $column_count==1 ){ ?>
<tr class="table-heading1">
<?php } //end if
if( (1 * $row_count) + ($num_rows * ($column_count-1) ) <= $num_questions){
?>
<td valign="middle"><?php echo (1 * $row_count) + ($num_rows * ($column_count-1) ) ?></td>
<?php }else{$i--;}
if($num_columns == $column_count){ ?>
</tr>
<?php $row_count++; $column_count=0;} //end if
} //end for
?>
</table>
The interesting part of the above code is this:
(1 $row_count) + ($num_rows ($column_count-1) )
That's what I'm using to caculate the number that needs to be displayed in any given table cell as we loop from 1-$num_questions. I know there's a better formula for determining this, but I can't ficure it out. Here's my reasoning behind the above formula:
We know the first number of each row will simply be the same as the count for the row we're in. So then we just count by a specific increment to determine the number for other columns. In other words, the number at any given position should be it's row number plus the product of the number of rows and the current column number-1. The problem here is when data doesn't perfectly fill all the cells. We want the last column to be incomplete. We will still need to iterate past the unfilled cells in the last column to fill in the last rows of the table, so we simply don't fill it. To determine if the cell should be filled or not, I simply test the algorithm to see if it generates a number higher than the highest number in our set ($num_questions), and if so, we don't fill it. So basically the above code does all that, but not once you get past 50 or so questions, it craps out because of my conditional. So I guess that's what needs fixing is just the conditional that determines whether or not to fill the table cell with the number. Try out the above code with $num_questions=30 and then with $num_questions=100 and $num_columns=7 and see the problem in action.
******NOTE I have fixed the test code above to account for the problem...now if only the more complicated version worked right in my program!