tags:

views:

94

answers:

5

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?

A: 

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

for (;$counter < 11; ++$counter) {
    do_loop_stuff();
}
dbemerlin
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
@Robin, I bet you haven't seen `for(;;)`, which is also quite legal :)
vava
A: 

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
    }
}
Tom Haigh
A: 

After the first loop, you can do:

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

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.

Mark Moline
A: 

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.

leChuck