PDA

View Full Version : Generating alternating classes for dynamic tables?


colinkites2000
05-13-2009, 02:55 PM
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

abduraooft
05-13-2009, 03:11 PM
<style type="text/css">
tr.even {
background:#ccc;
}
tr.odd{
background:#ccf;
}
</style>

<table>
<?php
$i=0;
while($i<5){
echo "<tr class=".($i%2==0? '"even"':'"odd"').">";
echo "<td>$i</td></tr>";
$i++;
}
?>
</table>

colinkites2000
05-13-2009, 03:54 PM
Thanks for this A!! Now I know it is possible, but I am having trouble implementing.... Could you explain how I can set this up with my table below? Sincerely, Colin


<h2>Table 1 - </h2></p>

<div>



<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 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></div>

abduraooft
05-13-2009, 04:01 PM
Something like
<table>
<?php
$i=0;
do {
echo "<tr class=".($i%2==0? '"even"':'"odd"').">";
?>

<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 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 $i++;
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>

colinkites2000
05-13-2009, 06:41 PM
Super clean solution. I can now keep building my site in Dreamweaver and then simply find and replace the two snippets of code. THANKS! HUGE HELP!!