我目前正忙於根據數據庫中的價格開發價格計算器。如果數據庫值A和數據庫值B之間的輸入值選擇數據庫值A
客戶指定所需的寬度和高度,計算器應在數據庫中查找相應的產品及其價格。
寬度= 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形式(數據庫-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>";
感謝您的代碼!我現在面臨的唯一問題是編碼也會舍入舍入的數字。例如,10變成0,20變成10. 10應該是10,20應該是20;) –
上面添加了答案。 – Braydos1986
感謝您的回答!與此同時,我找到了解決這個問題的類似方法。上面加了! –