Jump to content

PHP 5 Multi Conditional IF Statement


malkocoglu

Recommended Posts

Hello guys,

 

I have a slight problem with a booking form i made. Booking form is for minicab booking so price calculation is involved. The booking form was working fine with php4 enabled hosting but i have problem with php5 hosting. Script doesnt pass the varible after if conditions are mathced.

 

What happens is, when user selects a pick up address and dropp of address, price should be set in $quotation. The code is below;

 

if ($postcode == "AL1 St. Albans" && $airport == "Heathrow T1" )            { $quotation = 42;}

 

where you see 42 is how much the journey will cost but it doesnt reflect on the final page. I have also tried the following code which works fine;

 

if ($postcode == "AL1 St. Albans" && $airport == "Heathrow T1" )            { echo "42";}

 

Can anyone help me solve this error.

 

Thanks

 

Link to comment
Share on other sites

Instead of using an if, it would be easier to load data into a table, and let the system look for both locations, and pull out the appropriate option if column A, or column B are correct to save on the hundreds of if statements.

Link to comment
Share on other sites

Thanks Jessica and Q695 for replies.

 

Jessica; I have been trying different ways of setting the variable if two conditions are met but there is no luck so far. Could you give me an example how to write it in functions?

 

Q695; I think you mean database tables. I am not really good at database but I am working on it. It may be my future goal and I am hopping to get this issue fixed before i go into database.

Link to comment
Share on other sites

Thanks for the quick reply Jessica, I will look for that. I will try to include the codes here...

 

 

This is the index.php (First part of the form where user selects the type of the journey)

<form action="indexa.php" method="post" name="form1" >
		<table width="99%"  border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="30" align="center" valign="middle"><font size = "4">Journey Type: </font></td>
            </tr>
          <tr>
            <td height="30" align="center" valign="middle">
			    <select name="journey" class="dropdown" id="journey">
                <option value="Select Journey" selected>Select Your Journey</option>
                <option value="Town to Airport"> Post Code to Airport </option>
                <option value="Town to Station"> Post Code to Station </option>
                <option value="Town to City"> Post Code to City </option>
                <option value="Airport to Town"> Airport to Post Code </option>
                <option value="Airport to Airport">Airport to Airport</option>
                <option value="Airport to City"> Airport to City </option>
                <option value="Station to Town"> Station to Post Code </option>
                </select></td>
            </tr>
          <tr>
            <td height="10" align="center" valign="middle"> </td>
          </tr>
          
          <tr>
            <td height="20" align="center" valign="middle"><input type="hidden" name="posted" value="true" /></td>
          </tr>
          <tr>
            <td height="20" align="center" valign="middle">
              <input name="Submit2" type="submit" class="form-button" value="Continue" />
          </td>
            </tr>
        </table>  
        </form>

This is indexa.php where other form is generated according to the selection on index.php

<?php
error_reporting( E_ALL );


if(isset($_POST["posted"]))
    $journey = $_POST["journey"];
    
switch($journey)
{
        case "Airport to Town":
        include("journey-types/a2t.php");
        break;  

        case "Town to Airport":
        include("journey-types/t2a.php");
        break;  

        case "Airport to Airport":
        include("journey-types/a2a.php");
        break;  

        case "Station to Town":
        include("journey-types/s2t.php");
        break; 

        case "Town to Station":
        include("journey-types/t2s.php");
        break; 

        case "Airport to City":
        include("journey-types/a2c.php");
        break; 

        case "Airport to Station":
        include("journey-types/a2s.php");
        break; 

        case "Town to City":
        include("journey-types/t2c.php");
        break; 

        default:
        include("journey-types/error.php");        
        break;
  
}
?>
     

This is the index2.php where the journey price is calculated according to prices.php

