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 #183455 is a reply to message #183406] Sat, 26 October 2013 01:04 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Christoph Michael Becker wrote:

> richard wrote:
>> 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?
>
> To create a table from a 1-dimensional indexed array $values in N-order,
> you may use the following outline:
>
> $rows = 40;
> $cols = 5;
> echo '<table>';
> for ($i = 0; $i < $rows; ++$i) {
> echo '<tr>';
> for ($j = 0; $j < $cols; ++$j) {
> echo '<td>';
> $n = $j * $rows + $i;
> echo $values[$n];
> echo '</td>';
> }
> echo '</tr>';
> }
> echo '</table>';

Or you could use

echo '<table>'
. '<tr>'
. implode('</tr><tr>',
array_map(
function ($e) {
return '<td>' . implode('</td><td>', $e) . '</td>';
},
array_chunk($values, $cols)
)
)
. '</tr>'
. '</table>';

and additionally use array_slice() if you wanted to limit the output to
$rows rows :)

See also:

<http://php.net/array_chunk>

(the exploit or false entry apparently has disappeared; Chromium no longer
warns about php.net)


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
[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 03:17:11 GMT 2024

Total time taken to generate the page: 0.03956 seconds