0

I'm trying to create two unordered lists from a PHP array, I found this thread which is pretty much what I'm looking for, but I would like the first list to have 11 items, the second list to have the rest. Here's my code:

<?php if ($rows) : 

    $items = count($rows);
    $split = ceil($items/2);
    $firsthalf = array_slice($rows,$split);
    $secondhalf = array_slice($rows,0,$split);
?>

    <div class="tickets">

      <div class="col1">
        <ul>
          <?php foreach ($firsthalf as $item) : ?>
          <li><a href="">test 1</a></li>
          <?php endforeach; ?>
        </ul>
      </div>

      <div class="col2">
        <ul>
          <?php foreach ($secondhalf as $item) : ?>
          <li><a href="">test 2</a></li>
          <?php endforeach; ?>
        </ul>
      </div>

      <div class="clear"></div>
    </div>

<?php endif; ?>
3
  • and what is the question? Commented Jan 20, 2013 at 23:27
  • Just shuffle the array before you do your array_sliceing. Commented Jan 20, 2013 at 23:28
  • how to make the first list have 11 items Commented Jan 20, 2013 at 23:28

3 Answers 3

2

Here is how to split the array into 11 items and then the rest using array_slice():

$firsthalf = array_slice($rows, 0, 11);
$secondhalf = array_slice($rows, 11);
Sign up to request clarification or add additional context in comments.

Comments

1

If you have a look at the array_slice documentation, you can see that you specify the size of the split as the third parameter, while the second is the offset:

<?php 
    if ($rows) : 
      $firsthalf = array_slice($rows, 0, 11); // returns 11 rows from the start
      $secondhalf = array_slice($rows, 11); // returns everything after the 11th row
?>

1 Comment

Thanks so much (same answer as below) - but thanks for the documentation to go with :)
1
// $items = count($rows);
// $split = ceil($items/2);
$firsthalf = array_slice($rows, 0, 11);
$secondhalf = array_slice($rows, 11);

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.