Add class to third element - IF Else %

Hi all

I’m running sum basic content from a XML file and have the below which outputs things ok.
My problem is when I try and use an if else statement to supply a div class=“last” to every third element, it adds it to all the div elements.

Wondering what I’m doing wrong?
Can anybody spot the problem with syntax below?

<?php foreach ($hotels->hotel as $hotel): ?>
                <?php if($i % 3 == 0) : ?>
                    <div class="last">
                <?php else: ?>
                    <div>
                <?php endif; ?>
                    <img src="images/<? echo $hotel->img->name ?>">
                    <h3><? echo $hotel->name ?></h3>
                    <p><? echo $hotel->description ?></p>
                    <p><? echo $hotel->address->postcode ?></p>
            </div>
            <? endforeach ?>

Thanks, Barry

Assuming $hotels->hotel is a regular array with numeric keys:


<?php foreach ($hotels->hotel as $i => $hotel): ?> 
    <div<? echo ($i != 0 && $i % 3 == 0) ? ' class="last"' : ''; ?>>
        <img src="images/<? echo $hotel->img->name ?>"> 
        <h3><? echo $hotel->name ?></h3> 
        <p><? echo $hotel->description ?></p> 
        <p><? echo $hotel->address->postcode ?></p> 
    </div> 
<? endforeach ?>

Thanks Scallio
That doesn’t seem to do anything, no class is added.

The HTML output should look like this:

<div></div>
<div></div>
<div class="last"></div>
<div></div>
<div></div>
<div class="last"></div>

Above my code I have:

<?  include 'hotels.php';
                $hotels = new SimpleXMLElement($xmlstr);
            ?>

hotels.php looks like:

<?php
$xmlstr = <<<XML
<hotels>
    <hotel>
        <img>
            <name>hotel_preview.jpg</name>
            <alt>hotel 1</alt>
        </img>
        <name>hotel 1</name>
and so on ...

What do you suggest cheers?

Barry

Why dont you start var_dump() ing some of the vars you are testing and see what they actually contain?


var_dump($i);

Thanks.

NULL (working from my code in the first post)

string(5) “hotel” (working from Scallio code )

?

I currently have 4 hotels in my XML file.

try this:


<?php $i=0; foreach ($hotels->hotel as $hotel): ?>
              <?php $clss= ($i % 3 == 0)? 'class="last"':'' ?>
              <div <?php echo $clss ?>
                    <img src="images/<? echo $hotel->img->name ?>">
                    <h3><? echo $hotel->name ?></h3>
                    <p><? echo $hotel->description ?></p>
                    <p><? echo $hotel->address->postcode ?></p>
            </div>
            <? $i++;?>
            <? endforeach ?> 


which should work even if the $hotels->hotel array is associative.
Also I have made class a variable eliminating the IF statement for output AND allowing you to use the same code to insert other attributes to the div or target different divs w/o much alteration.

hope that helps

Cool thanks.
I just need to understand this now :slight_smile:

Ok, was missing a closing > on the div, that’s fixed.
Works good now but, seems to be adding the class to the first and forth divs?

Something to do with

<?php $clss= ($i % 3 == 0)? 'class="last"':'' ?>

update
If I change the 3 to a 2 its works - the problem is that its also adding the class to the first div.
How do I stop this happening?

Barry

Change $i = 0 to $i = 1 and set it back to $i % 3 instead of $i % 2

Thank you :slight_smile: