1

I have this PHP loop,

foreach($returnedContent as $k => $v) {
    $imageName = str_replace($replaceData, "", $v['contentImageName']);
    echo "<a class='contentLink' href='".base_url()."welcome/getFullContent/$v[contentId]'>";
    echo "<img src='/media/uploads/".strtolower($v['categoryTitle'])."/".$imageName."_thumb.png' alt='$v[contentTitle]' />";
    echo "</a>";
}

Once the lopp has finished I was hoping it would be possible to do loop to print x amount of grey boxes is this possible and if so how, basically if the first loop returns 1 item i need the second loop to print out 11 boxes, if the first one returns 9 items I need the second loop to return 3 boxes.

Make sense? Can anyone help me?

6 Answers 6

2

So if you want a total of 12 boxes, set a counter and decrement:

$boxes = 12;
foreach($returnedContent as $k =>$v){
   // all your previous stuff
   $boxes--;
}

for($i = 0; $i < $boxes; $i++){
   // print your box here
}

Depending on your application you may also want to check that the number of items in $returnContent is <= $boxes. If it is greater than $boxes you won't get an error but you will get rows with more than $boxes images.

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

Comments

0

Just keep a counter and increment it for each loop iteration, then add

for (;$counter < 11; ++$counter) {
    do_loop_stuff();
}

2 Comments

I would use count($returnedContent) instead of variable. Also, is it really possible to start a for loop with a ;? I've never seen this.
@Robin, I bet you haven't seen for(;;), which is also quite legal :)
0

Maybe you could do something like this (assuming $returnedContent is numerically indexed):

//count to 12 so we get 12 items
for ($i=0; $i<12; $i++) {
    //check if there is an entry to print
    if (isset($returnedContent[$i])) {
        $v = $returnedContent[$i];
        $imageName = str_replace($replaceData, "", $v['contentImageName']);
        echo "<a class='contentLink' href='".base_url()."welcome/getFullContent/$v[contentId]'>";
        echo "<img src='/media/uploads/".strtolower($v['categoryTitle'])."/".$imageName."_thumb.png' alt='$v[contentTitle]' />";
        echo "</a>";
    } else {
        //draw grey box
    }
}

Comments

0

After the first loop, you can do:

for($i = 0; $i < 12 - count($returnedContent); $i++)
{
 // print the grey boxes.
}

Comments

0

Hmmm Im not sure Im understanding you but

$c = count($returnedContent);

will get you the amount of items in the variable

then:

$c = (11-$c);
if($c > 0) {
    for($i=0;$i<$c;$i++) {
      // print gray box
    }
}

after the first loop. You could also use a counter variable inside the first loop.

Comments

0

I did interpret the question as "Do something when the loop has finished iterating". In which case a for/foreach loop isn't the best choice here. how about

<?php
$i = 0;
do {
    echo $i;
} while ($i > 0);
//then do whatever else you need to.
?>

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.