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


Groups > comp.lang.php > #11548 > unrolled thread

splitting list into columns

Started byrichard <noreply@example.com>
First post2013-10-22 18:19 -0400
Last post2013-10-29 22:38 -0700
Articles 13 — 8 participants

Back to article view | Back to comp.lang.php


Contents

  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

#11548 — splitting list into columns

Fromrichard <noreply@example.com>
Date2013-10-22 18:19 -0400
Subjectsplitting list into columns
Message-ID<powohrz248gd.1bjaj4r4el9yc$.dlg@40tude.net>
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?

http://mroldies.net/200/jukebox.php


<?php

include "arrayxx.php";

$min=1;
$max=40;

$bnumber=1;

while ($bnumber<=5) {

echo "<td>";

echo $min;
echo $max;

while ($min<=$max){

echo $playme[$min][2]."<br>";

$min++;

}

echo "</td>";

$min=$min+40;
$max=$max+40;
$bnumber++;

}

?>

[toc] | [next] | [standalone]


#11549

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2013-10-23 01:31 +0200
Message-ID<52670aad$0$9522$9b4e6d93@newsspool1.arcor-online.net>
In reply to#11548
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>';

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#11550

Fromrichard <noreply@example.com>
Date2013-10-22 20:53 -0400
Message-ID<byjm7um49xu3$.1h6wcbtjhspps$.dlg@40tude.net>
In reply to#11549
On Wed, 23 Oct 2013 01:31:26 +0200, 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:
> 

Which is basically what I decided on doing and works just fine.

[toc] | [prev] | [next] | [standalone]


#11551

FromEvan Platt <evan@theobvious.espphotography.com.invalid>
Date2013-10-22 18:40 -0700
Message-ID<m8ae695uep2nrprte7tdffrtn7p2jodr2e@4ax.com>
In reply to#11550
On Tue, 22 Oct 2013 20:53:40 -0400, richard <noreply@example.com>
wrote:

>Which is basically what I decided on doing and works just fine.

Apparently the words "thank you" are not in your vocabulary either.
-- 
To reply via e-mail, remove The Obvious and .invalid from my e-mail address.

[toc] | [prev] | [next] | [standalone]


#11591

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-10-26 03:04 +0200
Message-ID<1457114.2ZHQPtZS3d@PointedEars.de>
In reply to#11549
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)

[toc] | [prev] | [next] | [standalone]


#11598

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2013-10-27 17:42 +0100
Message-ID<526d4270$0$9517$9b4e6d93@newsspool1.arcor-online.net>
In reply to#11591
Thomas 'PointedEars' Lahn wrote:

> 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 :)

That is a nice solution (the applicative programming paradigm is so
elegant; unfortunately, I'm still not much used to it). :)

However, this algorithm will emit the table in Z-order.  I wonder if
there is an equally elegant solution for N-order.

> See also:
> 
> <http://php.net/array_chunk>
> 
> (the exploit or false entry apparently has disappeared; Chromium no longer 
> warns about php.net)

Some explanation about what has happened can be found on
<http://php.net/archive/2013.php#id2013-10-24-2>.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#11604

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-10-28 01:02 +0100
Message-ID<2504041.36zPCE0XX7@PointedEars.de>
In reply to#11598
Christoph Michael Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Christoph Michael Becker wrote:
>>> To create a table from a 1-dimensional indexed array $values in N-order,
>>> you may use the following outline:
>>>
>>> [loop]
>> 
>> 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 :)
> 
> That is a nice solution (the applicative programming paradigm is so
> elegant; unfortunately, I'm still not much used to it). :)
> 
> However, this algorithm will emit the table in Z-order.  I wonder if
> there is an equally elegant solution for N-order.

I presume you mean this:

  /**
   * Returns the transpose of a two-dimensional array.
   * 
   * NOTE: Does not preserve the indexes.
   * 
   * @author Codler
   * @param array $a
   * @return array
   * @see http://stackoverflow.com/a/3423692/855543
   */
  function array_transpose ($a)
  {
    array_unshift($a, null);
    return call_user_func_array('array_map', $a);
  }

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

You can additionally use array_map('array_slice', …) to limit the number of 
columns :)

>> See also:
>> 
>> <http://php.net/array_chunk>
>> 
>> (the exploit or false entry apparently has disappeared; Chromium no
>> longer warns about php.net)
> 
> Some explanation about what has happened can be found on
> <http://php.net/archive/2013.php#id2013-10-24-2>.

Thanks.


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)

[toc] | [prev] | [next] | [standalone]


#11640

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2013-10-30 01:34 +0100
Message-ID<52705409$0$6559$9b4e6d93@newsspool4.arcor-online.net>
In reply to#11604
Thomas 'PointedEars' Lahn wrote:

> Christoph Michael Becker wrote:
> 
>> However, this algorithm will emit the table in Z-order.  I wonder if
>> there is an equally elegant solution for N-order.
> 
> I presume you mean this:
> 
>   /**
>    * Returns the transpose of a two-dimensional array.
>    * 
>    * NOTE: Does not preserve the indexes.
>    * 
>    * @author Codler
>    * @param array $a
>    * @return array
>    * @see http://stackoverflow.com/a/3423692/855543
>    */
>   function array_transpose ($a)
>   {
>     array_unshift($a, null);
>     return call_user_func_array('array_map', $a);
>   }
> 
>   echo '<table>'
>     . '<tr>'
>     . implode('</tr><tr>',
>         array_map(
>           function ($e) {
>             return '<td>' . implode('</td><td>', $e) . '</td>';
>           },
>           array_transpose(array_chunk($values, $rows))
>         )
>       )
>     . '</tr>'
>     . '</table>';
> 
> You can additionally use array_map('array_slice', …) to limit the number of 
> columns :)

