如何使用多个数组检查foreach循环中的$ _post变量

时间:2017-04-11 14:46:46

标签: php arrays foreach isset

我正在使用此代码过滤多个字段

$fields = array('ageid', 'heightid', 'waistid'); //database fields
    $txtfields = array('txtagefrom', 'txtheightfrom', 'txtwaistfrom'); //from range
    $txttofields = array('txtageto', 'txtheightto', 'txtwaistto'); // To Range


$conditions = array();
foreach (array_map(NULL, $fields, $txtfields, $txttofields) as $x) {

  list($fields, $txtfields, $txttofields) = $x;
 echo "'" . $fields . "' between '" . $txtfields . "' And '" . $txttofields . "'";
}

但是我被卡住了,如果某些字段为空,那么它也会显示结果,我尝试了多种方法但是失败了。

我需要,如果这些字段中的任何一个为空,那么它将不会显示在结果上。

请参阅完整代码here

1 个答案:

答案 0 :(得分:0)

检查$ fields是否为空或is_null
你的代码中还有一个拼写错误

<input type="text" name="txtwasitto" placeholder="Wasit To" />

将其更改为

<input type="text" name="txtwaistto" placeholder="Wasit To" />

确保从$ _POST

中删除XSS
$fields = array('ageid', null, 'heightid', null, 'waistid'); //database fields
$txtfields = array('txtagefrom', 'testnull', 'txtheightfrom', 'testnull', 'txtwaistfrom'); //from range
$txttofields = array('txtageto', 'testnull', 'txtheightto', 'testnull', 'txtwaistto'); // To Range

$conditions = array();
foreach (array_map(NULL, $fields, $txtfields, $txttofields) as $x) {
    list($fields, $txtfields, $txttofields) = $x;
    if(!empty($_POST[$txtfields]) && !empty($_POST[$txttofields]))
    echo "'" . $fields . "' between '" . $_POST[$txtfields] . "' And '" . $_POST[$txttofields] . "'";
}