我目前正忙于根据数据库中的价格开发价格计算器。
客户指定所需的宽度和高度,计算器应在数据库中查找相应的产品及其价格。
宽度= 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
有没有办法以简单易懂的方式做到这一点?
谢谢!
ANSWER
我使用以下代码来解决问题:
$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>";
答案 0 :(得分:1)
这是一个非常简单的功能,可以对数字进行四舍五入,
<?php
$width = $_POST['width'];
$width1 = $width - ($width % 10 - 10);
echo "The number ". $width ." rounded up is ". $width1 ."!"
?>
此功能将向下舍入,
<?php
$value = $_POST['width'];
$value1 = ceil($value / 10) * 10 - 10;
echo "The number ". $value ." rounded down is ". $value1 ."!"
?>
此函数将检查宽度是否包含零,如果是,它将传递变量,如果不包含它将向下舍入(strpos
检查字符串内的字符串);
<?php
$value = $_POST['width'];
if (strpos($value,'0') !== false) {
$SQL = "SELECT * FROM price WHERE height = " . $_POST["height"] . " AND width = " . $value . "";
$result = mysql_query($SQL);
}
else {
$value1 = ceil($value / 10) * 10 - 10;
$SQL = "SELECT * FROM price WHERE height = " . $_POST["height"] . " AND width = " . $value1 . "";
$result = mysql_query($SQL);
}
?>
答案 1 :(得分:0)
一些if
语句应该这样做,
$height = $_POST["height"];
$width = $_POST["width"];
if ($width >= 10 && $width <= 19) {
$SQL = "SELECT * FROM price WHERE height = '10' AND width = '10' ";
$result = mysql_query($SQL);
}
if ($width >= 20 && $width <= 29) {
$SQL = "SELECT * FROM price WHERE height = '10' AND width = '20' ";
$result = mysql_query($SQL);
}