Reputation: 69
First of all, I'm new to PHP programming.
For the development of a webshop (windows blinds), I'm trying to create a price calculation form where the customer has to specify the height and width of the window. The form has to pick the right price out of the database.
This is a picture of how the example database looks like:
When the customer picks a height (1st column) and width (1st row), the script must look for the price where the two intersect (in this example it's 90).
Unfortunately I cannot find any tutorials, references or anything on this subject.
Can anyone please help me out by providing me links to tutorials, codes, or anything helpful?
Thanks in advance!
Upvotes: 3
Views: 110
Reputation: 4616
The query should look something like this:
$width = "20";
$height = 30;
query("SELECT `" . quote($width) . "` FROM table WHERE Hoogte = " . quote($height));
Where query
and quote
are your mysql methods that you use to query the database and escape data.
If Hoogte
is a string you have to alter the query like this:
query("SELECT `" . quote($width) . "` FROM table WHERE Hoogte = '" . quote($height) . "'");
EDIT
This query will return what you need, but you definitely should take your time optimizing your table. Have a look at normalization.
A possible table could be the following:
price
-------
id
value
height
width
And your query would then look like this:
query("SELECT value FROM price WHERE height = " . quote($height) . " AND width = " . quote($width));
With this you have no problem adding new height and width values without the need to alter you table!
Upvotes: 7
Reputation: 595
Well, I guess you might have two separate tables for this reasons.
But at the moment I may suggest the following:
Firstly, let's get the desired size. It is meant that the first size in the table is '10'. So, we are searching in the column '10' for the value '30':
$q = mysql_query("SELECT * FROM tbl_name WHERE 10 = '30';");
Once done, we need to get the result from that row. We do not need number from any other column rather than '60', right? So, we are trying to get a result from the column '60', like this:
$res = mysql_fetch_array($q);
$resultForColumn = $res['60'];
I guess the user might choose something other then 60, so you might change it anytime:
$i = $_POST['UserChosedSize']; // Do not forget to sanitize it, if you have something like this;
$resultForColumn = $res[$i];
Hope this helps.
UPD: Probably, Mr. Tobias Kun wrote it right and his example is much faster and easier.
Upvotes: -1