The idea of my code is to make a user choose flight then I开发者_Go百科 decrease(-1) the flight seats of that flight he chooses.
When the user change his flight I decrease the new flight(-1) seats and increase the previous flight seats since the user changed his flight.But I don't know how to hold the previous flight id, actually I tried but I failed,anyway..
How do I store the previous flight id?
Because the variable gets reset every time the page gets reloaded by clicking the submit button (this what happened, because I have to initiate flight id holder variable every time I open the page).Is holding something like that even possible?...
Here what I did to try and use sessions:
$flight_id = '';//empty
$_SESSION['prev_flight_id'] = $flight_id; // I just initiate the session now
echo $_SESSION['prev_flight_id']; // to make sure there's something here
// result is nothing(empty) important note: I set the value of $flight_id
// later in the script
I also tried this :
session_start();
// bunch of code (I didn't create the $flight_id yet) then :
// submiting the old flight id in the session (I GET THE ERROR HERE)
$_SESSION['prev_flight_id'] = $flight_id;
echo $_SESSION['prev_flight_id'];
// get flight_id from text area
$flight_id = $_POST['flight_id_res'];
// result : Undefined variable: flight_id...
PHP
Make use of Sessions.
- http://www.php.net/manual/en/session.examples.basic.php
MySQL
Or you could use MySQL SET @@global.your_var
to globally store and SELECT
to retrieve your flight id.
- http://dev.mysql.com/doc/refman/5.1/en/set-option.html
My dear you can store previous flight id in Session after you use that id then unset the value in you session. Simple
U can do 1 thing, add a variable named last_flight id in the parameter_list(send to the server) like Session while submitting the form, and there in the server side it will check whether there isn't any value for the last_flight, if it is , it will increase the seatcount and do the rest as before
Assuming that you use user table and flightplan table in this case then you need to make relation table between user table and flightplan table. i.e. userflightplan table that store the user and flightplan number. By using this table you can use trigger on update to achieve seat increase/decrease in flightplan table. By accessing the OLD.flightplan to increase and NEW.flightplan to decrease seat in the related flightplan in flightplan table.
You will need to keep track of the user_id, and do some sort of session tracking.
The rest of your question I would solve in MySQL.
Is holding something like that even possible?...
Of course, as long as you have a user_id, you can get all the info from the database, that's what it's for.
$user_id = mysql_real_escape_string($_SESSION['user_id']);
$sql = "SELECT b.flight_id, other fields...
FROM booking b
WHERE b.customer_id = '$user_id'
AND b.not_yet_closed = 'TRUE';
If you have multiple flights open, you'll have to present a list of flights to change, or you'll have to store the current flight_id in a $_SESSION
variable.
Here's how to change the number of seats available upon changing the flight_id of a booking
In order to track the available seats on a flight you need to have a booking table and a flight table.
booking
--------
id unsigned integer auto_increment primary key,
customer_id integer foreign key references customer(id),
flight_id foreign key references flight(id),
not_yet_closed BOOLEAN NOT NULL DEFAULT TRUE,
other fields....
flight
-------
id unsigned integer auto_increment primary key
available_seats
other fields ....
For every change in the booking
table you update the available seats in a flight using a trigger:
DELIMITER $$
CREATE TRIGGER bi_booking_each BEFORE INSERT ON booking FOR EACH ROW
BEGIN
DECLARE SeatsAvailable INTEGER;
INSERT SUM(f.available_seats) INTO SeatsAvailable
FROM flight f
WHERE flight.id = NEW.flight_id;
IF SeatsAvailable = 0 THEN
//Signal throws an error (in MySQL 5.5+)
SIGNAL SQLSTATE 45000
SET MESSAGE_TEXT = CONCAT('Flight ',NEW.flight_id,' is full');
//SELECT `Flight_is_full` FROM Error (use this in MySQL 5.1 and before to throw error)
END IF;
END $$
CREATE TRIGGER ai_booking_each AFTER INSERT ON booking FOR EACH ROW
BEGIN
UPDATE flight
SET available_seats = available_seats - 1
WHERE flight.id = NEW.flight_id;
END $$
CREATE TRIGGER bu_booking_each BEFORE UPDATE ON booking FOR EACH ROW
BEGIN
DECLARE SeatsAvailable INTEGER;
INSERT SUM(f.available_seats) INTO SeatsAvailable
FROM flight f
WHERE flight.id = NEW.flight_id;
IF SeatsAvailable = 0 THEN
//Signal throws an error (in MySQL 5.5+)
SIGNAL SQLSTATE 45000
SET MESSAGE_TEXT = CONCAT('Flight ',NEW.flight_id,' is full');
//SELECT `Flight_is_full` FROM Error (use this in MySQL 5.1 and before to throw error)
END IF;
END $$
CREATE TRIGGER au_booking_each AFTER UPDATE ON booking FOR EACH ROW
BEGIN
UPDATE flight
SET available_seats = available_seats + 1
WHERE flight.id = OLD.flight_id;
UPDATE flight
SET available_seats = available_seats - 1
WHERE flight.id = NEW.flight_id;
END $$
DELIMITER ;
Simple logic could be: 1. User books one flight so You decrease number of free seets of that flight and here is the point when You can save the ID of this flight into the PHP session. 2. When the user changes his mind and books another flight while knowing the ID of the last booked flight You can easily increese free seats by that ID, decrease free seats of new flight and store that new flight ID instead of the last one...
Simple algorythm:
<?php
session_start();
$_SESSION['booked_flight_id'] = 0;
// ...
// user wants to book a flight
$_SESSION['booked_flight_id'] = book_flight($flight_id); // lets assume that function book_flight($flight_id) decreases free seats and returns the flight ID
// ...
// user changes his mind
unbook_flight($_SESSION['booked_flight_id']); // assuming that method unbook_flight($flight_id) will increase free seats
$_SESSION['booked_flight_id'] = book_flight($new_flight_id);
Was this helpful?
精彩评论