Help - Search - Members - Calendar
Full Version: Assigning classes to alternating rows on dynamic table?
HTMLHelp Forums > Programming > Databases
colinkites2000
Hi All,

I'm very new to PHP and MySQL. I have been using dreamweaver to make dynamic tables to display some data. Before the tables were dynamic, I manually added classes to each table row (.even, .odd) in the html markup so that I could control the styling in CSS and have alternating colors on the rows.

Is there anyway to somehow pull the data with alternating classes like this? Or perhaps someone knows a CSS trick where I can do this without classes?

Sincerely,
Colin
pandy
You could use JavaScript (and CSS of course). I've used this snip since - oops! - 2002. blush.gif

http://lists.evolt.org/archive/Week-of-Mon...007/124011.html
colinkites2000
Thanks Pandy - don't know much about javascript but I assume that I would paste this code into my header with the javascript open and close tags... and then add an id to my table in HTML of "table_id"??

Best,
Colin

QUOTE(pandy @ May 13 2009, 09:48 AM) *

You could use JavaScript (and CSS of course). I've used this snip since - oops! - 2002. blush.gif

http://lists.evolt.org/archive/Week-of-Mon...007/124011.html

pandy
Yeah, and then change in the script to your id where it says 'table_id' (2 places) and also change the below to the class name you actually use (or use evenRow).

CODE
x[i].className = 'evenRow';


If I didn't miss something, that's all. smile.gif
Brian Chandler
QUOTE(pandy @ May 13 2009, 10:48 PM) *

You could use JavaScript (and CSS of course). I've used this snip since - oops! - 2002. blush.gif

http://lists.evolt.org/archive/Week-of-Mon...007/124011.html


I think it's simply crazy to think of using javascript for things like this.

If you have a program generating the rows of (whatever it is), simply alternate. You can use a number which you increment for each row, then use whether it's odd or even. There must be dozens of "different" ways of determining whether variable $x is odd or even, but for example

[code]
($x & 1) ? 'odd' : 'even')
[code]

is an expression with the appropriate value.
pandy
Why is it crazy? It isn't like it's something very important.
colinkites2000
Thanks Brian - I'm not sure exactly how to implement this. This is the table I am working with. Any further advice would be greatly appreciated. Best, Colin

CODE
     <h2>Table 1 - Abrasive Cutting Discs for use with Angle Grinders - Form 41</h2></p>

    <div>

          <table id="product" class="product">

            <tr>

              <th>Diameter</th>

           <th>Thickness</th>

              <th>application</th>

              <th>Brand</th>

              <th>Product Id</th>

              <th>List Price</th>

              <th>Pcs Box</th>

              <th>Order</th>

              <th>&nbsp;</th>

            </tr>

            <?php do { ?>

              <tr>

                <td><?php echo $row_Recordset1['Diameter']; ?></td>

                <td><?php echo $row_Recordset1['Thickness']; ?></td>

                <td><?php echo $row_Recordset1['application']; ?></td>

                <td><?php echo $row_Recordset1['Brand']; ?></td>

                <td><?php echo $row_Recordset1['product_Id']; ?></td>

                <td>$<?php echo $row_Recordset1['List_Price']; ?></td>

                <td><?php echo $row_Recordset1['Pcs_Box']; ?></td>

                <td><?php echo $row_Recordset1['Order']; ?></td>

                <td><form method="post" name="form2" action="<?php echo $editFormAction; ?>">

            <table id="" class="addtocart">

              <tr valign="baseline">

                <td nowrap align="right">New Price:</td>

                <td><input type="text" name="List_Price" value="<?php echo htmlentities($row_Recordset1['List_Price'], ENT_COMPAT, 'UTF-8'); ?>" size="4"></td>

                <td nowrap align="right">New Order:</td>

                <td><input type="text" name="Order" value="<?php echo htmlentities($row_Recordset1['Order'], ENT_COMPAT, 'UTF-8'); ?>" size="10"></td>

                <td nowrap align="right">&nbsp;</td>

                <td><input type="submit" value="Update"></td>

              </tr>

            </table>

            <input type="hidden" name="MM_update" value="form2">

            <input type="hidden" name="product_Id" value="<?php echo $row_Recordset1['product_Id']; ?>">

          </form>

            </td>

           </tr>

          <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

          </table><br><br>

          </div>



QUOTE(Brian Chandler @ May 13 2009, 10:59 AM) *

QUOTE(pandy @ May 13 2009, 10:48 PM) *

You could use JavaScript (and CSS of course). I've used this snip since - oops! - 2002. blush.gif

http://lists.evolt.org/archive/Week-of-Mon...007/124011.html


I think it's simply crazy to think of using javascript for things like this.

If you have a program generating the rows of (whatever it is), simply alternate. You can use a number which you increment for each row, then use whether it's odd or even. There must be dozens of "different" ways of determining whether variable $x is odd or even, but for example

[code]
($x & 1) ? 'odd' : 'even')
[code]

is an expression with the appropriate value.