<?php 
			
			
			 if ($journey == "Airport to Town" ){ 
			$price = (($quotation * $executive)+ $airportpickup);
			echo "Executive";
			echo "&nbsp";
			echo "£";
			echo number_format($price, 2, '.', ''); }
			

			else {($price = $quotation * $executive);
			echo "Executive";
			echo "&nbsp";
		    echo "£";
			echo number_format($price, 2, '.', ''); }
			?>" 

This is prices.php where the price is calculated according to selection on indexa.php

<?php
//Airports//
if ($postcode == "AL1 St. Albans" && $airport == "Heathrow T1" )            { $quotation = 42;}
if ($postcode == "AL1 St. Albans" && $airport == "Heathrow T2" )            { $quotation = 42;}
if ($postcode == "AL1 St. Albans" && $airport == "Heathrow T3" )            { $quotation = 42;}
if ($postcode == "AL1 St. Albans" && $airport == "Heathrow T4")             { $quotation = 42;}
if ($postcode == "AL1 St. Albans" && $airport == "Heathrow T5")             { $quotation = 42;}
if ($postcode == "AL1 St. Albans" && $airport == "Gatwick North Terminal" ) { $quotation = 77;}
if ($postcode == "AL1 St. Albans" && $airport == "Gatwick South Terminal" ) { $quotation = 77;}
if ($postcode == "AL1 St. Albans" && $airport == "Luton" )                  { $quotation = 32;}
if ($postcode == "AL1 St. Albans" && $airport == "Stansted" )               { $quotation = 60;}
if ($postcode == "AL1 St. Albans" && $airport == "City" )                   { $quotation = 60;}



//Stations//
if ($postcode == "AL1 St. Albans" && $airport == "Victoria Station")        { $quotation = 57;}
if ($postcode == "AL1 St. Albans" && $airport == "St Pancras Station")      { $quotation = 55;}
if ($postcode == "AL1 St. Albans" && $airport == "Kings Cross Station" )    { $quotation = 55;}
if ($postcode == "AL1 St. Albans" && $airport == "Waterloo Station" )       { $quotation = 57;}
if ($postcode == "AL1 St. Albans" && $airport == "Euston Station" )         { $quotation = 55;}
if ($postcode == "AL1 St. Albans" && $airport == "Liverpool St. Station" )  { $quotation = 55;}
//Towns//
if ($postcode == "AL1 St. Albans" && $airport == "Dover")                   { $quotation = 150;}
if ($postcode == "AL1 St. Albans" && $airport == "Southampton")             { $quotation = 135;}
if ($postcode == "AL1 St. Albans" && $airport == "Harwich" )                { $quotation = 120;}
if ($postcode == "AL1 St. Albans" && $airport == "Portsmouth" )             { $quotation = 135;}
if ($postcode == "AL1 St. Albans" && $airport == "Birmingham" )             { $quotation = 140;}
if ($postcode == "AL1 St. Albans" && $airport == "Manchester" )             { $quotation = 215;}
if ($postcode == "AL1 St. Albans" && $airport == "Glasgow" )                { $quotation = 425;}
if ($postcode == "AL1 St. Albans" && $airport == "Cambridge" )              { $quotation = 78;}
if ($postcode == "AL1 St. Albans" && $airport == "Brighton" )               { $quotation = 130;}
if ($postcode == "AL1 St. Albans" && $airport == "Oxford" )                 { $quotation = 89;}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>

I hope this information is usefull...

Link to comment
Share on other sites

now might be a good time to learn how to use databases. all the code you have just posted is hard coded with source/destination information, decisions, and amounts. using a database based design, all of that could become just a source selection and a destination selection.

 


 

some ways that the code could be reduced by using a database based solution -

 

1) if you know what the source and destination are, you don't need to have a form to input the type of journey. the stored data for any location would tell you if when it is used as a source location what the pickup amount is (some amount for airports, zero for everything else.)

 

2) indexa.php really isn't needed at all.

 

3) the if/else logic in index2.php isn't needed. if $airportpickup is a zero, both of those calculations are identical and you will have the correct pickup value for any source location out of the database table storing the information about each location.

 

