v. 0.β.3
Back to the Top
Instead of displaying records one per row, you want to display them three per row, or four per row or something like that?
Let's say that there are three columns. The amount of padding needed to finish off the last row for various numbers of items is worked out in the following table. The first two columns are straightforward. The other three illustrate my working in getting from the numbers in the "items" column to the numbers in the "padding" column - in other words, how I figured out how much padding each number of items needed.
items padding 2-padding = b 3*?+b = a a-2 = items
0 0 2-0 = 2 3*0+2 = 2 2-2 = 0
1 2 2-2 = 0 3*1+0 = 3 3-2 = 1
2 1 2-1 = 1 3*1+1 = 4 4-2 = 2
3 0 2-0 = 2 3*1+2 = 5 5-2 = 3
4 2 2-2 = 0 3*2+0 = 6 6-2 = 4
5 1 2-1 = 1 3*2+1 = 7 7-2 = 5
6 0 2-0 = 2 3*2+2 = 8 8-2 = 6
7 2 2-2 = 0 3*3+0 = 9 9-2 = 7
8 1 2-1 = 1 3*3+1 =10 10-2 = 8
9 0 2-0 = 2 3*3+2 =11 11-2 = 9
10 2 2-2 = 0 3*4+0 =12 12-2 =10
items padding padding = 2-b b = a%3 a = items+2
Looks complicated, but really the hardest thing about it is counting to three. I've worked out how much padding is needed for different numbers of items, and now I'm trying to figure out how to relate those two columns ("items" and "padding") to each other.
Well, for a start the numbers in "padding" decrease while those in "items" increase (because the amount of padding needed decreases as items are added to the last row). Subtracting all the numbers in the "padding" column from 2 creates a list of numbers in increasing order while still keeping them between 0 and 2. In other words, I've figured out how many items there are in the last row (with an imaginary empty row added whenever I run out of padding).
The sequence doesn't look like it's increasing - what with all the jumping from 2 down to 0 - but now that I'm counting items I can take into account the full rows. The first jump from 2 to 0 is because I filled a row of three and started a new empty row. If I now count items in full rows I really will have a steadily increasing sequence (this is the hard part: how many times three do I need to add? Hint: I'm adding one item at a time).
Ooh, almost. That fourth column is so close - but every line has two items too many. What can we do? Well, we can subtract two. And that gets us the number of items. That's the good news. The bad news is that now we have to turn around and go back. We started with the padding, and worked out how many items would require it. We were supposed to start with the items and work out the padding.
But the good news is that we've done all the work. If subtracting 2 from the fourth column gives us the fifth column, then adding 2 to the fifth column gives us the fourth column. And so on. (This is where the modulus operator comes in: it lets you throw away full rows as you complete them, leaving you with the last row and its padding). That's what the last line of the table shows: how to get from the number of items to the amount of empty padding (add 2, remainder modulo 3, subtract from 2). We can write that in code. Let's do that thing.
Note that purely for the sake of example this code has been written to display in each cell a single field from a MySQL result set. If your needs differ, you'll need to change this to suit whatever data source you're actually using. Ideally, the table-generation code should be independent of the data-collection code.
// We want three columns:
$display_columns = 3;
// We do the query, and find out how many items we'll need to list.
$result = mysql_query($sql);
$count = mysql_numrows($result);
// We have enough to figure out how many empty spaces there'll be left at
// the bottom of the table.
$padding = ($display_columns-1)-(($count-1)%$display_columns);
//
// We also know enough to know how many rows there'll be, but I for one
// don't really care about that right now.
echo '<table>';
// Let's begin our loop.
for($i=0; $i<$count; $i++)
{ // Whenever $i is a multiple of $display_columns (including 0),
// it's a sign that we're hitting the start of a new row.
if($i%$display_columns == 0)
echo '<tr>';
//Now for an item.
echo '<td>';
echo mysql_result($result,$i,'value');
echo '</td>';
//Whenever $i is one less than a multiple of $display_columns,
// it's a sign that we're hitting the end of the current row.
if($i%$display_columns == $display_columns-1)
echo '</tr>';
}
// That's all the items done; now we just need to pad the table out and
// wrap up. We don't need to pad at all if $padding is zero.
if($padding!=0)
{ for($i=0;$i<$padding;$i++)
echo '<td></td>';
echo '</tr>';
}
echo '</table>';