Help: alternating row colours in table php mysql

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

Post Reply
azukah
Forum Newbie
Posts: 3
Joined: Sat Mar 13, 2010 10:08 pm

Help: alternating row colours in table php mysql

Post by azukah »

So I managed to get my code to work to populate a table dynamically from mysql with php. Now I have to show alternating row colours in the table and I'm list... Any ideas?? Don't even know where to start...

<?php
require_once('../cms_config.php');
$myfilter=$_GET['catfilter'];
if (isset($myfilter) && $myfilter!=""){
$add_condition=" AND prod_cat=$myfilter ";
}else{
$add_condition="";
}
mysql_select_db($database, $makeconnection);
$sql_get_prods="SELECT *, cat_name, cat_id
FROM tbl_products p, tbl_cat c
WHERE p.prod_cat = c.cat_id $add_condition
ORDER BY prod_id ASC";
$get_prods = mysql_query($sql_get_prods, $makeconnection) or die(mysql_error());
$row_get_prods = mysql_fetch_assoc($get_prods);
$totalRows_get_prods = mysql_num_rows($get_prods);

mysql_select_db($database, $makeconnection);
$sql_get_cats="SELECT *
FROM tbl_cat
ORDER BY cat_name ASC";
$get_cats = mysql_query($sql_get_cats, $makeconnection) or die(mysql_error());
$row_get_cats = mysql_fetch_assoc($get_cats);
$totalRows_get_cats = mysql_num_rows($get_cats);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Kuy:to CMS | Vectors</title>
<link href="../../css/cssBackEnd.css" rel="stylesheet" type="text/css" />
<script src="../../js/jsMain.js" type="text/javascript"></script>
</head>
<body>
<div id="cmswrapper">
<?php include("../xcmsnav.php");?>
<div id="cms_heading">
<div id="cms_title">
<h1>Vectors Management:</h1>
<p>Add or delete vectors, enter artist/creator name and update your vector description or price.</p>
</div>
<!--END OF DIV CMS_TITLE-->
<div id="cms_add"> <a href="prod_add.php"><img src="../cms_images/bg_icons3.png" id="myAddIcon" /></a>
<p><a href="prod_add.php"> Add a new vector</a></p>
</div>
<!--END OF DIV CMS_ADD-->
</div>
<!--END OF DIV CMS_HEADING-->
<div class="myclear"></div>
<div id="cms_form">
<form name="form" id="form">
<p>View Products:
<select name="jumpMenu" class="myDropDown" id="jumpMenu" onchange="MM_jumpMenu('self',this,0)">
<option selected="selected">Select Category</option>
<option>---------------</option>
<option value="prod_view.php">All Categories</option>
<?php do {
if($row_get_cats['cat_id']==$myfilter){
$makestelcted=' selected="selected" ';
}else{
$makestelcted='';
}
?>
<option
<?php echo $makestelcted; ?>
value="prod_view.php?catfilter=<?php echo $row_get_cats['cat_id']; ?>"> <?php echo $row_get_cats['cat_name']; ?></option>
<?php } while ($row_get_cats = mysql_fetch_assoc($get_cats)); ?>
</select>
<span><em>(<? echo $totalRows_get_prods ?> vectors found)</em></span></p>
</span>
</form>
</div><!--END OF CMS_FORM-->
<div class="myClear"></div>
<div id="cms_table">

<table width="960" border="0" align="left" cellpadding="5" cellspacing="0">
<tr bgcolor="#F0F0F0" align="center" valign="middle" class="mytbltitle">
<td width="50" align="left" ><strong>New</strong></td>
<td width="120" align="left" ><strong>Vector Name</strong></td>
<td width="100" align="left" ><strong>Artist Name</strong></td>
<td width="100" ><strong>Image</strong></td>
<td width="75" ><strong>Price</strong></td>
<td width="115" ><strong>Category</strong></td>
<td width="250" ><strong>Description</strong></td>
<td width="75" ><strong>Modify</strong></td>
<td width="75" ><strong>Delete</strong></td>
</tr>
<?php do { ?>
<tr align="center" valign="middle" class="mytbltxt">
<td align="left" valign="top"></td>
<td align="left" valign="top"><?php echo $row_get_prods['prod_name']; ?></td>
<td align="left" valign="top"><?php echo $row_get_prods['prod_artist']; ?></td>
<td valign="top"><img src="../../images/products/<?php echo $row_get_prods['prod_img']; ?>" height="50" class="myicontxt"/></td>
<td valign="top">&#8364; <?php echo $row_get_prods['prod_price']; ?></td>
<td valign="top"><?php echo $row_get_prods['cat_name']; ?></td>
<td valign="top"><textarea name="textarea" cols="25" rows="1" class="mycomments" id="textarea" ><?php echo $row_get_prods['prod_desc']; ?></textarea></td>
<td valign="top"><a href="prod_modify.php?prod=<?php echo $row_get_prods['prod_id']; ?>"><img src="../cms_images/bg_icons4.png" id="myModifyIcon" /></a></td>
<td valign="top"><a href="javascript:deleteprod(<?php echo $row_get_prods['prod_id']; ?>);"><img src="../cms_images/bg_icons4.png" id="myDeleteIcon" /></a></td>
</tr>
<?php } while ($row_get_prods = mysql_fetch_assoc($get_prods)); ?>
</table><!--END OF TABLE-->
</div><!--END OF CMSTABLE-->
</div><!--END OF WRAPPER-->
</body>
</html>
<?php mysql_free_result($get_prods);?>
michaeru
Forum Commoner
Posts: 28
Joined: Sun Mar 07, 2010 5:22 pm

Re: Help: alternating row colours in table php mysql

Post by michaeru »

Track the number of rows you already have. If the number of rows is even, set the background color of that table row to one color, then if it is odd, set the background color of that table row to another color.

Sample code:

Code: Select all

 
<?php
  
  $numRows = 0;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
 
    if($numRows % 2 == 0) {
 
?>
 
<tr bgcolor="#F00"></td>
 
<?php
 
    } else {
 
?>
 
<tr bgcolor="#0F0"></td>
 
<?php
 
    }
 
    $numRows++;
 
  }
 
?>
 
Note: I do not use bgcolor, so I do not know if it accepts that param.
DaiLaughing
Forum Commoner
Posts: 76
Joined: Thu Jul 16, 2009 8:03 am

Re: Help: alternating row colours in table php mysql

Post by DaiLaughing »

I tend to do a similar thing but set the class of the element to use styles for the background-color. Set the class to one and use an if to set it to the other each time through the loop. michaeru's way to alternate is more elegant but use CSS classes instead of bgcolor.
azukah
Forum Newbie
Posts: 3
Joined: Sat Mar 13, 2010 10:08 pm

Re: Help: alternating row colours in table php mysql

Post by azukah »

:D
thanks!!!
Post Reply