FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Rotating words?





Diablosblizz
I don't really know how to explain this, other than this code:

Code:
      <tr class="sectiontableentry1">
         <td>
            <input type="checkbox" name="delete[204]" value="del">
         </td>
         <td align=center>
            <img src="http://habombers.com/components/com_jim/images/1.png" border="0">

         </td>
         <td>
            <a href="index.php?option=com_jim&task=view&id=204" />

RE:</a>
         </td>
         <td>
David         </td>
         <td  align=center nowrap>

2008-02-04 00:37:19         </td>
      </tr>

      <tr class="sectiontableentry2">


I have this on my website, but I was wondering how to alternate between then two TR classes, sectiontableentry2 and 1 and so it will look like this:

Code:
<tr class="sectiontableentry2">
<tr class="sectiontableentry1">
<tr class="sectiontableentry2">
<tr class="sectiontableentry1">
<tr class="sectiontableentry2">


So it alternates, but having code inbetween the tr's. Does anybody know how to do this?

Kind Regards.
MrBlueSky
Do you mean how to output the alternating TR's using PHP? In that case you can use

Code:

    echo '<tr class="sectiontableentry';
    if ($x%2==0) echo "1"; else echo "2";
    $x++;
    echo '">';


instead of

Code:

echo '<tr class="sectiontableentry1">';


Or, if you like it short:

Code:

echo '<tr class="sectiontableentry'.($x++%2==0?"1":"2").'">';


Wink
kv
Neater way of doing the same

Code:

if ($x%2==0)
   echo '<tr class="sectiontableentry1">';
else
   echo '<tr class="sectiontableentry2">';


or

Code:

$rowclass="sectiontableentry";
if($x++%2==0)
   $rowclass.="1";
else
   $rowclass.="2";

echo "<tr class='$rowclass'>";
Diablosblizz
Thanks guys. Though this code works, I am interested on how it works. May one of you explain how it does?

Many thanks.
Stubru Freak
It's quite logical.

You have a variable x, which is always incremented.
The % operator takes the remainder of an integer division.
So 1%2 = 1, 2%2 = 0, 3%2 = 1 again and so on.
Related topics
Using Key Words
Words Are Not Enough
Words to Live by
How many words per minute do you type?
Not Voting is Reasonable for People Who Want Freedom
science vs. religion
words about our life
Delete Words Above Rank Image In PhpBB
Do words mean anything?
Funny Animal Moments
What were Zarqawis last words??
this aint poetry... its spoken word
Do you know how to love?
[SOLVED] Offensive words...
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.