2

When looping through an array how can I create a different css div style for the last element to be output in my array.

for($i=0;$i<=count($productid);$i++){if($productrank[$i]>0 ){

<? if (($productid[$i] % 2 ) && !( last element of array){ echo '<div class="centerBoxContentsFeatured centeredContent back  vLine " style="width:50%;">';}
else { echo '<div class="centerBoxContentsFeatured centeredContent back " style="width:50%;">';} ?>
3
  • 2
    fyi, you should never use <? but always <?php (unless you use the <?= some_expression ?> syntax which is always supported) Commented Aug 26, 2013 at 13:21
  • you get the last element with $productid[count($productid)-1]. your if construct is doing something different though. Commented Aug 26, 2013 at 13:24
  • 2
    If your last div is the last child of its parent, you could simply use the :last-child selector in your CSS file. Commented Aug 26, 2013 at 13:27

3 Answers 3

2

Just check if it is the last $productid

for(...)
{
    if ($i === (count ($productid) - 1))
        // Last one -> special CSS
    }
}

Also, DO NOT use count() in a FOR loop if you don't really have to. Just assign a value BEFORE and use it :

$count_temp  = count ($productid);
for ($i = 0; $i < $count_temp; ++$i)

And use this $count_temp again if the IF statement to check if it's the last element


Answer to comment :

How would this same method get the first element?

if ($i === 0)

Or

// Special CSS for $i = 0
// Start loop at 1 instead of 0
for ($i = 1; $i < $count_temp; ++$i)
Sign up to request clarification or add additional context in comments.

6 Comments

That worked for me, one question though... why is it not good to use the count() in the FOR loop?
Because it will REcount everytime you loop, so if your array has a length = 100 000, you will run count($array) 100 000 times, instead of 1 time by doing it outside of the loop.
How would this same method get the first element?
I edited my answer for the first element, check it out and tell me if it's what you're looking for.
Wow, if you've a new decent question, create a new post and delete these comments. No one can read this in four comments.
|
1

You can do this purely with CSS using :last-child. It is supported by all modern browsers.

div.centerBoxContentsFeatured:last-child {
    /* special styles for last */
}

See it in action: http://jsfiddle.net/9y93j/1/

1 Comment

I tried this in addition to what I had, it worked but not exactly what I need. I can use it in other projects those thanks for the feedback.
0

Like this:

if($productid[$i] ==  $productid[count($productid)-1]){
    //last element code here
} elseif (($productid[$i] % 2 ){
   //even row code here
} else {
   //odd row code here
}

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.