views:

59

answers:

4

Hi

I have a array like ('mary', 'johnny', 'butch', 'pony', 'katy', 'chuck', 'norris') The number of elements can vary...

How can I build a nice list from all these elements, like this:

<ul>
  <li>
    Mary
    Johnny
    Butch
  </li>

  <li>
    Pony
    Katy
  </li>

  <li>
    Chuck
    Norris
  </li>

?

Basically build the list like a spiral:

> -------------
               |
<--------------
|
>--------------
               |
<--------------

The number of list items is fixed, for eg. 3 in the example above

+1  A: 

What you should do is separate presentation from logic. Instead of figuring out how many items should go in each element, put each item in it's own element. Each one is a line-item after all.

<ul class="the_list">
    <li>Mary</li>
    <li>Johnny</li>
    <li>Butch</li>
    <li>Pony</li>
    <li>Katy</li>
    <li>Chuck</li>
    <li>Norris</li>
</ul>

Then use CSS to display the list however you want. For example, you could do something like this:

.the_list    { width:300px;}
.the_list li { display:block;float:left;width:100px;}

You'll need some additional rules like a reset stylesheet first, but that's the gist of it.

Stephen
thanks, but in my case it's not possible because I'm applying this to a graphic slider. Number of list items are fixed, and only 3 items are displayed at a time. the rest will slide...
Alex
What is this graphic slider you mention? Can it not be edited?
Stephen
actually there are 3 such sliders applied to each of the 3 `<li>`'s. This is why I need them separated. the slider is the "cycle" jquery plugin
Alex
+1  A: 

You could loop through the array and use modulus

for ($i = 0; $i < count($array); $i++)
{
    if ($i % 3 == 0)
    {
        //append to first li
    }
    else if ($i % 3 == 1)
    {
        //append to second li
    }
    else
    {
        //append to third li
    }
}
fehays
+1  A: 

You want to group them in 3's? Something like:

<?php
$list = ('mary', 'johnny', 'butch', 'pony', 'katy', 'chuck', 'norris');
$c = 0;
$LIMIT = 3;
echo "<ul>";
foreach($list as $current) {
    echo "<li>$current</li>";
    $c += 1;
    if($c == $LIMIT) { echo "</ul><ul>"; }
}
echo "</ul>";

Very ugly, but gets the job done

axon
I think Alex is looking for a script that will shift as more are added. So if there's 12, he wants groups of 4, 9 would be groups of three, etc. The hitch comes when you have non-divisible numbers, which would probably require everybody's favorite, the ceil command
bpeterson76
exactly :)the example above shows one group of 3 and two groups of 2.
Alex
+2  A: 

Yet Another Path, Because There Was Not Weird Enough Code Posted.

//set the amount of items
$li_items = 3;//can be anything, really

echo '<ul>';
//loop
for(;$li_items > 0;$li_items--){
   echo "\n\t<li>\n\t".implode("\n\t",
       array_splice($array,0,ceil(count($array)/$li_items))
      )."\n\t<li>";
}
echo '</ul>';
Wrikken
lol, thanks. but I'll go with the partition() function I found :)
Alex
That function _does_ have the advantage of being legible indeed ;)
Wrikken