ncurran217 Posted February 5, 2013 Share Posted February 5, 2013 I am looking to alternate the row color on this loop I have. I have looked up a couple things through google, but none of them seem to fit into what I have. Let me know if this is simple or any suggestions on how to do this. Here is my code: $fields = array('RefNumber'=>'Reference Number','Status'=>'Status','Rep'=>'Rep','Disposition'=>'Disposition','appNumber'=>'appNumber', 'Con_Number'=>'Contract Number','Finance_Num'=>'Finance Number','Phone_Num'=>'Phone Number','Disc_Amount'=>'Discount Amount', 'Total_Cost'=>'Total Cost','Total_MP'=>'Total Monthly Payments','New_MP_Amt'=>'New MP Amount','New_DP_Amt'=>'New DP Amount','Notes'=>'Notes'); // start table and produce table heading echo "<table>\n<tr>"; $color1 = "#EFEFEF"; $color2 = "#FBFBFB"; foreach($fields as $legend){ echo "<th>$legend</th>"; } echo "</tr>\n"; // output table data while($row = sqlsrv_fetch_array( $result,SQLSRV_FETCH_ASSOC)) { echo "<tr>"; foreach($fields as $key=>$not_used) { echo "<td>$row[$key]</td>"; } echo "</tr>\n"; } echo "</table>\n"; $color1 & $color2 are the background colors I want to alternate on each row. Quote Link to comment Share on other sites More sharing options...
ncurran217 Posted February 5, 2013 Author Share Posted February 5, 2013 Ok I finally found something that worked. Sorry for the premature post, but if there are possibly better ways to do this that would be nice to know. Here is what I got and I will mark as solved: // each array key is the database column name, the corresponding value is the legend/heading to display in the HTML table // the order of the items in this array are the order they will be output in the HTML table $fields = array('RefNumber'=>'Reference Number','Status'=>'Status','Rep'=>'Rep','Disposition'=>'Disposition','appNumber'=>'appNumber', 'Con_Number'=>'Contract Number','Finance_Num'=>'Finance Number','Phone_Num'=>'Phone Number','Disc_Amount'=>'Discount Amount', 'Total_Cost'=>'Total Cost','Total_MP'=>'Total Monthly Payments','New_MP_Amt'=>'New MP Amount','New_DP_Amt'=>'New DP Amount','Notes'=>'Notes'); // start table and produce table heading $color1 = "white"; $color2 = "#A4A4A4"; $i = 1; echo "<table>\n<tr>"; foreach($fields as $legend){ echo "<th>$legend</th>"; } echo "</tr>\n"; // output table data while($row = sqlsrv_fetch_array( $result,SQLSRV_FETCH_ASSOC)) { if ($i % 2 != 0) //An Odd Row {$rowcolor = $color1;} else //An Even Row {$rowcolor = $color2;} echo "<tr bgcolor='$rowcolor'>"; foreach($fields as $key=>$not_used) { echo "<td>$row[$key]</td>"; } $i++; //Increment Row Counter echo "</tr>\n"; } echo "</table>\n"; Quote Link to comment Share on other sites More sharing options...
Nodral Posted February 5, 2013 Share Posted February 5, 2013 try this $i=1; while($row = sqlsrv_fetch_array( $result,SQLSRV_FETCH_ASSOC)){ if($i=1){ echo "<tr style='background-color:".$color1.">"; $i++; }else{ echo "<tr style='background-color:".$color2.">"; $i=1; } foreach($fields as $key=>$not_used) { echo "<td>$row[$key]</td>"; } echo "</tr>\n"; dirty, but works Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 5, 2013 Share Posted February 5, 2013 (edited) @Nodral: You shouldn't replicate code that does not change int he alternating logic. It will lead to bloated, hard to maintain code. Keep it to the core values. @ncurran217: The ternary operator is a great solution for this // each array key is the database column name, the corresponding value is the legend/heading to display in the HTML table // the order of the items in this array are the order they will be output in the HTML table $fields = array( 'RefNumber'=>'Reference Number', 'Status'=>'Status', 'Rep'=>'Rep', 'Disposition'=>'Disposition', 'appNumber'=>'appNumber', 'Con_Number'=>'Contract Number', 'Finance_Num'=>'Finance Number', 'Phone_Num'=>'Phone Number', 'Disc_Amount'=>'Discount Amount', 'Total_Cost'=>'Total Cost', 'Total_MP'=>'Total Monthly Payments', 'New_MP_Amt'=>'New MP Amount', 'New_DP_Amt'=>'New DP Amount', 'Notes'=>'Notes'); $color1 = "#FFFFFF"; $color2 = "#A4A4A4"; // start table and produce table heading echo "<table>\n<tr>"; foreach($fields as $legend) { echo "<th>$legend</th>"; } echo "</tr>\n"; // output table data $rowCount = 1; while($row = sqlsrv_fetch_array( $result,SQLSRV_FETCH_ASSOC)) { $rowcolor = ($rowCount % 2) ? $color1 : $color2; //Ternary operator echo "<tr bgcolor='{$rowcolor}'>"; foreach(array_keys($fields) as $key) { echo "<td>{$row[$key]}</td>"; } echo "</tr>\n"; $rowCount++; //Increment Row Counter } echo "</table>\n"; Edited February 5, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.