2017-04-11 81 views
0

我使用這個代碼來過濾多個字段

$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

+0

'如果(空(是$ var)){做某事}' –

+0

如果{do_something()(空($ VAR)!); } 你意思是? –

+0

你可以顯示「過濾多個字段」的部分嗎? – JustOnUnderMillions

回答

0

檢查是否$字段爲空或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] . "'"; 
} 
+0

嘗試...但仍然沒有工作 – pawan35

+0

@ pawan35我沒有看到任何空的結果( 'ageid'在'txtheightfrom'和'txtheightto''waistid'之間'txtagefrom'和'txtageto''heightid'在'txtwaistfrom'和'txtwaistto'之間)你期待什麼? – swaveg

+0

我會簡單地告訴你。我正在創建一個建模機構門戶,任何用戶都可以使用範圍虎鉗搜索模型,例如年齡:從20到30或身高或腰圍。 但是,如果任何用戶只想搜索年齡和身高,但不與腰圍。那麼這段代碼將無法正常工作。 選中此項。 http://phpfiddle.org/main/code/ijn4-usx0 – pawan35