SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?php foreach ($hotels->hotel as $hotel): ?>
                    <?php if($i == 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
    The more you learn.... the more you learn there is more to learn.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,036
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Assuming $hotels->hotel is a regular array with numeric keys:

    PHP Code:
    <?php foreach ($hotels->hotel as $i => $hotel): ?> 
        <div<? echo ($i != && $i == 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 ?>
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Scallio
    That doesn't seem to do anything, no class is added.

    The HTML output should look like this:
    Code HTML4Strict:
    <div></div>
    <div></div>
    <div class="last"></div>
    <div></div>
    <div></div>
    <div class="last"></div>

    Above my code I have:
    PHP Code:
    <?  include 'hotels.php';
                    
    $hotels = new SimpleXMLElement($xmlstr);
                
    ?>
    hotels.php looks like:
    PHP Code:
    <?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
    The more you learn.... the more you learn there is more to learn.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Why dont you start var_dump() ing some of the vars you are testing and see what they actually contain?
    PHP Code:
    var_dump($i); 

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    The more you learn.... the more you learn there is more to learn.

  6. #6
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,791
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    try this:


    Code:
    <?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

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool thanks.
    I just need to understand this now

    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 Code:
    <?php $clss= ($i == 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
    The more you learn.... the more you learn there is more to learn.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Change $i = 0 to $i = 1 and set it back to $i % 3 instead of $i % 2
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you
    The more you learn.... the more you learn there is more to learn.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •