Welcome Guest, Not a member yet? Register   Sign In
Getting last ID
#1

[eluser]JamieBarton[/eluser]
Hey guys,

I have a homepage, I'm showing 5 boxes of latest news. I have done the query and getting all recent and limiting it to 5.

I would like the 5th box to be slightly different to the other 4 boxes. What would be the best way to do this?

My code for the view is:
Code:
<h3 class="homehead">Latest News</h3>
    
    &lt;?php foreach($news->result() as $item): ?&gt;
        
         <div class="box-news">
             <a href="/news">
                 <img >picture?&gt;" alt="" width="176" height="130" />
             </a>
            
            <h4>
                <a >id?&gt;">&lt;?=$item->title?&gt;</a>
            </h4>
            
            <p>
                &lt;?=$item->description?&gt;
            </p>
                
        </div>
        
    &lt;?php endforeach; ?&gt;

For the 5th box, I'd like the following code added to the DIV ID.
Code:
style="margin-right:0;"



Regards,

Jamie
#2

[eluser]BrianDHall[/eluser]
I'd declare a variable outside the foreach and increment it as it went through the loop, checking to see if it is equal to 5 then output the changed div code.
#3

[eluser]JamieBarton[/eluser]
I don't follow..

It's not the ID I want to check, it is the last last LIMIT, out of 5 I want to be different.
#4

[eluser]BrianDHall[/eluser]
[quote author="Jamie B" date="1254857488"]I don't follow..

It's not the ID I want to check, it is the last last LIMIT, out of 5 I want to be different.[/quote]

Code:
&lt;?php

$i = 0;

foreach($news->result() as $item): ?&gt;
        
         &lt;? $i++; ?&gt;
&lt;? if ($i == 5)
{
echo '<div class="box-news" style="margin-right:0;">';
}
else
{
echo '<div class="box-news">';
}
?&gt;
             <a href="/news">
                 <img >picture?&gt;" alt="" width="176" height="130" />
             </a>
            
            <h4>
                <a >id?&gt;">&lt;?=$item->title?&gt;</a>
            </h4>
            
            <p>
                &lt;?=$item->description?&gt;
            </p>
                
        </div>
        
    &lt;?php endforeach; ?&gt;

Quod erat demonstrandum Smile

I think that was what you were wanting, the 5th box has a different style than the others.
#5

[eluser]JamieBarton[/eluser]
wow!

Thank you very much BrianDHall! You just literally saved me hours form trying solutions, I'd never have used $i++. I must read more into that Smile

Thanks again for your help!

Much appreciated
#6

[eluser]Phil Sturgeon[/eluser]
Apparently ++$i; is a TINY amount faster. Increase application runtime by 0.000000000000000001%? Yes please.
#7

[eluser]kurucu[/eluser]
I'm sure it's fixed now, but is there any clever CSS that could have detected the 5th box? Something like
Code:
.box-news + .box-news + .box-news + .box-news + .box-news {margin-right: 0}

Or even, you could simply add a box-$i class and set a style for that (not too nice).

Just other ideas.
#8

[eluser]mrwilson1[/eluser]
I am far from an expert on this, but couldn't you use the child pseudo element here? fifth-child?
#9

[eluser]n0xie[/eluser]
[quote author="mrwilson1" date="1254860390"]I am far from an expert on this, but couldn't you use the child pseudo element here? fifth-child?[/quote]
You can easily with javascript. (Jquery has this pretty much covered).
#10

[eluser]JamieBarton[/eluser]
All look good! I'll try the jQuery one out Smile




Theme © iAndrew 2016 - Forum software by © MyBB