【问题标题】:If input value between database value A and database value B, select database value A如果输入值介于数据库值 A 和数据库值 B 之间,则选择数据库值 A
【发布时间】: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>";

【问题讨论】:

    标签: php database range


    【解决方案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);
    
    }
    
    ?>
    

    【讨论】:

    • 感谢代码!我现在面临的唯一问题是编码也会舍入舍入数字。例如,10 变为 0,20 变为 10。10 应为 10,20 应为 20 ;)
    • 感谢您的回答!与此同时,我想出了一个类似的方法来解决这个问题。上面加了!
    【解决方案2】:

    几个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);
    }
    

    【讨论】:

    • 我想如果是这个,但如果你看看人们可以做出的选择数量,这真的会让我头疼。这是我使用的类似价格表的链接:@ 987654321@
    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    相关资源
    最近更新 更多