I've been learning PHP+MySQL with much help from the generous coders on Stackoverlflow and am nearing the end of my first functional database but am stuck.
I'm trying to set up a database wherein a user selects a room type for each person or family (upto 12 people total). The form is setup such that each person is assigned a room category (because the product is sold per person instead of per room). Available room types are SGL, DBL (max 2 persons), TPL (max 3 persons), and QUD (max 4 persons).
I have a table with entered prices for each room type called tourprices with these prices varying based on the date of tour start in a separate table called tourstart.
Basically, I want to write code that will look at the room type selected for each person, look up the associated room price, count to the occupany per room type, multitply the price x the room type quantity (displaying the per person rate next to each person) and summarize the total cost for all rooms combined.
For example: tourroom prices(per person)
user is wanting to book - 1 SGL, 2 DBL, + 1 TPL for 8 people for the 1May2012 tour.
So, resulting confirmation display should resemble this (along with the usual HTML: 'Thank you, you're the best customer..blah, blah' which is already good to go):
Total price = 1x$300 + 4x$150 + 3x$100 + 0x$75 = $1200
I've gotten the html forms setup for the system as well as the insert query sections and the data is posting to the database. For the calculation query (for the booking confirmation page), I've gotten as far as:
$selectedDate = $tourstart
$query = "SELECT `sgl`, `dbl`, `tpl`, `qud` FROM tourprices WHERE `tour_id` = $tour AND `tourstart` = $tourstart";
$query .= "COUNT `sgl` as SglRmCount FROM `clients`";
$query .= "COUNT `dbl` as DblRmCount FROM `clients`";
$query .= "COUNT `tpl` as TplRmCount FROM `clients`";
$query .= "COUNT `qud` as QudRmCount FROM `clients`";
$result = $pdo->query($query);
?>
thank you in advance.
The query you have created will not work. Count is not being used correctly. Count is meant to return a number out of the select field as followed:
SELECT COUNT(`sgl`) FROM your_table WHERE your_value = 0;
Which will return a single number that is the summation the number of records returned. What follows is the query you have created:
SELECT `sgl`, `dbl`, `tpl`, `qud` FROM tourprices WHERE `tour_id` = INPUTED_VALUE_FROM_PHP AND `tourstart` = YOUR_tourstart_VALUE COUNT `sgl` as SglRmCount FROM `clients` COUNT `dbl` as DblRmCount FROM `clients` COUNT `tpl` as TplRmCount FROM `clients`;
This query will not work because first count is not used correctly and secondly because you can only call one FROM is a given query. It looks like you wish to return multiple unrelated values. If that is the case then you can just create multiple SELECT queries for everything you need.
Also math in line in a query is totally possible, you can do just about anything inline. Here is a resource for some of the basic math that you can do: http://www.keithjbrown.co.uk/vworks/mysql/mysql_p9.php