[Solved] Alternate table row colours

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Hello, Brian.

Well, pardon for the word but your code is way too messed :). There are missing quotes (") here and there, wrong loop statement declaration, single-quote in place of double, missing scape character (\), etc. And these are the syntax errors only. There are also some logic errors: missing database querying and fetching functions (maybe they are few lines earlier in the code), variable mistype, etc.

I decided to start from the initial code, the one you started from too:

Code: Select all

<table width="100%" border="1">
  <tr>
  <td><h2>250 Championship</h2></td>
  </tr>
  <tr><td>
<table width="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr align="center">
            <td><h1>Name</h1></td>
            <td><h1>&nbsp;</h1></td>
            <td><h1>Cookstown</h1></td>
            <td><h1>Tandragee</h1></td>
            <td><h1>NW200</h1></td>
            <td><h1>Athea</h1></td>
            <td><h1>Skerries</h1></td>
            <td><h1>Southern</h1></td>
            <td><h1>Walderstown</h1></td>
            <td><h1>Kells</h1></td>
            <td><h1>Faugheen</h1></td>
            <td><h1>Mid-Antrim</h1></td>
            <td><h1>UGP </h1></td>
            <td><h1>Killalane</h1></td>
            <td><h1>Scarborough</h1></td>
            <td><h1>Total</h1></td>
  </tr><!-- Database might be 'initialized' some point above. Please read the note #1 below this code block -->
          <?php do { // Please read note #2
						$class = @($class == 'trOne') ? 'trTwo' : 'trOne';
					?>
            <tr class="<?php echo $class; ?>"> <!-- Please read note #3 -->
            <td><p><?php echo $row_get250Results['f_name']; ?></p></td>
            <td><p><?php echo $row_get250Results['l_name']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['cookstown']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['tandragee']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['northwest']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['athea']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['skerries']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['southern']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['walderstown']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['kells']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['faugheen']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['midantrim']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['ugp']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['killalane']; ?></p></td>
            <td align="center" class="innerborder"><p><?php echo $row_get250Results['scarborough']; ?></p></td>
            <td align="center"><p><?php echo $row_get250Results['TotalScore']; ?></p>
            </td>
          </tr>
          <?php } while ($row_get250Results = mysql_fetch_assoc($get250Results)); ?>
</table>
</td>
</tr>
</table>
Note #1: The database must have to be 'initialized' (mysq_connect, mysq_select_db, query, etc) some point before this line. Since the initial code is fully functional as you said, we can assume that database setup and querying were made.

Note #2: I presume your code checks if at least 1 row was returned from DB before it shows this table. There might be some if (mysql_num_rows(... > 0) a few lines earlier in the code. If not, consider Syranide's recommendation about getting a NULL for the first loop.

Note #3: Setting background color for a <tr> only works if their <td> children have background color set to null.

I think this is it. If you still have problems I suggest you to post the entire code.

Hope it helps!

Falou,
Scorphus.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Hi Scorphus,
I’m most humbled by your help which gave me a perfect result. It was a good tutorial too and as I’m so eager to get the hang of PHP was most welcome. I would love to someday be in a position to help others in the way you (all) have helped me.
Thanks again
Brian
:wink: :wink: :wink:
Post Reply