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
Switch to threaded view of this topic Create a new topic Submit Reply
splitting list into columns [message #183405] Tue, 22 October 2013 22:19 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
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++;

}

?>
Re: splitting list into columns [message #183406 is a reply to message #183405] Tue, 22 October 2013 23:31 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
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
Re: splitting list into columns [message #183407 is a reply to message #183406] Wed, 23 October 2013 00:53 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
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.
Re: splitting list into columns [message #183408 is a reply to message #183407] Wed, 23 October 2013 01:40 Go to previous messageGo to next message
Evan Platt is currently offline  Evan Platt
Messages: 124
Registered: November 2010
Karma: 0
Senior Member
On Tue, 22 Oct 2013 20:53:40 -0400, richard <noreply(at)example(dot)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.
Re: splitting list into columns [message #183420 is a reply to message #183405] Wed, 23 October 2013 21:24 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
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
Re: splitting list into columns [message #183432 is a reply to message #183420] Thu, 24 October 2013 12:35 Go to previous messageGo to next message
Frank Thomason is currently offline  Frank Thomason
Messages: 2
Registered: October 2013
Karma: 0
Junior Member
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.
Re: splitting list into columns [message #183433 is a reply to message #183432] Thu, 24 October 2013 13:01 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: splitting list into columns [message #183434 is a reply to message #183433] Thu, 24 October 2013 14:05 Go to previous messageGo to next message
Frank Thomason is currently offline  Frank Thomason
Messages: 2
Registered: October 2013
Karma: 0
Junior Member
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.
Re: splitting list into columns [message #183455 is a reply to message #183406] Sat, 26 October 2013 01:04 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
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)
Re: splitting list into columns [message #183462 is a reply to message #183455] Sun, 27 October 2013 16:42 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
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
Re: splitting list into columns [message #183468 is a reply to message #183462] Mon, 28 October 2013 00:02 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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(at)news(dot)demon(dot)co(dot)uk> (2004)
Re: splitting list into columns [message #183525 is a reply to message #183468] Wed, 30 October 2013 00:34 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
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
Re: splitting list into columns [message #183536 is a reply to message #183405] Wed, 30 October 2013 05:38 Go to previous message
Biju S is currently offline  Biju S
Messages: 3
Registered: December 2012
Karma: 0
Junior Member
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>
  Switch to threaded view of this topic Create a new topic Submit Reply
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 06:27:09 GMT 2024

Total time taken to generate the page: 0.03999 seconds