Need help for alternating row classes

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
mastagino
Forum Newbie
Posts: 2
Joined: Wed Dec 31, 2008 3:41 am

Need help for alternating row classes

Post by mastagino »

Hey guys,

I am still a noob using PHP, but I am trying to teach myself as much as I can before asking for help.
Well, I am stuck....
I have tried almost every method out there, and still cant get it to work in my table...

I have been trying to alternate row classes in order to display alternating backgrounds in my PHP table.
Here is the code:

Code: Select all

<table class="stats">
    <thead>
        <tr>
                <th class="header text_left">Nom</th>
            <th class="header text_right width">#</th>
            <th class="header text_right width">J</th>
            <th class="header text_right width"><a href='statistiques.php?buts=1'>B</a></th>
            <th class="header text_right width"><a href='statistiques.php?assista=1'>A</a></th>
            <th class="header text_right width">PTS</th>
            <th class="header text_right width"><a href='statistiques.php?penal=1'>Pen</a></th>
            <th class="header text_right width">Pts<sup>%</sup></th>
        </tr>
    </thead>
    <?php
    //boucle d'affichage SQL Stats
    if($_GET['buts']==1)
    {
    $reponse = mysql_query("SELECT * FROM joueur WHERE type='a' OR type='d' OR type='g' ORDER BY but DESC");
    }
    elseif(isset($_GET['assista'])==1)
    {
    $reponse = mysql_query("SELECT * FROM joueur WHERE type='a' OR type='d' OR type='g' ORDER BY assist DESC");
    }
    elseif(isset($_GET['penal'])==1)
    {
    $reponse = mysql_query("SELECT * FROM joueur WHERE type='a' OR type='d' OR type='g' ORDER BY penal DESC");
    }
    else
    {
    $reponse = mysql_query("SELECT * FROM joueur WHERE type='a' OR type='d' OR type='g' ORDER BY nom DESC");
    }
    while ($donnees = mysql_fetch_array($reponse) )
    {
    ?>
    <tr>
        <td class="text_left bold">
             <a href="player.php?id=<?php echo $donnees['id'];?>">
             <?php echo $donnees['nom'];?>, <?php echo $donnees['prenom'];?> 
             </a>
        </td>
        <td class="text_right">
            <?php echo $donnees['numero'];?> #
        </td>
        <td class="text_right green">
            <?php echo $donnees['matches'];?>
        </td>
        <td class="text_right yellow">
            <?php echo $donnees['but'];?>
        </td>
        <td class="text_right blue">
            <?php echo $donnees['assist'];?>
        </td>
        <td class="text_right">
            <?php 
            $points=$donnees['assist']+$donnees['but'];
            echo $points; ?>
        </td>
        <td class="text_right">
            <?php echo $donnees['penal'];?>
        </td>
        <td class='td_joueur'>
            <?php
                if($points!='0')
                {
                $moyenne=$points/$donnees['matches'];
                echo $moyenne;
                }
                else
                {
                echo '0';
                }
            ?>
        </td>
    </tr>
                                    
    <?php
        } //fin de la boucle statistiques
    ?>
</table>
I would like to add the aternating TR classes "odd" and "even".

Thanks guys!!

Gino
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help for alternating row classes

Post by requinix »

Before the loop pick a style to start with. After printing the row, switch styles.
Sounds pretty simple, right?

Code: Select all

$tr = "odd";
while ($donnees = mysql_fetch_array($reponse) )
{
?>
<tr class="<?php echo $tr; ?>">
...
</tr>
 
<?php
    $tr = ($tr == "odd" ? "even" : "odd");
    } //fin de la boucle statistiques
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Need help for alternating row classes

Post by onion2k »

I prefer to use a modulus with a counter..

Code: Select all

$counter = 0;
echo "<tr class=\"".(($counter++%2==0)?"odd":"even")."\">";
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Need help for alternating row classes

Post by VladSun »

Code: Select all

$counter = 0;
//$div = 2;
$div = 3;
echo "<tr class=\"row_".($counter++%$div)."\">";
 
:)
There are 10 types of people in this world, those who understand binary and those who don't
mastagino
Forum Newbie
Posts: 2
Joined: Wed Dec 31, 2008 3:41 am

Re: Need help for alternating row classes

Post by mastagino »

OMG thank you Tasairis!
Your a genius..... My table works perfectly now :D
Been trying to integrate several techniques over the web into my table, but it wasnt working, since my table is pretty complex CSS wise.

Again thank you so much, and everyone else as well for posting :D

Next time I wont let my pride stop me from asking pros
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Need help for alternating row classes

Post by onion2k »

VladSun wrote:

Code: Select all

$counter = 0;
//$div = 2;
$div = 3;
echo "<tr class=\"row_".($counter++%$div)."\">";
 
:)
That's really nice. I'd never use it, but I appreciate the beauty of it as a solution. Good work mate.
Post Reply