php代码返回基于数组的值


php code to return value based on array?

基本上我有2页;Page1.php和Page2.php…在第1页上,用户可以输入他们的邮政编码和属性值,提交后页面将POST到Page2.php。

我正在寻求帮助,让一个小脚本根据属性值返回(回显或打印)一个值给客户。为了简单起见,我会将属性值设置在范围内,例如:

£0        =>   £100,000  -->  Return as £125
£101,000  =>   £200,000  -->  Return as £195
£201,000  =>   £300,000  -->  Return as £275

等等…

我希望让脚本返回输出变量,这样我就可以使用echo函数在页面上的set css框中显示用户输入的范围的默认值集。

因此,如果我以客户身份输入page1.php,并输入我的邮政编码,并将我的财产价值输入为225000英镑(这不会用作选定选项,而是由用户手动输入,与邮政编码框相同)

当我提交并被带到page2.php时,脚本会意识到我输入的金额在201000-300000英镑之间,并使用echo变量函数返回默认值。

我希望我已经尽我所能解释了,因为我真的在学习基础知识,但仍在努力适应某些术语。我们非常感谢您的任何帮助,并感谢您抽出时间阅读这篇文章,包括发表评论和提供帮助

第1页包含用户输入的表单:

<form action="page2.php" method="post">
  <input type="text" name="userinput" placeholder="Type.." />
  <input type="submit" value="Send to page 2" name="submitPrice" />
</form>

查看输入元素上的name属性

第2页

此页面处理表单中的数据。

一种方法是使用一个查找表来存储范围:

$data = array(
    array(
        'min' => 0,
        'max' => 100,
        'value' => 125
    ),
    array(
        'min' => 101,
        'max' => 200,
        'value' => 195
    ),
    array(
        'min' => 201,
        'max' => 300,
        'value' => 275
    )
);

现在,您可以使用此表检查用户输入是否在minmax之间的范围内。我在这个例子中使用了array_filter

function getAdjustedPrice($price, &$table) {
    $priceData = current(array_filter($table, function($value) use(&$price) {
        return $value['min'] <= $price && $value['max'] >= $price;
    }));
    return $priceData['value'];
}

检查它是否有效:

$input = 101;
printf("User entered: %d, adjusted price: %d", 
       $input, 
       getAdjustedPrice($input, $data));

到目前为止,$input只是静态的,所以我们可能想用值。该值存储在超全局数组_POST:中

$input = intval($_POST["userinput"]);

"userinput"是我们在第1页表单中对输入元素使用的名称。

这是page1.php:的代码

<form action="page2.php" method="get">
    <input name="userfield" id="thiscanbedifferent" type="text">
 </form>

page2.php的这段代码将使用户输入可以安全使用:

$userPrice = $_GET['userfield'];//The $_GET variable has all the values from your form as long as you specify method: get. Remember that users are both incompetent and devious, and that you can never trust their input. Always clean and check it!!
$userPrice = preg_replace('/[^0-9.]/','',$userPrice);//This is called a regular expression, it filters out stuff we don't want. This one gets rid of characters that are not in the range 0-9, or "."
$userPrice = floatval($userPrice);//This turns the cleaned value into a number

此代码将把任意数字转换为一个范围。

$rangeLabels = ["$0 - $100,000",
"$101,000-$200,000",
"$201,000-$300,000"];//labels for pricing. can be extended indefinately, as needed
$rangePricing = [125,175,195];//pricing steps. can be extended indefinately, as needed
$rangeIndex = floor($userPrice/100000);//100000 can be adjusted to any increment you would like, just be sure to update labels accordingly
//$rangeIndex = floor(($userPrice-1000)/100000);//this alternate equation makes values such as 100,999 fall below $101,000. My literal (probably wrong) interpretation of your range labels.
if($rangeIndex < 0 || $rangeIndex > count($rangePricing)){
   die('Number is out of range!');//Use better error handling in production
}
echo 'The user has selected the price $' . floatval($userPrice) . ' which falls under into the range ' . $rangeLabels[$rangeIndex] . ', for which the pricing is ' . $rangePricing[$rangeIndex] . '<br>';