#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    Change css for every 4th row


    Hi all, I want to display every 4th row grabbed from the database with different CSS style. The first three rows from the DB should take the CSS style (col-4) but the fourth row should take the CSS style (col-4 last): How can I do this with PHP. My code is showing below.

    PHP Code:
    <?php foreach($result as $value) : ?>
        <div class="col-4">
            <a href="view.php?m_id=<?php echo $value['m_id']; ?>">
                <img alt="" src="<?php echo $value['m_image']; ?>" width="131px" height="120px">
            </a>
            <h3><a href="view.php?m_id=<?php echo $value['m_id']; ?>"><?php echo $value['m_title']; ?></a></h3>
            <p><strong><?php echo $value['name']; ?></strong> </p>
            <p><?php echo date('M j, g:i A'strtotime($value['date'])); ?></p>
        </div>
    <?php endforeach; ?>
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    Reference:
    PHP Code:
    <?php
      $x
    =0;
      while( 
    $x 100 ){
        if (!(
    $x%4)){ print "$x = 4!<br>"; }
        
    $x++;
      }
    Track your iteration and do a modulus on it. Start with $x=1 if you don't want your first row "col-4 last".
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,689
    Rep Power
    6351
    Any question of "for every N iterations" is solved through modular division.

    I believe this scenario is in the new user guide, actually. Edit: It's #23
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo