Page 1 of 1

- PHP 4 - Alternate row color ?

Posted: Mon Nov 17, 2008 12:54 am
by Peuplarchie
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 :

Code: Select all

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;
        
}

How can I make $lines have alternate row color ?

Thanks !

Re: - PHP 4 - Alternate row color ?

Posted: Mon Nov 17, 2008 4:05 am
by aceconcepts

Code: Select all

 
function drawList($list)
{
      $color1 = "#F0F0F0"; 
      $color2 = "#FFFFFF"; 
      $row_count = 0;
 
      $thelist = '';        
      foreach($list as $file=>$string)
      {
              $row_color = ($row_count % 2) ? $color1 : $color2;
 
        $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/>';
 
              $row_count++;
    }
    return $thelist;
       
}
 
Place $row_color wherever you want.

Re: - PHP 4 - Alternate row color ?

Posted: Mon Nov 17, 2008 10:03 am
by Peuplarchie
There we are !

Code: Select all

 
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;
       
}