TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-17-2008, 06:54 AM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application 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 !
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 11-17-2008, 11:36 AM   #2 (permalink)
The Wanderer
 
Join Date: Nov 2008
Location: Plymouth, UK
Posts: 9
Thanks: 0
Measter is on a distinguished road
Default

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.
Measter is offline  
Reply With Quote
Old 11-17-2008, 01:29 PM   #3 (permalink)
The Visitor
 
Join Date: Aug 2008
Posts: 4
Thanks: 0
enzochi@gmail.com is on a distinguished road
Default

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.
enzochi@gmail.com is offline  
Reply With Quote
Old 11-17-2008, 04:03 PM   #4 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

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;         } 
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 11-23-2008, 10:59 PM   #5 (permalink)
The Wanderer
 
Join Date: Dec 2007
Location: united states
Posts: 16
Thanks: 7
Matt is an unknown quantity at this point
Default

i found an easy way to do this.
Code:
$cell_color = ($i % 2 == 0 ? "class1" : "class2"); $i++;
Send a message via AIM to Matt Send a message via MSN to Matt Send a message via Yahoo to Matt
Matt is offline  
Reply With Quote
Old 11-24-2008, 12:22 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

That's definitely the best way, Matt, and the way I always use to do this.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-24-2008, 01:04 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Attached Files
File Type: rar Rows.rar (689 Bytes, 20 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 11-24-2008 at 01:58 AM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
codefreek (11-24-2008), Matt (11-24-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:10 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design