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


Alternating between even and odd in a while loop





Diablosblizz
I am wondering if it is possible to alternate between even and odd in my news system which is in a while loop. I currently have:

Code:
      
$getfrom = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0, 3");
while($news = mysql_fetch_array($getfrom)) {
                     $title = $news['title'];
                     $short = $news['shortmsg'];
                     $id = $news['id'];
                     $date = $news['datep'];
echo "<ul class='widelist'>           
            <li class='even'>
                <a href='news.php?id=$id'>$title</a><div class='newsitem-date'>$date</div>
            </li>                       
        </ul>";
        }


If you didn't notice, the "li" has even as the class. I am wondering if when the while loop goes around for the second time to make the even change to odd? So it would display:

Code:
<ul class='widelist'>           
            <li class='even'>
                <a href='news.php?id=$id'>$title</a><div class='newsitem-date'>$date</div>
            </li>                       
        </ul>
<ul class='widelist'>           
            <li class='odd'>
                <a href='news.php?id=$id'>$title</a><div class='newsitem-date'>$date</div>
            </li>                       
        </ul>


It would display this in the source code. Many thanks for your help!
Diablosblizz
I apologize for double posting, but I do not need help anymore. Mod, if you wish you can close this topic.
Peterssidan
You can do something like this in the loop:
Code:
$b =!b;
if($b)
   echo...
else
   echo....

Where $b is a boolean that switch from true to false and from false to true.
Diablosblizz
I did a number adding.

Outside of the while loop there was a number defined and then inside the loop it adds itself. Then I did a if the number equals one to display odd and two to display even.
Related topics
[php scripts ] phpweather&email
Make search engine With PHP and mySQL, for your site
If Hardly Davidson made Computers instead of Motorcycles...
The justification for war
C++ help
Fortran Tutorials(77)
New Superman Movie "Superman Returns"
need help building a switch/case dynamically w/ a loop &
Help: Want to update an old inline JS feature.
Is Jesus Perfect?
I want to be xhtml compliant...
AJAX tutorial [2nd part now updated]
Taking bath in Hot Water is good?
Noobie's code
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.