Issue I am coming across is that my calculation is not coming out correctly, it will display "0s" if nothing is entered, also it shows the previous entered data and then the newly entered info, but multiple times. It should only be showing the gravity multiplied by the weight entered.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lab 03 - Planet Weight Calculator</title>
<?php
$planets = array( "Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto" ); //array to set the dropdown
$choice = "Mercury";
if( isset( $_POST['choice'] ) ) {
$choice = $_POST['choice'];
}
?>
<?php
$weight = "";
if( ! empty( $_POST['weight'] ) ) {
$weight = $_POST['weight'];
}
?>
</head>
<body>
<?php
$planet_file = fopen( "PLANET.TXT", "r" ); //will error if file is not found
if( ! $planet_file ) {
echo "No such file to perform task.</body></html>";
exit;
}
?>
<h1>Planet Weight Calculator</h1>
<form action="lab03.php" method="post">
<label for="choice">Choose a Planet</label><br/>
<select name="choice" id="choice">
<?php
foreach( $planets as $planet ) { //conditional to make sure that users choice of planet sticks
if( $choice == $planet ) {
echo "<option selected>$planet</option>";
} else {
echo "<option>$planet</option>";
}
}
?>
</select><br/>
<label for="weight">Enter the weight of an item: </label><br/>
<input type="text" name="weight" id="weight" value="<?php echo $weight ?>"/>
<input type="submit" value="Submit"/>
</form>
<?php
if( $choice == "Mercury" ) { //chooses the correct picture for the chosen planet
echo "<img src='PlanetImages/Planet1.jpg' />";
} else if ( $choice == "Venus" ) {
echo "<img src='PlanetImages/Planet2.jpg' />";
} else if ( $choice == "Earth" ) {
echo "<img src='PlanetImages/Planet3.jpg' />";
} else if ( $choice == "Mars" ) {
echo "<img src='PlanetImages/Planet4.jpg' />";
} else if ( $choice == "Jupiter" ) {
echo "<img src='PlanetImages/Planet5.jpg' />";
} else if ( $choice == "Saturn" ) {
echo "<img src='PlanetImages/Planet6.jpg' />";
} else if ( $choice == "Uranus" ) {
echo "<img src='PlanetImages/Planet7.jpg' />";
} else if ( $choice == "Neptune" ) {
echo "<img src='PlanetImages/Planet8.jpg' />";
} else if ( $choice == "Pluto" ) {
echo "<img src='PlanetImages/Planet9.jpg' />";
}
?>
<hr />
<?php
if( $weight == "" )
echo "<font color='red'>You must enter a weight!</font><br/>"; //fancy statements to let the user know
?>
<?php
if ( $choice == "Earth" ) {
echo "<font color='red'>You must choose a planet other than Earth!</font><br/>"; //fancy statements to let the user know
}
?>
<?php
$planet = "";
$gravity = "";
$field = "";
$planet = array( );
while(! feof($planet_file)) {
$char = fgetc($planet_file);
if($char == " "){
$planet = $field;
$field = "";
} else if($char == "\n"){
$gravity = $field;
$field = "";
} else {
$field .= $char;
}
if( $choice == $planet ) {
echo $gravity*$weight;
} else if ( $choice == "Earth" ) {
echo $gravity = " ";
}
} //end while
fclose( $planet_file );
?>
</body>
</html>