3

How would I achieve this PHP task?

I have an unordered html list and and array. My code adds list tags to each item in the array to create one big unordered list

            <ul>
                 <?php foreach ($rows as $id => $row): ?>
                        <li><?php print $row ?></li>
                <?php endforeach; ?>
            </ul>

current output below

            <ul>
                <li>01</li>
                <li>02</li>
                <li>.....</li>
                <li>.....</li>
                <li>15</li>
            </ul>

What I want is to split the list items so that they are in groups of 4 as sub unordered lists. If the number is not divisible by 4 then the remainder should be a smaller list at the end, for example.

            <ul>
                <ul>
                    <li>01</li>
                    <li>02</li>
                    <li>.....</li>
                    <li>.....</li>
                </ul>
                <ul>
                    <li>05</li>
                    <li>06</li>
                    <li>.....</li>
                    <li>.....</li>
                </ul>
                <ul>
                    <li>09</li>
                    <li>10</li>
                </ul>
            </ul>

Thanks in advance.

1
  • have you loop maintain a counter, when the counter reach to a value which provide a mod 0 you draw a </ul><ul> and also a starting <ul> and a ending </ul> at the beginning and end of the loop Commented Aug 15, 2013 at 12:22

5 Answers 5

5
<ul>
<?php foreach ($rows as $id => $row): ?>
    <?php if ($id > 0 && $id % 4 === 0): ?>
        </ul><ul>
    <?php endif ;?>
    <li><?php echo $row; ?></li>
<?php endforeach; ?>
</ul>

(Note that if the key of your $rows array is not simply an index number, you'll need to maintain your own counter variable.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. See my final code below. I have included a statement to close the tags at the end of the list, regardless if it is divisible by four or not.
0
<?php
$id=0;
foreach ($rows as $id => $row)
{
    $id+=1;
    if ($id % 4 == 0)
    {
         echo "<ul>";
    }

    echo "<li>$i</li>";

    if ($id % 4 == 0)
    {
         echo "</ul>";
    }
}

if ($id % 4 != 0)
{
    echo "</ul>";
}    
?>

2 Comments

Thanks a lot for the answers. Much appreciated. I went with this solution in the end as the array may change to an associative array later.
@n00b13: note that this solution (also) will not work if the array is associative: the foreach will overwrite $id.
0
foreach ($rows as $id => $row) {
  if ($id % 4 == 0) {
    // do something
  }
}

Comments

0

Try something like this :

   <?php
                 $rows = array("2","3","5","6","8","9","0","3","5");
                 $i =0;
                  foreach ($rows as $id => $row): 
                  if($i % 4 == 0)
                  { echo "</ul><ul>";}
                  $i++;
                  ?>
                        <li><?php print $row ?></li>
                <?php endforeach; ?>
            </ul>

Comments

0

In the end this is the solution I went for. This solution includes a statement to close the tags if there are a number of items not divisible by four, for example if there are five items in total. Thanks to everyone, I couldn't have done it without your input.

            $count;
            foreach ($rows as $id => $row)
                {
                    if ($count % 4 == 0)
                    {           
                        echo "<ul>";
                    }

                    echo '<li>' . $row . '</li>';

                    if ($count % 4 == 3 || $count == count($rows)-1)
                    {
                        echo "</ul>";
                    }
                    $count++;
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.