Jump to content

[SOLVED] Every other while highlighting


Clinton

Recommended Posts

How do I get every other result to highlight its background a different color?

 

Here's me code:

 

<table width="95%" border="0" cellspacing="0" cellpadding="1" class="boxtitle">
				<?php

				$sql = "SELECT * FROM thelist WHERE username = '$uid' ORDER BY jpted";

				$rs = mysql_query($sql);

        			while($row = mysql_fetch_array($rs))
				{
    				extract($row);
    				?>
    				
  					<tr><td>
				<a href="editaj.php?jid=<?php echo $id; ?>"> <?php echo $jtitle; ?> </a></td> <td> <?php echo $city; ?>, <?php echo $state; ?> </td>     <td><a href="editaj.php?jid=<?php echo $id; ?>"> Edit </a></td>     <td><a href="editaj.php?jid=<?php echo $id; ?>&del=yes"> Delete </a></td>
				</td></tr>
				<?php } ?>

		</table>

 

 

Link to comment
https://forums.phpfreaks.com/topic/139743-solved-every-other-while-highlighting/
Share on other sites

<table width="95%" border="0" cellspacing="0" cellpadding="1" class="boxtitle">
               <?php

               $sql = "SELECT * FROM thelist WHERE username = '$uid' ORDER BY jpted";

               $rs = mysql_query($sql);
               $i=0;
                 while($row = mysql_fetch_array($rs))
               {
                extract($row);
                
                 $bground = "some style";
                 if (($i%2) == 0) 
                       $bground = "somealretnatestyle";
                 $i++;
                ?>
                
                 <tr class="<?php echo $bground; ?>"><td>
               <a href="editaj.php?jid=<?php echo $id; ?>"> <?php echo $jtitle; ?> </a></td> <td> <?php echo $city; ?>, <?php echo $state; ?> </td>     <td><a href="editaj.php?jid=<?php echo $id; ?>"> Edit </a></td>     <td><a href="editaj.php?jid=<?php echo $id; ?>&del=yes"> Delete </a></td>
               </td></tr>
               <?php } ?>

         </table>

 

And a side note, the % operator is the modulus operator if you want to look up on it.

 

http://us2.php.net/manual/en/language.operators.arithmetic.php

Ok, so it does a/b or in this case $i/2. $i starts off as 0 so 0/2 = 0 and it's the first style. Then it adds one $i++ so then it's 1/2, which is different then 0. But then we add one more, right??? Which then becomes 2/2... which equals 1... which is not 0... so how does the script work (and it does work, thank you.)???

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.