Jump to content

Replacing items in a foreach (?)

Marty Walker

Recommended Posts


I'm not even sure how to ask this question technically so here goes. I'm working on a home page that'll have a grid of images like this:


But every so often I need to remove two of those images and replace it with an image twice the width and in a certain place, like this:


Any pointers? Many thanks. - Marty

Link to comment
Share on other sites

Generally speaking you need some variable to determine which image fills one grid and which one two. 

foreach($page->images as $image){
    // some markup for the dual grid view / maybe only a class
    // default case

If you really need to include things from another place have a look at the WireArray functions in the cheatsheet. Then the foreach/if statement would most likely be little bit different.

Link to comment
Share on other sites

Hi Marty,
assuming you want to build a grid upon an unknown collection of images and you want to have full control, you may start with this construct and change it to suite your needs:

    echo "<br /><hr /><pre style='font-family:monospace;'>"; // only a little test unit 
    $yourImagesArray = array(   1,1,1,1,1,1,

    // build the grid
    $images = $yourImagesArray; // containing lots of images, most with no tag or a "single" tag and some with a "twice" tag! You have to make sure it contains enough images for your grid, so.
    $i = 0;
    for ($row=1; $row<=7; $row++) {
                                                                  echo "\n$row:"; // only for the test
        for ($col=1; $col<=6; $col++) {
            // get the next image from the collection
            $img = $images[$i++];          // you would use $images->eq(++$i) with real images here!
            if (6 == $col) {                // only special case is with the last $col
                while (2 == $img) {        // last $col cannot take a "twice"-image. For the example I simply drop all "twices" while searching for a single-one.
                    $img = $images[$i++];
            // now we have a valid $img for our grid, so create output
            if (2 == $img) {
                $col++; // adjust the $col counter!
                echo 'oo';
            } else {
                echo 'O';
    // ready with grid

    echo "</pre><br /><hr />";  // only for the little test unit

test output is:

Edited by horst
  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...