Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternating TR BGCOLOR with each loop of the repeat region funtion

Status
Not open for further replies.

Kharas

Programmer
Jan 30, 2007
39
MX
I have inv_list.php as a page that shows the basic details of each forklift article: ID#, A small thumbnail, Manufacturer, Model, Year and Price.

The way it works is the following: I have a table that looks like this:

Headers: ID,Img_URL,Manufacturer,Model,Price,Year
Content: ID,Img_URL,Manufacturer,Model,Price,Year

The whole row content has a repeat region function:

<?php do { ?>
<tr bgcolor="$rowcolor">
<td align="left" scope="col" >&nbsp;<a href="inv_detalle.php?siittoID=<?php echo $row_lista_main['mont_ID']; ?>"><?php echo $row_lista_main['mont_ID']; ?></a></td>
<td align="left" scope="col"><img src="<?php if ($rows_conteo_prod <= 0) { echo "";} else {echo $row_lista_main['img_url1']; } ?>" height="30" width="30"></td>
<td align="left" scope="col"><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo $row_lista_main['marca_name'];} ?></td>
<td scope="col">&nbsp;</td>
<td align="left" scope="col"><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo $row_lista_main['modelo'];} ?></td>
<td scope="col">&nbsp;</td>
<td align="left" scope="col"><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo $row_lista_main['year'];} ?></td>
<td scope="col">&nbsp;</td>
<td align="left" scope="col"><?php if ($rows_conteo_prod <= 0) { echo "";} else {echo "$".$row_lista_main['precio'];} ?></td>
</tr><?php } while ($row_lista_main = mysql_fetch_assoc($lista_main));
?>

So that way, you only have as many table rows as data rows in the database.

$rowcolor makes reference to:

<?php

do { $rowcolor = ($rowcolor=="#CCCCCC"){$rowcolor= "#FFFFFF";}else{ $rowolor = "#CCCCCC";}

?>

Unfortunately this returns an error.
Does anybody know how to alternate the bgcolor with every loop of the repeat region function?

Thanks in advance
 
Oh, I apologize for posting the old version:

<tr bgcolor="$rowcolor"> should have been
<tr bgcolor='<?php echo $rowcolor;?>' >
 
Ok it's working now:

<?php

if ($rowcolor = "#FFFFFF") { $rowcolor = "#CCCCCC" ; } else {$rowcolor = "#FFFFFF" ;}

?>

<tr bgcolor="<?php if ($rowcolor == "#FFFFFF") { $rowcolor = "#CCCCCC" ; } else {$rowcolor = "#FFFFFF" ;} {echo $rowcolor;} ?>">

A million thanks to Bastien
 
you should read up on the difference between "=" (the assignment operator) and "==".

you could also use a ternary operator for neatness of coding

Code:
$rc = ($rc=="#CCCCCC") ? "#FFFFFF" : "#CCCCCC";
echo <<<EOL
<tr bgcolor="$rc"><td>&nbsp;</td></tr>
EOL;
//using the heredoc syntax
//or
echo '<tr bgcolor="'.$rc.'"><td>&nbsp;</td></tr>';
//using a more standard syntax.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top