THis code selects the closest quantity...
if you have the two closest quantity, you could finish this project ?
<?
if($_POST)
{
include("connect.php");
$q=$_POST["q"];
$sql=" SELECT (SELECT q FROM `quantity` WHERE q >=$q ORDER BY q ASC LIMIT 1) AS `upper`,
(SELECT q FROM `quantity` WHERE q <=$q ORDER BY q DESC LIMIT 1) AS `lowest`
FROM quantity LIMIT 1";
print $sql;
$rs=mysql_query($sql);
$rows=mysql_fetch_assoc($rs);
print "Selected quantity: ".$_POST["q"]. " --> upper value: ".$rows['upper']." AND the closest to down is:".$rows['lowest']. "<br>";
if ($rows['lowest'] AND $rows['upper'])
{
$lowestminus=$q-$rows['lowest'];
$upperminus=$rows['upper']-$q;
if ($lowestminus>=$upperminus)
print $rows['upper']." is the quantity";
else
print $rows['lowest']." is the selected quantity";
}
if($rows['upper']==0)
print "the quantity is :" . $rows['lowest'];
if($rows['lowest']==0)
print "the quantity is :" . $rows['upper'];
}//If that was a post...End
?>
<form name="form1" method="post" action="?">
q<input name="q" type="text">
s<input name="s" type="text">
<input type="submit" name="Submit" value="Submit">
</form>
thiis the table structure,
CREATE TABLE IF NOT EXISTS quantity (
q int(11) NOT NULL,
s int(11) NOT NULL,
p int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
--table data: : quantity
INSERT INTO quantity (q, s, p) VALUES
(10, 1, 50),
(20, 1, 80),
(30, 1, 90),
(10, 3, 60),
(10, 10, 70),
(20, 3, 100),
(20, 10, 120),
(30, 3, 130),
(30, 10, 150);