距离成本计算器

时间:2011-11-20 10:17:13

标签: php

您好我正在寻找像机场这样的远程费用计算器,例如出租车公司:

从x到xx它将花费......

这是我在其他人的帮助下所做的:http://pastebin.com/0pSF7VsA 但是我无法弄清楚如何让它工作,例如当用户从表单中选择数学时进行数学运算。

到目前为止,这是我的表单:http://pastebin.com/sq14eYMQ

感谢

1 个答案:

答案 0 :(得分:1)

这是一个清理和后发布计算的一个例子。希望它有所帮助。

<?php
error_reporting(E_ALL);

/* places array, multi dimensional could also be used but noticed prices are the same  */
$data = array("Algeciras"=>10,"Almuñecar"=>20,"Alora"=>30,"Antequera"=>10,"Benahavis"=>15,
"Benalmadena costa"=>20,"Benalmadena pueblo"=>25,"Calahonda"=>30,"Cartama"=>40,
"Coin"=>12,"Estepona"=>15,"El Faro"=>20,"Frigiliana"=>16,"Fuengirola"=>30,"Gibraltar frontier - La Linea"=>12,
"Granada"=>30,"La Cala de Mijas"=>10,"La Cala Golf Resort"=>20,"Malaga"=>5,"Manilva"=>12,"Marbella"=>20,"Mijas Costa"=>7,
"Mijas Pueblo"=>20,"Nerja"=>10,"Puerto Banus"=>15,"Puerto de la Duquesa"=>10,"Rincon de la victoria"=>23,
"Ronda"=>20,"San Pedro de Alcantara"=>10,"San Roque"=>20,"Sierra Nevada"=>12,"Sotogrande"=>19,"Tarifa"=>20,
"Torre del Mar"=>30,"Torremolinos"=>10,"Torremuelle"=>14,"Torrenueva"=>18,"Torrox Costa"=>11);


/* number of people array */
$car=array('1'=>0,'2'=>50,'3'=>70,'4'=>90,'5+'=>110);


$now = date("H:i");
$cutoff = "18:56";

function rateOffset($now, $cutoff){
    if ($now < strtotime($cutoff)){
        // if less than 6:56pm add 20.00 20.00 euros to price
        return 20.00;
    } else {
        // if more than 6:56pm add only 6 euros to price
        return 6.00;
    }
}

//This is a basic example of how to check if the form was posted
if(isset($_POST['name']) && isset($_POST['to'])){

    if(isset($_POST['from']) && $_POST['from']=='Gibraltar'){
        /* places array to overwrite malaga prices   */
        $data = array("Algeciras"=>10,"Almuñecar"=>20,"Alora"=>30,"Antequera"=>10,"Benahavis"=>15,
        "Benalmadena costa"=>20,"Benalmadena pueblo"=>25,"Calahonda"=>30,"Cartama"=>40,
        "Coin"=>12,"Estepona"=>15,"El Faro"=>20,"Frigiliana"=>16,"Fuengirola"=>30,"Gibraltar frontier - La Linea"=>12,
        "Granada"=>30,"La Cala de Mijas"=>10,"La Cala Golf Resort"=>20,"Malaga"=>5,"Manilva"=>12,"Marbella"=>20,"Mijas Costa"=>7,
        "Mijas Pueblo"=>20,"Nerja"=>10,"Puerto Banus"=>15,"Puerto de la Duquesa"=>10,"Rincon de la victoria"=>23,
        "Ronda"=>20,"San Pedro de Alcantara"=>10,"San Roque"=>20,"Sierra Nevada"=>12,"Sotogrande"=>19,"Tarifa"=>20,
        "Torre del Mar"=>30,"Torremolinos"=>10,"Torremuelle"=>14,"Torrenueva"=>18,"Torrox Costa"=>11);
    }

    $rateCalc = rateOffset($now, $cutoff);

    echo $now." = $rateCalc<br />";
    echo "Price to {$_POST['to']} from {$_POST['from']}: ".number_format($data[$_POST['to']],2)."<br />";
    echo "Number of people price: ".number_format($car[$_POST['people']],2)."<br />";
    echo "Time rate calculator: ".$rateCalc."<br /><hr />";

    $total = number_format($data[$_POST['to']],2) + number_format($car[$_POST['people']],2) + $rateCalc." in euros<br /><br />";
    echo "Total: ".$total;

}else{
    echo '<form method="POST" action="">
                    <label>Name:</label>
                    <input type="text" value="name" name="name" />

                    <label>From:</label>
                    <select name="from">
                        <option selected="selected">Malaga</option>
                        <option>Gibraltar</option>
                    </select>
                    <div class="clr"></div>

                    <label>To:</label>
                    <select name="to">
             ';
    foreach ($data as $place => $price){
        echo "<option>{$place}</option>\n";
    }
    echo '</select>
                    <div class="clr"></div>

                    <label>Date:</label>
                    <input type="text" value="dd/mm/yyyy" id="date" name="date" />

                    <label>Number of people:</label>
                    <select id="people" name="people">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5+</option>
                    </select>
                    <div class="clr"></div>

                    <div id="return">
                        <label>Is this a return<br />journey?</label><div class="clr"></div>
                        <div id="radio-buttons"><input type="radio" name="return" value="Yes" class="radio" />Yes<br />
                        <input type="radio" name="return" value="No" class="radio" />No</div>
                    </div>
                    <div class="clr"></div>

                    <input type="submit" name="submit" class="fauxButton" />
                </form>';
}
?>