Forum Moderators: coopster

Message Too Old, No Replies

Searching a database for something that doesn't exist!

Then find the next size up. Some pointers required.

         

Spook

11:27 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



Hi Guys.

I am still quite new to php but could do with a "nudge" in the right direction. Not even sure if I can describe it clearly but here goes!

I have a list of widgets/prices in a MySql database. The widgets are available in any size [height x width] within a given limit [say 1m square]. There are a range of "standard" sizes and corresponding prices. Non standard prices can be calculated by taking the next standard size above and adding 20%.

To give you some idea, there are potentially 1.4 million possible size variations, the prices of which can all be calculated from 136 "standard" sizes/prices.

So, standard sizes/prices may look like this:

715 x 313 = $16
715 x 395 = $17.23
895 x 295 = $21.60
895 x 395 = $23.61

Height, width and price are stored in separate fields in the table.

My question is this: If a price is required for a widget 895 x 313, [no standard price] the price would be $28.33 [ie next size up plus 20%]. How do I search the database and when the size I require does not exist, find the next size above which does have a price?

I hope this makes sense! I just can't get my head around the principles involved.

Sorry if this is a little long winded.

Thanks in advance

Spook

ergophobe

6:37 am on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The way my brian is working today, I probably should shut up, but since nobody else is chiming in...

SELECT price FROM table1 WHERE height='$height' AND width='$width'

When you get your result, do a mysql_num_rows. If the result is 0, you have to try again.

SELECT price from table1 WHERE height>'height' OR width>'width' ORDER BY price

This should give you the lowest price for the next size up. Add 20% and you're done.

Tom

Spook

7:39 am on Mar 24, 2004 (gmt 0)

10+ Year Member



Many thanks for your reply Tom.

mysql_num_rows is a new one on me but I have located it in the php manual and will "play around with it" today.

I will let you know how I get on!

Thanks again.

Spook

Nova Reticulis

4:13 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Actually, everything and more of this can be done with SQL.

When looking for texts, you get things like

name LIKE '%widget%'
. When looking for dimensions, you could write something like this:


SELECT * FROM WIDGETS
WHERE
name ILIKE '%widget%'
AND
size < 20 AND size > 10
ORDER BY size DESC;

Then just fetch the topmost row from the result to get your most likely result. There are more powerful tools to do that, just read your database's manual carefully.

ergophobe

5:11 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Nova's query won't give the answer the original poster was looking for. He has to check first for an exact match. In that case, the price is the list price. If it is not an exact match, the price is bumped up 20% for a custom job.

The following will give you the nearest, next higher price:

SELECT price FROM table1 WHERE height >= '$height' AND width >= '$width' ORDER BY price LIMIT 1

It will not, however, tell you whether or not it was an exact match and whether or not the 20% surcharge applies.

Nova Reticulis

5:21 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



ergophobe, it will, actually, if you properly order and group your data rows. Then again, with SQL possibilities are endless, down to the point where you can write a function that will produce a quantifier of likelihood by a number of given parameters.

coopster

5:30 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Nova,
I think what ergophobe means here is that Spook won't know the lower and higher ends, only the size itself at the time of the query. Therefore, it would be impossible to use a "<20 and >10" type query.

ergophobe, I think you meant

ORDER BY height, width
:

$sql = "SELECT height, width, price FROM table1 WHERE height >= '$height' AND width >= '$width' ORDER BY height, width LIMIT 1";

Also, you could use the single query to the database and compare values upon return to determine price:

$row = mysql_fetch_assoc(mysql_query($sql)); 
if ($row['height'] == $height AND $row['width'] == $width) {
$price = $row['price'];
} else {
$price = $row['price'] * 1.20; // 20% markup
}

ergophobe

5:55 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




ergophobe, I think you meant ORDER BY height, width:

Actually, I meant price. I think both should ultimately give the same result.

715 x 313 = $16
715 x 395 = $17.23
895 x 295 = $21.60
895 x 395 = $23.61

if the user requests 736 x 250, then the result set is

895 x 295 = $21.60
895 x 395 = $23.61