colinkites2000
Problem solved with PHP below. Thanks for the tips everyone.

CODE
<table>
   <?php
   $i=0;
   do {
              echo "<tr class=".($i%2==0? '"even"':'"odd"').">";
              ?>

<SNIP>

          <?php $i++;
          } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>

Brian Chandler
QUOTE(pandy @ May 14 2009, 12:18 AM) *

Why is it crazy? It isn't like it's something very important.


You have (apparently) a bit of program generating the rows of a table. It is completely trivial to vary some parameter of the row as you generate it, in a way that will be transparent. The suggestion instead of this is to generate the wrong output, and accompany it with a bit of client-side script to parse the output, and convert it to what was intended. From a programming perspective this appears obviously perverse.

But no, it's not "important" (whatever that means exactly, in this context). And people do crazy things all the time.

But it doesn't seem a very good recommendation to make in a forum such as this. (Apart from anything else, it is adding javascript dependency for no good reason.)
Brian Chandler
QUOTE(colinkites2000 @ May 14 2009, 01:46 AM) *

Problem solved with PHP below. Thanks for the tips everyone.

CODE
<table>
   <?php
   $i=0;
   do {
              echo "<tr class=".($i%2==0? '"even"':'"odd"').">";
              ?>

<SNIP>

          <?php $i++;
          } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>



Basically you have

do ( <write a row> ) while $row = getnextrow();

How does the first row get read?

A couple of other comments...

* Yes, you have put in a counter to oscillate even-odd. (You might possibly have used a more mnemonic name than i, like evenodd.)

* There's always a decision between making a (part of a) page look like a php program with bits of html scattered through it, or like a bit of html page with php scattered in it. But once you get to a loop like this, I think looking like a program makes it easier to read. It's also a good idea to parameterise on the field names. So you have an outer *row* loop, and an inner *field* loop. If you make an array keyed by the DB field names to the (longer) plain descriptions you use in the table heading you will use the same sort of loop for the headings (<th>) and the data (<td>). Then if you change something, it all updates automatically. (In real life, you _always_ change something eventually.)

pandy
QUOTE(Brian Chandler @ May 13 2009, 07:44 PM) *

But it doesn't seem a very good recommendation to make in a forum such as this.


You are right. I thought this was in the HTML forum. Sorry. blush.gif
colinkites2000
Hi Brian,

Regarding your question below....Hmm, I'm not sure about how the first row gets read... could you offer the best way to do this?

Best,
Colin



CODE
<table>
   <?php
   $i=0;
   do {
              echo "<tr class=".($i%2==0? '"even"':'"odd"').">";
              ?>

<SNIP>

          <?php $i++;
          } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>

[/quote]

Basically you have

do ( <write a row> ) while $row = getnextrow();

How does the first row get read?

A couple of other comments...

* Yes, you have put in a counter to oscillate even-odd. (You might possibly have used a more mnemonic name than i, like evenodd.)

* There's always a decision between making a (part of a) page look like a php program with bits of html scattered through it, or like a bit of html page with php scattered in it. But once you get to a loop like this, I think looking like a program makes it easier to read. It's also a good idea to parameterise on the field names. So you have an outer *row* loop, and an inner *field* loop. If you make an array keyed by the DB field names to the (longer) plain descriptions you use in the table heading you will use the same sort of loop for the headings (<th>) and the data (<td>). Then if you change something, it all updates automatically. (In real life, you _always_ change something eventually.)
[/quote]
Darin McGrew
QUOTE
How does the first row get read?
I'll use pseudo-code because I'm unfamiliar with PHP, but there are two basic approaches. One is to get the first row outside the loop:
CODE
<get first row>
do ( <write a row> ) while ( <get next row> )
The other is to get the next row at the beginning of each loop:
CODE
while ( <get next row> ) do ( <write a row> )
colinkites2000
Thanks for you help Darin. Unfortunately, I'm unfamiliar with PHP and not precisely sure how to implement this either! It seems to be working ok as is though.... is it important to add this first row reader?

Best,
Colin
Brian Chandler
QUOTE(colinkites2000 @ Jun 11 2009, 05:29 AM) *

Thanks for you help Darin. Unfortunately, I'm unfamiliar with PHP and not precisely sure how to implement this either! It seems to be working ok as is though.... is it important to add this first row reader?



It would help if you show us what you actually have. But if you do "write a row" before you have read anything in, what row will be written?

You need to read the php manual *carefully* (um, yes, there's a lot of it, but you need to understand basic constructs; you can read function definitions as you need them) -- here's the index page for "control structures":
http://jp2.php.net/manual/en/language.control-structures.php

Having a loop where first or last case is not treated properly is called a "one-off error"; sloppily written programs can contain lots of these, yet still appear to "work", but they usually go wrong later in some unpredictable way. Actually the simples standard way to do "print a load of stufff from db" is

while (<get record from db succeeds>)
{ <print record details>
}

and probably <print record details> should generate one <tr> of an html table.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.