2014-03-26 30 views
1

我目前正忙於根據數據庫中的價格開發價格計算器。如果數據庫值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>"; 

回答

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); 

} 

?> 
+0

感謝您的代碼!我現在面臨的唯一問題是編碼也會舍入舍入的數字。例如,10變成0,20變成10. 10應該是10,20應該是20;) –

+0

上面添加了答案。 – Braydos1986

+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); 
} 
+0

我想如果這一個,但如果你看看人們可以做出的選擇的數量,這真的會讓我頭痛。以下是我使用的類似價格表的鏈接:http://www.entry-vouwgordijnen.nl/price_table.php –