PHP: Circumference of planets table with alternating rows
$rad=array(
'mercury' => 1516,
'venus' => 3760,
'earth' => 3959,
'mars' => 2104,
'jupiter' => 42982,
'saturn' => 35615,
'uranus' => 15700,
'neptune' => 15256,
);
echo "<table border='1' width='300' >";
echo "<tr><td>Planet</td><td>Radius (mi)</td><td>Circumference (mi)</td></tr>";
foreach($rad as $key => $val){
$cir=(2*M_PI)*$rad[$key];
if($key == 'venus' || $key == 'mars' || $key == 'saturn' || $key == 'neptune'){
echo "<tr><td><b>";
printf("%s", $key);
echo "</b></td>";
echo "<td><b>";
printf("%.2f", $val);
echo "</b></td>";
echo "<td><b>";
printf("%.2f",$cir);
echo "</b></td></tr>";
}
else{
echo "<tr><td>";
printf("%s", $key);
echo "</td>";
echo "<td>";
printf("%.2f", $val);
echo "</td>";
echo "<td>";
printf("%.2f",$cir);
echo "</td></tr>";
}
}
echo "</table>";
'mercury' => 1516,
'venus' => 3760,
'earth' => 3959,
'mars' => 2104,
'jupiter' => 42982,
'saturn' => 35615,
'uranus' => 15700,
'neptune' => 15256,
);
echo "<table border='1' width='300' >";
echo "<tr><td>Planet</td><td>Radius (mi)</td><td>Circumference (mi)</td></tr>";
foreach($rad as $key => $val){
$cir=(2*M_PI)*$rad[$key];
if($key == 'venus' || $key == 'mars' || $key == 'saturn' || $key == 'neptune'){
echo "<tr><td><b>";
printf("%s", $key);
echo "</b></td>";
echo "<td><b>";
printf("%.2f", $val);
echo "</b></td>";
echo "<td><b>";
printf("%.2f",$cir);
echo "</b></td></tr>";
}
else{
echo "<tr><td>";
printf("%s", $key);
echo "</td>";
echo "<td>";
printf("%.2f", $val);
echo "</td>";
echo "<td>";
printf("%.2f",$cir);
echo "</td></tr>";
}
}
echo "</table>";
//Result
Planet | Radius (mi) | Circumference (mi) |
mercury | 1516.00 | 9525.31 |
venus | 3760.00 | 23624.78 |
earth | 3959.00 | 24875.13 |
mars | 2104.00 | 13219.82 |
jupiter | 42982.00 | 270063.87 |
saturn | 35615.00 | 223775.64 |
uranus | 15700.00 | 98646.01 |
neptune | 15256.00 | 95856.28 |
No comments:
Post a Comment