- PHP 4 - Alternate row color ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

- PHP 4 - Alternate row color ?

Post 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 !
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: - PHP 4 - Alternate row color ?

Post 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.
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Re: - PHP 4 - Alternate row color ?

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