Jump to content

Help: alternating row colours in php mysql dynamic table


azukah

Recommended Posts

Hi-

so I managed to make this code work... Updating a table dynamically from php mysql and know got to add alternating row colours... Ideas??

 

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

easy. use php to alter the background colour of the row as it creates the row

something like this

 

if ( ($rowcount % 2) == 0) {
$bgcol = '#FFFFFF;';
} else {
$bgcol = '#000000;';
}
echo sprintf('<tr style="background-color:%s">', $bgcol);

You could always add a counter to keep track of how many rows you've printed so far, then use modulus (%) to figure out whether the row is odd or even. If you divide a whole number by two then the remainder will either be 0 (even) or 1 (odd). Take that, then change the colour as required. :)

 

For example, let's say we've got a 5 rows to print out:

<?php
$i = 0;
foreach($rows as $key => $value) {
if ($i % 2 == 1) { $colour = "#FFFFFF"; } else { $colour = "#000000"; }
echo $colour;
$i++;
}
?>

 

Apply this to your loop and you ought to be good to go! :)

 

Edit: Beaten by Internet downtime. Curses! :)

You'd need to place it within your while loop where you display your results

        <?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">€ <?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)); ?>

 

Change the above to

        <?php
        $i = 0;
        while ($row_get_prods = mysql_fetch_assoc($get_prods))
        {
            $bgColorg = (($i % 2 === 0) ? "#FFFFFF" : "#000000");
        ?>
        <tr align="center" valign="middle" class="mytbltxt" style="background-color: <?php echo $bgColor ?>">
        <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">€ <?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 $i++; } ?>

Archived

This topic is now archived and is closed to further replies.

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