Jump to content

PHP Table Alternating Row


ncurran217

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/274061-php-table-alternating-row/
Share on other sites

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

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

@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";

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.