【发布时间】:2014-05-05 04:40:12
【问题描述】:
我目前正忙于根据数据库中的价格开发价格计算器。
客户指定所需的宽度和高度,计算器应在数据库中查找相应的产品及其价格。
宽度 = 10 & 高度 = 10:价格 = 20
宽度 = 20 & 高度 = 10:价格 = 30
我现在面临的问题:
但是如果宽度在 10 到 19 的范围内,他应该选择宽度为 10 的价格。如果宽度在 20 到 29 的范围内,他应该选择宽度为 20 的价格。等等等等。
当前 HTML 输入表单:
<form action="Database-Output.php" method="post">
<table width="470" border="0">
<tr>
<td>Geef hier de door u gewenste hoogte in:</td>
<td>
<input type="number" name="height" width="100" placeholder="Hoogte">
</td>
</tr>
<tr>
<td>Geef hier de door u gewenste breedte in:</td>
<td>
<input type="number" name="width" width="100" placeholder="Breedte">
</td>
</tr>
</table>
<br />
<br />
<input type="submit" value="Bereken prijs">
</form>
当前 PHP 表单(Database-Output.php)
<?PHP
$user_name = "root";
$password = "root";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SELECT * FROM price WHERE height = " . $_POST["height"] . " AND width = " . $_POST["width"] . "";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
print $db_field['value'] . "<BR>";
}
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
数据库包含以下值:
id
value (this is the price)
height
width
打印价格表的示例
http://www.entry-vouwgordijnen.nl/price_table.php
有没有什么方法可以以一种简单易维护的方式做到这一点?
谢谢!
回答
我用下面的代码解决了这个问题:
$width = $_POST['width'];
// Vaste variabele voor breedte
$height = $_POST['height'];
// Vaste variabele voor hoogte
// ----------------------------TERUGREKENEN NAAR BENEDEN BREEDTE------------------------------ >> GOED //
if ($width % 10 == 0) {
$widthrounded = $width;
// If width ends with a zero, do not round the number
} else {
$widthrounded = ceil($width / 10) * 10 - 10;
// If width does not ends with a zero, round down the number
}
// ----------------------------TERUGREKENEN NAAR BENEDEN HOOGTE------------------------------- >> GOED //
if ($height % 10 == 0) {
$heightrounded = $height;
// If height ends with a zero, do not round the number
} else {
$heightrounded = ceil($height / 10) * 10 - 10;
// If height does not ends with a zero, round down the number
}
// ----------------------------IF DATABASE FOUND SELECT DB VALUE------------------------------ >> GOED //
if ($db_found) {
$SQL = "SELECT * FROM price WHERE height = " . $heightrounded . " AND width = " . $widthrounded . "";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
// ----------------------------PRINT RESULT AND CLOSE DB-------------------------------------- >> GOED //
print $db_field['value'] . "<BR>";
【问题讨论】: