Modulus help please??

Discussion in 'PHP' started by 123GoToAndPlay, Mar 9, 2010.

  1. #1
    Hi,

    I have a loop which creates 4 div's in a rows. Now i like to add a different class to the first and fourth div in a row

    
    <div class="grid_4 alpha center">
    
    <div class="grid_4 center">
    
    <div class="grid_4 center">
    
    <div class="grid_4 omega center">
    
    PHP:
    Should i use modulus, if so how ??
     
    123GoToAndPlay, Mar 9, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    if your divs are static to 4 counts you can just use the condition

    for($i=1,$n=4; $i<=$n; $i++)
    {
    $addedString = $i=1 || $i=$n ? 'add your string here' : '';
    echo '<div class="grid_4 alpha center" '. $addedString .'>';
    }
     
    bartolay13, Mar 9, 2010 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nice suggestion, going to try it out. but i need two different classes

    i have found this to work also
    ok found it
    
    $extraclass = $i % 4;
    		if($extraclass==0)		
    			$extraclass = 'alpha';
    		elseif($extraclass==3)		
    			$extraclass = 'omega';
    		else 
    			$extraclass = '';
    		$output .= "<div class='grid_4 $extraclass center'>
    
    Code (markup):
     
    123GoToAndPlay, Mar 9, 2010 IP