4) all (i'm sure you have those lines of code repeated for each possible $postcode) the prices.php code will disappear and become data in a source/destination cost database table.

Link to comment
Share on other sites

A bit busy here, so I'll make it quick: I'm fairly certain that the main problem is register_globals. Or rather, the fact that they were turned off in PHP 5 due to the security risk it presents.

Make sure that all of your code uses the $_POST superglobal, when trying to access data which has been posted via a form.

Link to comment
Share on other sites

  • 2 months later...

now might be a good time to learn how to use databases. all the code you have just posted is hard coded with source/destination information, decisions, and amounts. using a database based design, all of that could become just a source selection and a destination selection.

 


 

some ways that the code could be reduced by using a database based solution -

 

1) if you know what the source and destination are, you don't need to have a form to input the type of journey. the stored data for any location would tell you if when it is used as a source location what the pickup amount is (some amount for airports, zero for everything else.)

 

2) indexa.php really isn't needed at all.

 

3) the if/else logic in index2.php isn't needed. if $airportpickup is a zero, both of those calculations are identical and you will have the correct pickup value for any source location out of the database table storing the information about each location.

 

4) all (i'm sure you have those lines of code repeated for each possible $postcode) the prices.php code will disappear and become data in a source/destination cost database table.

 

Hello everyone. As it states above, I am planning the load all the data to the database and get the calculation done. Could anyone tell me the logic of doing this and give an example if possible.

 

Thanks

Link to comment
Share on other sites

you would basically need two database tables - a location table (used for picking sources and destinations and with things like a pickup amount) and an quotation table, that holds the quotation price for the valid combinations of sources/destinations.

 

i would start with -

 

location table - id, name, type_id, pickup_amt

  • id  - the auto increment id for any location, used when referencing any location
  • name - the display name for the location, "AL1 St. Albans", "Heathrow T1", ...
  • type_id - the type of location (used for categorizing and picking/limiting the selection of locations), Town, City, Airport, Station, ... this should actually be a type_id with a separate table holding the actual text of the types.
  • pickup_amt - any amount to add when the location is the source (you could also have a dropoff_amt column to add when the location is the destination.) the amount is zero for any location that doesn't have any extra cost for a pickup at that location.

quotation table - location_id1, location_id2, amount

 

location_id1 and location_id2 - the source and destination ids that are defined in the location table.

amount - the quoted amount for the trip between the source and destination.

 

to pick a source and destination, you would simply display (all) the possible locations out of the location table. to limit and narrow down selections, you would have a way of selecting the type (checkboxes) or of searching for specific information (i.e. ajax lookup using the characters entered.) the form would just submit the ids for the source and destination.

 

once the source and destination have been submitted, you would query for the location information and quotation amount using those two ids. this would give you any pickup_amt and the quotation for that trip.

Edited by mac_gyver
Link to comment
Share on other sites

example of getting a trip pickup amount and quotation amount from the database tables, given a source and destination id -

$source_id = 3; // simulate two values that would be from a form submission
$dest_id = 2;

$source_id = (int)$source_id;
$dest_id = (int)$dest_id;
if($source_id == $dest_id){
    echo "Sorry, the source and destination cannot be the same.";
} else {

    $query = "SELECT loc1.pickup_amt, q.amount FROM locations loc1 join locations loc2 ON loc1.id = $source_id AND loc2.id = $dest_id
    join quotation q ON (loc1.id = q.location1_id AND loc2.id = q.location2_id) OR (loc2.id = q.location1_id AND loc1.id = q.location2_id)";
    $result = $mysqli->query($query) or die("Query failed: $query<br>Error: {$mysqli->error}");
    if($result->num_rows < 1){
        // either a submitted id was invalid (no location) or there was no matching row in the quotation table
        echo "Sorry, your search did not match any trip.";
    } else {
        $row = $result->fetch_assoc();
        print_r($row); // look at what values the query did return
    }
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.