Great, thank you very much.  I was not aware that array_map(null, ...)
implements the zip function as know from functional programming
languages (and even then, I might not have found this elegant solution).

The return statement of array_transpose() could be written even more
elegantly in PHP 5.6, if argument unpacking[1] will be implemented:

  return array_map(...$a);

[1] <https://wiki.php.net/rfc/argument_unpacking>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#11562

FromArno Welzel <usenet@arnowelzel.de>
Date2013-10-23 23:24 +0200
Message-ID<52683E7C.6080208@arnowelzel.de>
In reply to#11548
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

[toc] | [prev] | [next] | [standalone]


#11571

FromFrank Thomason <Frank@example.net>
Date2013-10-24 08:35 -0400
Message-ID<52691423.1020702@example.net>
In reply to#11562
Arno - thank you for taking the time and effort to provide such a useful 
and respectful response to the question. If more posters spent their 
time answering questions in this manner, instead of calling each other 
trolls, this NG would be a much better place.

[toc] | [prev] | [next] | [standalone]


#11572

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-10-24 09:01 -0400
Message-ID<l4b5nd$dn6$1@dont-email.me>
In reply to#11571
On 10/24/2013 8:35 AM, Frank Thomason wrote:
> Arno - thank you for taking the time and effort to provide such a useful
> and respectful response to the question. If more posters spent their
> time answering questions in this manner, instead of calling each other
> trolls, this NG would be a much better place.
>

Frank,

Once you're read this newsgroup for a while, you'll figure out that 
richard is unable to understand anything beyond copy and paste.  Arno's 
explanation is well beyond richard's level of comprehension.  That's why 
the regulars here respond like they do - they understand posts like 
Arno's are not worth the time invested in writing it.

And please note - there is nothing in Arno's update which even the most 
basic programmer should understand.  If someone doesn't already 
understand something this basic, he/she shouldn't be in programming.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#11573

FromFrank Thomason <Frank@example.net>
Date2013-10-24 10:05 -0400
Message-ID<5269293B.40307@example.net>
In reply to#11572
On 10/24/2013 9:01 AM, Jerry Stuckle wrote:
> On 10/24/2013 8:35 AM, Frank Thomason wrote:
>> Arno - thank you for taking the time and effort to provide such a useful
>> and respectful response to the question. If more posters spent their
>> time answering questions in this manner, instead of calling each other
>> trolls, this NG would be a much better place.
>>
>
> Frank,
>
> Once you're read this newsgroup for a while, you'll figure out that
> richard is unable to understand anything beyond copy and paste.  Arno's
> explanation is well beyond richard's level of comprehension.  That's why
> the regulars here respond like they do - they understand posts like
> Arno's are not worth the time invested in writing it.
>
> And please note - there is nothing in Arno's update which even the most
> basic programmer should understand.  If someone doesn't already
> understand something this basic, he/she shouldn't be in programming.
>
Jerry,

I'm a long time reader of this newsgroup, so I'm well aware of richard's 
reputation and the probable futility of providing him with a proper 
answer.  I was just observing that it was refreshing to see someone take 
the time to explain the thought process involved in solving a problem of 
this type, rather than just replying "that's a Programming 101 exercise 
that any idiot should be able to do".  Will it help richard - who knows? 
  Will it help some other newbie? Maybe.

[toc] | [prev] | [next] | [standalone]


#11651

FromBiju S <biju@pixnnova.com>
Date2013-10-29 22:38 -0700
Message-ID<7da54748-4892-4494-a0ce-3ac59335de39@googlegroups.com>
In reply to#11548
On Wednesday, October 23, 2013 3:49:27 AM UTC+5:30, 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?
> 
> 
> 
> http://mroldies.net/200/jukebox.php
> 
> 
> 
> 
> 
> <?php
> 
> 
> 
> include "arrayxx.php";
> 
> 
> 
> $min=1;
> 
> $max=40;
> 
> 
> 
> $bnumber=1;
> 
> 
> 
> while ($bnumber<=5) {
> 
> 
> 
> echo "<td>";
> 
> 
> 
> echo $min;
> 
> echo $max;
> 
> 
> 
> while ($min<=$max){
> 
> 
> 
> echo $playme[$min][2]."<br>";
> 
> 
> 
> $min++;
> 
> 
> 
> }
> 
> 
> 
> echo "</td>";
> 
> 
> 
> $min=$min+40;
> 
> $max=$max+40;
> 
> $bnumber++;
> 
> 
> 
> }
> 
> 
> 
> ?>



TRY this 


<table border="1" >
<tr><td>

<?php    for ($i=1; $i<=200; $i++)      {   
   
 
 if($i %40==0) {    echo $i . "<br/>";  ?>
 
 </td>  <td>
 
<?php } else {       echo "" . $i . "<br/>";      }     }      ?>

</td></tr> </table>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web