Alternate row color ?

Discussion in 'Programming' started by Peuplarchie, Nov 16, 2008.

  1. #1
    Good day to you all,
    Here I come again with a piece of code which reach a txt file and return the text, it also convert the "\n" into "<br>".

    Here it is :

    function drawList($list)
    {     
          $thelist = '';         
          foreach($list as $file=>$string)
          {
    
            $lines = nl2br($string);
                  $thelist .= '<b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b>';
                  $thelist .= '<div class="headh">';
                  $thelist .= '<b>'.$file.'</b>';
                  $thelist .= '</div>';              
                  $thelist .= '<div class="contenth"><div class="text">';
                  $thelist .= $lines.'<br/>';
                  $thelist .= '</div>';
                  $thelist .= '</div>';
                  $thelist .= '<b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b><br/>';
        }
        return $thelist;
            
    }
    PHP:

    How can I make $lines have alternate row color ?

    Thanks !
     
    Peuplarchie, Nov 16, 2008 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    function drawList($list)
    {     
          $thelist = '';         
          $i = 0;
          foreach($list as $file=>$string)      
          {
          $i++;
          if($i%1==0){
         $lines = "<span style=color1>".nl2br($string)."</span>";
          }else{
         $lines = "<span style=color2>".nl2br($string)."</span>";
          }
                  $thelist .= '<b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b>';
                  $thelist .= '<div class="headh">';
                  $thelist .= '<b>'.$file.'</b>';
                  $thelist .= '</div>';             
                  $thelist .= '<div class="contenth"><div class="text">';
                  $thelist .= $lines.'<br/>';
                  $thelist .= '</div>';
                  $thelist .= '</div>';
                  $thelist .= '<b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b><br/>';
        }
        return $thelist;
           
    }
    PHP:
    try like that
     
    crivion, Nov 17, 2008 IP
  3. Peuplarchie

    Peuplarchie Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but my problem is that I want to have each line within $lines to to be alternative row color.

    Not each $line being alternative row color.
     
    Peuplarchie, Nov 17, 2008 IP
  4. Peuplarchie

    Peuplarchie Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There we are !

    
    function drawList($list)
    {     
          $thelist = '';         
          foreach($list as $file=>$string)
          {
    
            $lines = nl2br($string);
                  $thelist .= '<b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b>';
                  $thelist .= '<div class="headh">';
                  $thelist .= '<b>'.$file.'</b>';
                  $thelist .= '</div>';              
                  $thelist .= '<div class="contenth"><div class="text'.(($c++)%2?' colour_class':'').'">'; 
    $lines=explode("\n",$lines);
    $count=0;
    $color[0] = "#cccccc";
    $color[1] = "#ffffff";
    foreach($lines as $key=>$line)
    {  $lines[$key]="<div class=\"contentalt\" style=\"background-color:{$color[$count]}\">{$line}</div>"; $count=(++$count & 1); }
    $lines=implode('<br>',$lines);
                  $thelist .= $lines.'<br/>';
                  $thelist .= '</div>';
                  $thelist .= '</div>';
                  $thelist .= '<b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b><br/>';
        }
        return $thelist;
           
    }
    
    
    PHP:
     
    Peuplarchie, Nov 17, 2008 IP