Regardless of whether you order by price or dimensions, it's the same order. The question is whether Spook wants to bump up to the next closest size or the next closest price. Based on his example, this will always be the same. Say there are some exceptions,though, he would presumably want to give his customer the best price rather than cut that provides the least waste.


Also, you could use the single query to the database and compare values upon return to determine price:

That's probably the best solution. One query and then check it.

coopster

6:05 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think both should ultimately give the same result.

True, assuming the price increases with dimension.

Spook said: >>...and when the size I require does not exist, find the next size above which does have a price?

That's why I figured sort by size. And now that I read the statement again, I see something else in there. "...which does have a price...". We may need to add that to the query?

$sql = "SELECT height, width, price FROM table1 WHERE height >= '$height' AND width >= '$width' AND price > 0 ORDER BY height, width LIMIT 1";

Spook

6:30 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Just to let you all know I am still here!

Like I said, I'm relatively new to PHP, but this stuff seems second nature to you guy's.

I am trying to work through [and understand] what you are all saying. It may take a little longer, but don't think I'm not grateful for your advice.

Thanks.

Spook

slade7

7:31 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Coopster -

technically...

$price = $row['price'] * 1.20; //is price + 20%

$price = $row['price'] / .80; // is a 20% markup

i.e. 20% markup usually means that if you sell the item you make 20% profit. So you must arrive at a figure 20% of which would be the difference between it and the cost.

ergophobe

8:07 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Spook said: >>...and when the size I require does not exist, find the next size above which does have a price?

That's why I figured sort by size. And now that I read the statement again, I see something else in there. "...which does have a price...". We may need to add that to the query?

Fair enough. I suspect it makes no difference, because price probably always increases with dimension.

My point is that if there are exceptions, you probably would not want the next size up as he said, you would want the cheapest alternative with those minimum dimensions, especially in a competitive bidding environment.

One other thing a little OT from the original quesiton. Will users always enter requests in the form "larger x smaller"? It seems like if they are just selecting dimensions from dropdowns or entering them in input boxes, you'll need to put it in that form:

if ($_POST['dim1'] > $_POST['dim2']) {
$height = $_POST['dim1'];
$width = $_POST['dim2'];
} else {
$height = $_POST['dim2'];
$width = $_POST['dim1'];
}

coopster

8:24 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



slade7 said:

Coopster -
technically...

true. true. I'll try to watch my comments from here on ;)

Spook

11:48 pm on Mar 27, 2004 (gmt 0)

10+ Year Member



Guys

Its taken me a while but having taken on board all of the above comments I now have a query [well two actually] which seems to work OK.

Maybe not the prettiest piece of code you have seen, but for anyone else in the same part of the learning curve as me, this is what I have:

{
$query = "SELECT * FROM product, product_price
WHERE product.product_height ='$height'
AND product.product_width ='$width'
AND product_price > 0
AND product_price.shopper_group_id= '8'
AND product.product_id = product_price.product_id
ORDER BY product_height, product_width
LIMIT 1";

$result = mysql_query($query)
or die("could not execute initial query.");

$base_price = mysql_fetch_array($result);

$message="<br> Exact Match";

}
if ($base_price == "0")
{

$query = "SELECT * FROM product, product_price
WHERE product.product_height >='$height'
AND product.product_width >='$width'
AND product_price > 0
AND product_price.shopper_group_id= '8'
AND product.product_id = product_price.product_id
ORDER BY product_height, product_width
LIMIT 1";

$result = mysql_query($query)
or die("could not execute query.");

$base_price = mysql_fetch_array($result);

$message="<br> Not Exact Match";
}

print_r($base_price[product_price]);

echo $message;

Many thanks for all of your advice.

Spook

ergophobe

6:30 pm on Mar 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You took my first solution. That's flattering, but I think Coopster's solution in message 7 really is better. If you have a lot of traffic, one query and then evaluating with PHP will be more more efficient than two queries.

Have fun!

Spook

9:11 pm on Mar 28, 2004 (gmt 0)

10+ Year Member



Thanks ergophobe. To be honest it was the first one I got working correctly, but I will re visit coopsters solution tomorrow.

Spook