[56K WARN] Alternating Thread Colours

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
Crazygar
Forum Newbie
Posts: 9
Joined: Thu Dec 06, 2007 8:16 pm

[56K WARN] Alternating Thread Colours

Post by Crazygar »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok, I have the code;

Code: Select all

$bgcolor = "#E0E0E0"; // light gray  
    $row = mysql_stuff() 
      
    while($row = mysql_fetch_array($result)){  
        if ($bgcolor == "#E0E0E0"){  
            $bgcolor = "#FFFFFF";  
        }else{  
            $bgcolor = "#E0E0E0";  
        }  

    echo("<tr bgcolor=".$bgcolor."\"><td>\n");
Graciously supplied by someone here to alternate between the colour of my forum threads when viewing. I know that I have to alter something in "viewforum.php" where it begins to "echo" the table data for viewing. Here is my problem -- where? A little pointer would help greatly. Thanks.

Gary


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What is your question? Where what?
Crazygar
Forum Newbie
Posts: 9
Joined: Thu Dec 06, 2007 8:16 pm

Post by Crazygar »

What I would like to know is where to put this code in to have my threads alternate colours. I am under the assumption its either viewforum.php or viewtopic.php. I am at a loss where to insert the code to have this happen. I hope this clarifies things.

This is the code that is supposed to help my accomplish this task. This is one of the final "loose ends" I need to have this happen. Or does anyone know a hack or mod out there that can do this with more detailed instructions?

Like this example I found; (this is for phpBB2)

Image

Having it going between the two colours makes everything so much better and much easier to read. I would appriciate any assistance in this matter.

Gary A.MacDonald
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Generally speaking, you can use the modulus (%)

Code: Select all

$x = 2;
while (something()) {
  if (($x % 2) == 0) {
     # First background color
  } else {
     # Second background color
  }
  $x++;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There have been a bunch of threads about this sort of display formatting. Look around for "zebra".
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Not to be super picky or anything, but I really hate hard-coded row colours.

They're sort of a style thing (and should be), but CSS engines today don't properly support child selectors. Boo-urns.

You could supplement this deficiency as a behaviour in the meantime:

Code: Select all

$(function(){
	$('.sometable tr:odd').css('background','#d0d0d0');
});
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Kieran Huggins wrote:Not to be super picky or anything, but I really hate hard-coded row colours.

They're sort of a style thing (and should be), but CSS engines today don't properly support child selectors. Boo-urns.

You could supplement this deficiency as a behaviour in the meantime:

Code: Select all

$(function(){
	$('.sometable tr:odd').css('background','#d0d0d0');
});
Or you could specify a class instead of a color in the code. :P
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I feel ways about non-semantic markup. Probably too many ways.

"odd" and "even" classes don't have any relevance to the data and are purely hackish CSS hooks. Yuck :-(

I'll feel better after my morning coffee & enema.
Crazygar
Forum Newbie
Posts: 9
Joined: Thu Dec 06, 2007 8:16 pm

Post by Crazygar »

Ok. I like the CSS Route much better than hard coding it myself. I am at loss where to insert the ACTUAL code. If it were turned into a "CSS Behaviour", obviously, all I need to do is modify viewtopic.tpl than start fooling around with .php files. I'll do a search for "zebra" thanks for the quick tip.

Now that I am awake, alternating thread colours like this forum.

Gary
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

phpBB has odd/even functions built-in to their custom templating system, if that's what you're looking for. Take a look inside an existing phpBB template for an example.
Crazygar
Forum Newbie
Posts: 9
Joined: Thu Dec 06, 2007 8:16 pm

Post by Crazygar »

Kieran, I'm having a devil of a time with this. What template(s) would have this? A Google search returned the same results as to the other searches I've been doing on trying to locate this. All I want it do is alternate between;

Even Background-color #2d3446
Odd Background-color #3d475f

For my viewing the threads in my Forum. The function method sounds good. As it eliminates harding coding to a php file which other templates might share. With defining the function, obviously it is being placed within the "viewtopic.tpl" file, its just a matter of placement.

Code: Select all

$(function(){ 
   $('.sometable tr:odd').css('background','#3d475f'); 
});
Even a CSS routine would be easier.

Code: Select all

tr:nth-child(even) {background: #2d3446}
tr:nth-child(odd) {background: #3d475f}
Its just the matter of "where" to place it and how to code into the appropriate file to get the output I desire. I've tried the Zebra Search and its rather "Cryptic" at best. My biggest problem is logical placement of the snippets of code to do the intended function.

I'm a little wet behind the ears in all this so I might need a more "precise" location and files to edit to perform the necessary task. I'm hunting for a good couple of starter books to get my down the php/phpBB2 road so I can stop bothering everyone with these questions.

Gary
Post Reply