Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #11591

Re: splitting list into columns

Message-ID <1457114.2ZHQPtZS3d@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-10-26 03:04 +0200
Subject Re: splitting list into columns
Newsgroups comp.lang.php
References <powohrz248gd.1bjaj4r4el9yc$.dlg@40tude.net> <52670aad$0$9522$9b4e6d93@newsspool1.arcor-online.net>

Show all headers | View raw


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@news.demon.co.uk> (2004)

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

splitting list into columns richard <noreply@example.com> - 2013-10-22 18:19 -0400
  Re: splitting list into columns Christoph Michael Becker <cmbecker69@arcor.de> - 2013-10-23 01:31 +0200
    Re: splitting list into columns richard <noreply@example.com> - 2013-10-22 20:53 -0400
      Re: splitting list into columns Evan Platt <evan@theobvious.espphotography.com.invalid> - 2013-10-22 18:40 -0700
    Re: splitting list into columns Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-26 03:04 +0200
      Re: splitting list into columns Christoph Michael Becker <cmbecker69@arcor.de> - 2013-10-27 17:42 +0100
        Re: splitting list into columns Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-10-28 01:02 +0100
          Re: splitting list into columns Christoph Michael Becker <cmbecker69@arcor.de> - 2013-10-30 01:34 +0100
  Re: splitting list into columns Arno Welzel <usenet@arnowelzel.de> - 2013-10-23 23:24 +0200
    Re: splitting list into columns Frank Thomason <Frank@example.net> - 2013-10-24 08:35 -0400
      Re: splitting list into columns Jerry Stuckle <jstucklex@attglobal.net> - 2013-10-24 09:01 -0400
        Re: splitting list into columns Frank Thomason <Frank@example.net> - 2013-10-24 10:05 -0400
  Re: splitting list into columns Biju S <biju@pixnnova.com> - 2013-10-29 22:38 -0700

csiph-web