Jump to content

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.)???

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.