Hey people. I need some help on a car hire project am working on. Basically i need to code a simple script in php where there is a drop-down list of cars e.g. Lexus, toyota, Mazda, Ford e.t.c
Each vehicle type has a different price associated with it. There is also an input box for the number of days the client will rent the vehicle. So when a user selects the car type and types the number of days the script (PHP_SELF) will calculate the total price quote for the client.
Additionally, i wanted to include an option of converting the currency type from one to another.
Any feedback will be appreciated. Thanks.
<?php
function makeDropList($name,$list,$selected="") {
// $name select name
// $list array of value, label pair
// $selected selected value
while(list($value,$label) = each($list)) {
$options .= '<option value="'.$value.'">'.$label.'</option>';
}
$dropList = '<select name="'.$name.'">'.$options.'</select>'."\n";
$dropList = ereg_replace("value=\"$selected\"","value=\"$selected\" selected",$dropList);
return $dropList;
} // end function makeDropList($name,$list,$selected="")
$cars = array(
"" => "Select a Car",
"500" => "Toyota",
"100" => "Mazda",
"200" => "Hyundai",
"600" => "Lexus",
"400" => "Ford",
"700" => "Mercedes",
"1000" => "BMW"
);
$currency = array(
"" => "Select a Currency",
"70" => "USD",
"90" => "Euro",
"120" => "Sterling Pound",
"1" => "KES"
);
$list = makeDropList('car',$cars,$cars);
$list2 = makeDropList('currency',$currency,$currency);
$total = $car * $days;
if(!is_numeric($days) or $days <= 0)
{ $error = "<p><font color=red>Rent days</font> has to be numeric and greater than zero.</p>";}
else {
if(((empty($days))||(empty($cars))||(empty($currency))))
{
echo 'Sorry, you forgot to fill out some required fields. ';
echo '<a href="javascript:history.back(1)">Try again</a>';
} else {
if(!isset($error) && isset($_GET['action']))
{
$total = $car * $days;
}
else
{
$total = '';
}
}
}
$form=<<<FORM
<form method="post" action="$PHP_SELF">
$list<br /><br />
$list2<br /><br />
<input type="text" size="32" name="$days"><br /><br />
$total<br /><br />
<input type="submit" value="Calculate">
</form>
FORM;
print $form;
?>