PHP Alternating Color Table Rows for Drupal or Dreamweaver sites
April 30, 2004
Drupal | PHP

This is based on the great tutorial by phpfreak at phpfreaks.com For a more complete explanation of this, take a look at his article.

The main variation I have made here is to adapt it for the drupal database and to separate the HTML more from the PHP to make it easier for designers to alter without needing to escape the code characters: a big source of errors for those of us who are not writing code day in and day out.

This will also work well dropped into a Dreamweaver-created php site - just change "nid" and "title" to whatever fields you want to display instead, and use the query dreamweaver generates when you define a recordset.

If you are new at this, it may go more smoothly if you first create a dynamic site in Dreamweaver, then create a dynamic table with whatever fields you want, THEN add in the additional code to generate the colors by comparing that file and this.


Ads by Google

Posted by ellen at April 30, 2004 01:33 PM



NOTE: If using this in a Drupal static page, leave off this line: <?php 


mysql_connect ('localhost', 'username', 'password') ;
mysql_select_db ('MyDatabase'); ?>


<HTML>
<HEAD>
<TITLE>Node titles browser with alternating colored rows</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</HEAD>

<BODY>



<table border='0' cellpadding='4' cellspacing='0'><TR><TD>No.</TD><TD>Title</TD></TR> 

<!--  **** color variables and initial row count ***-->
<?php  
$color1 = "#CCFFCC"; 
$color2 = "#BFD8BC"; 
$row_count = "0";  

$NodeTitles = mysql_query("SELECT nid, title FROM node WHERE type = 'story' ORDER BY title ASC") or die (mysql_error()); 
while ($row = mysql_fetch_assoc($NodeTitles)) { 
$Nid = $row["nid"]; 
$Title = $row["title"]; 
$row_color = ($row_count % 2) ? $color1 : $color2; 
?>
<tr> 
<td width="110" bgcolor="<?php echo $row_color; ?>"> 
<?php echo $row_color; ?></td> 
<td bgcolor="<?php echo $row_color ?>"> 
<A href="http://your.drupalsite.com/node/view/<?php echo $row["nid"]; ?>"><?php echo $row["title"];?></a>
</td></tr>

<?php
    $row_count++; 
} 
 ?>

</table>

</BODY>
</HTML>
<?php
mysql_free_result($NodeTitles);
?>

Ads by Google

1 Comment

It works very well. Thank you!


Ads by Google

 RSS   |   Contact Me


Ads by Google