Page 1 of 1

Need help for alternating row classes

Posted: Wed Dec 31, 2008 3:49 am
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

Re: Need help for alternating row classes

Posted: Wed Dec 31, 2008 4:07 am
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
?>

Re: Need help for alternating row classes

Posted: Wed Dec 31, 2008 4:09 am
by onion2k
I prefer to use a modulus with a counter..

Code: Select all

$counter = 0;
echo "<tr class=\"".(($counter++%2==0)?"odd":"even")."\">";

Re: Need help for alternating row classes

Posted: Wed Dec 31, 2008 4:14 am
by VladSun

Code: Select all

$counter = 0;
//$div = 2;
$div = 3;
echo "<tr class=\"row_".($counter++%$div)."\">";
 
:)

Re: Need help for alternating row classes

Posted: Wed Dec 31, 2008 4:26 am
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

Re: Need help for alternating row classes

Posted: Wed Dec 31, 2008 5:45 am
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.