I am working on a php script to calculate the weight of an object on a different planet. I have a text file to incorporate in my script that list the planets and their standard gravity. The person visits the page and selects a planet then types in a weight and an output should occur of what the weight is on the planet they selected. Also a picture of the planet must appear of the planet the user selects. The format of the .txt file is
Mercury x.xx
Venus x.xx
Mars x.xx
This is what I have so far for my script but I don’t know where to go from here.
[php]
<title>Planet Weight Calculator</title>
<?php
$planets=array("Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto");
$pick = "Mercury";
if( isset( $_POST['pick'] ) ) {
$pick = $_POST['pick'];
}
?>
<?php
$objectweight = "";
if( ! empty( $_POST['objectweight'] ) ) {
$objectweight = $_POST['objectweight'];
}
?>
</head>
<body>
<h1>Planet Weight Calculator</h1>
<form action="planets.php" method="post">
<label for="pick">Select a Planet:</label><br/>
<select name="pick" id="pick">
<?php
foreach( $planets as $planets ) {
if( $pick == $planets ) {
echo "<option selected>$planets</option>";
} else {
echo "<option>$planets</option>";
}
}
?>
</select><br/>
<label for="objectweight">Enter object weight: </label><br/>
<input type="text" name="objectweight" id="objectweight" value="<?php echo $objectweight ?>"/>
<input type="submit" value="Calculate Weight"/><br/>
<?php
if( $pick == "Mercury" ) {
echo "<img src= 'PlanetImages/Planet1.jpg' />";
} else if ( $pick == "Venus" ) {
echo "<img src='PlanetImages/Planet2.jpg' />";
} else if ( $pick == "Earth" ) {
echo "<img src='PlanetImages/Planet3.jpg' />";
} else if ( $pick == "Mars" ) {
echo "<img src='PlanetImages/Planet4.jpg' />";
} else if ( $pick == "Jupiter" ) {
echo "<img src='PlanetImages/Planet5.jpg' />";
} else if ( $pick == "Saturn" ) {
echo "<img src='PlanetImages/Planet6.jpg' />";
} else if ( $pick == "Uranus" ) {
echo "<img src='PlanetImages/Planet7.jpg' />";
} else if ( $pick == "Neptune" ) {
echo "<img src='PlanetImages/Planet8.jpg' />";
} else if ( $pick == "Pluto" ) {
echo "<img src='PlanetImages/Planet9.jpg' />";
}
?>
</form>
<?php
if( $objectweight == "" ){
echo "<b>Please enter an object weight<b/><br/>";
}
?>
<?php
if ( $pick == "Earth" ) {
echo "<b>Planet Earth not valid, select another planet<b/><br/>";
}
?>
<?php
if ( $pick == "Pluto" ) {
echo "<b> No calculation can be made from planet Pluto, select another planet<b/><br/>";
}
?>
</body>
</html>[/php]
Instead of typing the name of each planet throughout the array. I would like to just have the array gather the planet names from the .txt file. Also I need to have each gravity of each planet accessed in order to multiply by the weight the user inputs in the text field box. Any help would be greatly appreciated.