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 !
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
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.
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: