Saturday, 24 September 2016

PHP alternating colors




$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";

$result = mysql_query($sql);


while($row = mysql_fetch_array($result)) {

echo "

".$row['first_name']." ".$row['last_name']."
 

";


}


I wanted to add an alternating color in the loop. what code should I add?


Answer



$rowCount = 0;
$colorOne = '#ffffff';
$colorTwo = '#f3f3f3';

while($row = mysql_fetch_array($result)){

$rowColor = ($rowCount % 2) ? $colorOne : $colorTwo;
echo "";
$rowCount++;
}


Edited after @Jayrox comment.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...