FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » splitting list into columns
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: splitting list into columns [message #183420 is a reply to message #183405] Wed, 23 October 2013 21:24 Go to previous messageGo to previous message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma:
Senior Member
richard, 2013-10-23 00:19:

> I want to display the 200 item list into 5 columns.
> What I have now works, except that the 2 to 5th columns are not adding
> properly.
> What do I need to change to make it work right?
[...]

Try to think different. Software development is not just about line by
line of code. First *think* about your problem! I think your problem is
not how to break down problems at all.

Ok - i'll try to explain.

Problem: 200 items in 5 columns. Every column should contain items one
by one.

To simplify this - 17 items in 5 colums. But the general principle is
the same. It doesn't matter if you do this with 17, 200 or 5000 items,
the solution does not depend on this number. The items are numbered 0 to 16.

The result you want:

0 4 8 12 16
1 5 9 13
2 6 10 14
3 7 11 15

So - how to achieve this?

Which item is the first one in the second (third, fourth...) column of
the first row?

Divide the total number of items (17) by 5 - since there are 5 columns:

17/5 = 3.4

Then use the ceiling of it, since it is not possible to output just the
part of an item (0.4) at the and of a column and using the floor of only
3 items per column would be too few as 17/3 is more than 5 - and we only
have 5 columns for our items:

ceil(3.4) = 4

So now you know the number of items which will go in one column: 4.

So the first item is item 0, the next one item 4, the next item 8 and so on.

The number of rows is also known now - it's the calculated number of
items per column since after 4 items in one column a new column starts.

Now you can output the items row by row, and you only output something,
if the calculated item number is not larger than the total number of items.

Just "quick & dirty" without any formal check - so DON'T JUST COPY &
PASTE but try to UNDERSTAND:


<?php
$playme = array(
'Item 0',
'Item 1',
'Item 2',
'Item 3',
'Item 4',
'Item 5',
'Item 6',
'Item 7',
'Item 8',
'Item 9',
'Item 10',
'Item 11',
'Item 12',
'Item 13',
'Item 14',
'Item 15',
'Item 16'
);

// $totalitems is the number of items in our array $playme
$totalitems = count($playme);

// $columns is our number of columns we want to output
$columns = 5;

// $items_per_column is the number of items per column we need
$items_per_column = ceil( $totalitems / $columns );

// Now output as many rows as we have items per column
print '<table>';
for( $row_number = 0 ; $row_number < $items_per_column; $row_number++ )
{
// Start a new row
print '<tr>';

// Outout all columns of this row
for( $column_number = 0; $column_number < $columns; $column_number++ )
{
// Calculate the number of the item to be put out
$item = $column_number * $items_per_column + $row_number;

print '<td>';
if($item < $totalitems)
{
// We still have an item to be put out, so do it
print $playme[$item];
}
else
{
// We already put out every item - so nothing to output,
// just a placeholder to fill the remaining column
print '&nbsp;';
}
print '</td>';
}

// End the row
print '</tr>';
}
print '</table>';
?>


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Unzip password-protected ZIP file in RAM?
Next Topic: PDO - Cannot retrieve warnings with emulated prepares disabled
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 02:16:58 GMT 2024

Total time taken to generate the page: 0.04807 seconds