Jump to content

PHP CSS Alternating Row Colors


dprichard

Recommended Posts

I have a query and a do while loop.  I am trying to alternate row colors and think I can do it if I can pull in a number with each result returned, but am not sure what direction to head with it.  Any assistance would be greatly appreciated.

 

Here is my query:

 

$documents = mysql_query("SELECT docid, docname, docdescription, docbegindate, docenddate, docstatus, doctypeicon, doctypename, docfile, emp_name, emp_email, docstatusname, docorder FROM documentsmain WHERE (docstatus = '2' or docstatus = '1') AND docfolder = '$dcparent'") or die(mysql_error());
$row_documents = mysql_fetch_array($documents);
$row_documents_total = mysql_num_rows($documents);

 

Here is my loop:

 

<?php do {?><tr>
                  <td width="5" class="list_item_boxleft"><table width="10" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td class="icon_box"><a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank"><img src="images/file_icons/<?php echo $row_documents['doctypeicon']; ?>" alt="<?php echo $row_documents['docdescription']; ?>" width="16" height="16" border="0"></a></td>
                    </tr>
                  </table>                    <a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank"></a><a href="uploads/<?php echo $row_documents['docfile']; ?>"></a></td>
                  <td class="list_item_box"><a href="uploads/<?php echo $row_documents['docfile']; ?>" target="_blank" title="<?php echo $row_documents['docdescription']; ?>"><?php echo $row_documents['docname']; ?></a></td>
                  <td class="list_item_box"><?php echo $row_documents['docbegindate']; ?></td>
                  <td class="list_item_box"><?php echo $row_documents['docenddate']; ?></td>
                  <td class="list_item_box"><?php echo $row_documents['emp_name']; ?></td>
                  <td class="list_item_box"><?php echo $row_documents['docstatusname']; ?></td>
                  <td width="50" height="3" nowrap class="list_item_box"><form name="form3" method="post" action="">
               
                    <input name="docorder" type="text" id="docorder" value="<?php echo $row_documents['docorder']; ?>" size="3" maxlength="3">
                    <input name="submit" type="image" src="/images/refresh.png" alt="Approve Time Off Request" align="middle" width="16" height="16">
                    

                      </form>                  </td>
                  <td class="list_item_boxright"><div align="center"> </div></td>
                </tr><?php } while ($row_documents = mysql_fetch_array($documents)); ?>

Link to comment
https://forums.phpfreaks.com/topic/57553-php-css-alternating-row-colors/
Share on other sites

No need to pull in a number, use an auto increment variable and than use the modulous (%) sign.

 

Since your code goes in and out, which is not necessary here is a basic example:

 

<?php
$i=0;
$return_html = "";
while ($row_documents = mysql_fetch_array($documents) {
            $row_class = (($i%2) == 0)?"alternating_class":"first_class";
            $return_html .= '<tr><td class=' . $row_class . '>Testing</td></tr>';
}

echo $return_html;
?>

Okay, trying it like this, but am getting unexpected T_LNUMBER in line 165 which is the last line below

 

				<?php
			$i=0;
			$return_html = "";
			while ($row_documents = mysql_fetch_array($documents)) {
			$row_class = (($i%2) == 0)?"alt_class":"first_class";
			$return_html .='<tr>
                    <td width='10' class='list_item_boxleft'><table width='1' border='0' cellspacing='0' cellpadding='0'>
    

 

There are more lines below that, but that is where I am getting the error.

<?php
$i=0;
$return_html = "";

while ($row_documents = mysql_fetch_array($documents)) {
$row_class = (($i%2) == 0)?"alt_class":"first_class";
        $return_html .= "<tr><td width='10' class='" . $row_class . "'><table width='1' border='0' cellspacing='0' cellpadding='0'>";
        $i++; // forgot this part earlier
}

 

Or it could be because of syntax errors.

i'll give you the code i use to accomplish this and see if you cant put it into your code

 

<?php
$bg = '#eeeeee';//sets the row background color
do { //begin loop
$bg = ($bg == '#eeeeee' ? '#ffffff' : '#eeeeee');//switches background color ?>
  <tr bgcolor="<?php echo $bg;?>"><!--echos the background variable-->
             <td><?php echo $row_documents['bla'];?></td>
             <td><?php echo $row_documents['bla'];?></td>
             <td><?php echo $row_documents['bla'];?></td>
  </tr>
<?php } while ($row_documents = mysql_fetch_array($documents)); ?>

 

you can also use css, just change the <tr bgcolor= to display a class instead of the bgcolor and replace the #eeeeee, #ffffff with the name of the classes

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.