0

I am creating some jQuery functionality on my website whereas an array of Facebook posts and Tweets is looped through to show on the front page.

I have 5 boxes on my front page which I need 5 to show random elements from this array at the same time, then I using some jQuery cycle functionality to cycle through this array. The only issue is, as I am looping over this array 5 times (with each box) and there are only 20 items in this array it can show repeated data.

To help explain this more here is my code:

<?php
  $social_feeds = array_merge($tweets_feed, $facebook_feeds);
  $social_feeds_arranged = $tweets_feed_instance->sortArrayItemsByDate($social_feeds);
?>
<div id="social_box_item1" class="social_box_item">
  <?php foreach($social_feeds_arranged as $item) {  ?>
    <a class="item">
      <?php echo $item['text'] ?>
    </a>
  <?php } ?>
</div>
<div id="social_box_item2" class="social_box_item">
  <?php foreach($social_feeds_arranged as $item) {  ?>
    <a class="item">
      <?php echo $item['text'] ?>
    </a>
  <?php } ?>
</div>
<div id="social_box_item3" class="social_box_item">
  <?php foreach($social_feeds_arranged as $item) {  ?>
    <a class="item">
      <?php echo $item['text'] ?>
    </a>
  <?php } ?>
</div>
<div id="social_box_item4" class="social_box_item">
  <?php foreach($social_feeds_arranged as $item) {  ?>
    <a class="item">
      <?php echo $item['text'] ?>
    </a>
  <?php } ?>
</div>
<div id="social_box_item5" class="social_box_item">
  <?php foreach($social_feeds_arranged as $item) {  ?>
    <a class="item">
      <?php echo $item['text'] ?>
    </a>
  <?php } ?>
</div>

<script type="text/javascript">
  jQuery(document).ready(function() {

    jQuery('#social_box_item1').cycle({

    }); 
    jQuery('#social_box_item2').cycle({

    });
    jQuery('#social_box_item3').cycle({

    }); 
    jQuery('#social_box_item4').cycle({

    }); 
    jQuery('#social_box_item5').cycle({

    }); 

  });
</script>

What I need to happen I think is for the first loop to push the item it is displaying ot the back of the array, so when the second loop accesses the first item it will not show the same array item, the same with the 3rd, 4th and 5th loops. I have attempted to do this using array_push and array_shift but can't seem to get it working.

Does anybody have any ideas or can help point out where I am going wrong?

1 Answer 1

1

Those loops are not needed then. You can access those array items one by one, if you meant that. Try this:

        $social_feeds = array_merge($tweets_feed, $facebook_feeds);
        $social_feeds_arranged = $tweets_feed_instance->sortArrayItemsByDate($social_feeds);

    ?>
    <div id="social_box_item1" class="social_box_item">
        <a class="item">
                <?php echo $social_feeds_arranged[0]['text']; ?>
            </a>
        </div>
    <div id="social_box_item2" class="social_box_item">

            <a class="item">
                <?php echo $social_feeds_arranged[1]['text']; ?>
            </a>                                

    </div>
    <div id="social_box_item3" class="social_box_item">

            <a class="item">
                <?php echo $social_feeds_arranged[2]['text']; ?>
            </a>                                

    </div>
    <div id="social_box_item4" class="social_box_item">

            <a class="item">
                <?php echo $social_feeds_arranged[3]['text']; ?>
            </a>                                

    </div>
    <div id="social_box_item5" class="social_box_item">

            <a class="item">
                <?php echo $social_feeds_arranged[4]['text']; ?>
            </a>                                

    </div>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi. The loops are there as I still need to populate each box with items for the jQuery cycle to loop through. I just need to ensure that the same array item is now shown at the same time. Sorry if I was unclear at first!
But each box gets populated with the relevant item in the answer above :) those loops were not serving any purpose in this case if only 1 item needs to be displayed in one social_box_item div
Hi. Thanks for your help. I managed to sort the issue using array_slice but your answers definitely helped!

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.