TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Alternate row color ? (http://www.talkphp.com/advanced-php-programming/3621-alternate-row-color.html)

Peuplarchie 11-17-2008 06:54 AM

Alternate row color ?
 
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 :

PHP Code:

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 !

Measter 11-17-2008 11:36 AM

You would flip a variable every time the loop is run. For example:

PHP Code:

$flipper false;

foreach(
$array as $something)
  {
    
$flipper = !$flipper;
    
    
$echo '<div class="someclass' . (int)$flipper '">some text</div>';
  } 

Basicly, if $fipper is false, you'll end up with the class being 'someclass0', and if $flipper is true, you'll end up with 'someclass1'. The joys of typecasting.;-)

enzochi@gmail.com 11-17-2008 01:29 PM

could just do a mod oppr:
PHP Code:

for($i=0;mysql_num_rows($rsSomthing)>$i;$i++){
   
$color "#ededed";
   if(
$i%2){
      
$color "#ccc";
   }
   
$i++;
   echo 
"<div style='background:".$color."'>something here</div>\n";


should do the trick.

Peuplarchie 11-17-2008 04:03 PM

There we are !
PHP Code:

 function drawList($list) {            $thelist '';                foreach($list as $file=>$string)       {          $lines nl2br($string);               $thelist .= '';               $thelist .= '';               $thelist .= ''.$file.'';               $thelist .= '';                             $thelist .= '';  $lines=explode("\n",$lines); $count=0$color[0] = "#cccccc"$color[1] = "#ffffff"; foreach($lines as $key=>$line) {  $lines[$key]="{$line}"$count=(++$count 1); } $lines=implode('
'
,$lines);               $thelist .= $lines.'
'
;               $thelist .= '';               $thelist .= '';               $thelist .= '
'
;     }     return $thelist;         } 


Matt 11-23-2008 10:59 PM

i found an easy way to do this.
Code:

$cell_color = ($i % 2 == 0 ? "class1" : "class2"); $i++;

Wildhoney 11-24-2008 12:22 AM

That's definitely the best way, Matt, and the way I always use to do this.

Wildhoney 11-24-2008 01:04 AM

1 Attachment(s)
I had a stab at a different approach for doing this without the ternary operator. See what you think to this, as it has support for many colours also.

Code:

<table>
<?php declare (ticks = 1): ?>
        <?php foreach ($aData as $iNumber): ?>
       
                <tr style="background-color: <?php echo $pRowColours->get(); ?>">
                        <td>Row <?php echo $iNumber; ?></td>
                </tr>
               
        <?php endforeach; ?>
<?php enddeclare; ?>
</table>

Edit: Just a side note though, you don't have to use ticks for this. It could work without it.


All times are GMT. The time now is 05:06 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0