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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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