2016-05-22 53 views
2

我想創建一個基本的過濾系統,它從HTML表單中的輸入獲取數據,然後使用PHP中的數據從$profileArray過濾現有的「配置文件」 ,輸出包括用戶輸入的數據/變量的任何配置文件。我不知道如何通過我的filter_array函數發送用戶輸入數據,據我所知,它使用var_dump進行測試。作爲初學者,我無法理解php.net中的$ foo和$ bar解釋,因此非常感謝您的清楚解釋。PHP:從HTML表單輸入獲取數據,然後使用filter_array輸出

HTML

<html> 
<body> 
    <form action="profileFilter.php" method="POST"> 
    <p>Age: <input type="text" name="ageChoice"></p> 
    <p>Colour: <input type="text" name="colourChoice"></p> 
    <input type="submit" name="catQualitiesBtn"> 
    </form> 
</body> 
</html> 

PHP

<?php  
    $profileArray = array( 
    array( 'Name' => "Toby", 
      'Age' => 3, 
      'Colour' => "Ginger", 
      ), 

array( 'Name' => "Cassie", 
     'Age' => 3, 
     'Colour' => "Tabby", 
     ), 

array( 'Name' => "Lucy", 
     'Age' => 1, 
     'Colour' => "Black", 
     ) 
     ); 

function get_profile_by_age ($profile, $age){ 
    return array_filter ($profile, function($ageInput) use ($age){ 
    return $ageInput['Age'] === $age; 
    });  
    } 

function get_profile_by_age ($profile, $colour){ 
    return array_filter ($profile, function($colourInput) use ($colour){ 
    return $colourInput['Colour'] === $colour; 
    });  
    } 

$ageInput = intval($_POST['ageChoice']); 
$colourInput = strval($_POST['colourChoice']); 

echo function get_profile_by_age ($ageInput , $age); 
echo function get_profile_by_colour ($colourInput , $colour); 
?> 

回答

0

我不明白你的問題是什麼,但我認爲你無法遍歷數組找你。此循環將幫助您循環遍歷多維數組。而在你的數組中,年齡並不是字符串,所以你不必把用戶輸入的值從字符串改爲int。

<html> 
<body> 
<form action="profileFilter.php" method="POST"> 
<p>Age: <input type="text" name="ageChoice"></p> 
<p>Colour: <input type="text" name="colourChoice"></p> 
<input type="submit" name="catQualitiesBtn"> 
</form> 
</body> 
</html> 

PHP

<?php 

$profileArray = array( 
array( 'Name' => "Toby", 
     'Age' => 3, 
     'Colour' => "Ginger", 
     ), 

    array( 'Name' => "Cassie", 
    'Age' => 3, 
    'Colour' => "Tabby", 
    ), 

    array( 'Name' => "Lucy", 
    'Age' => 1, 
    'Colour' => "Black", 
    ) 
    ); 

    if (isset($_POST['ageChoice']) && isset($_POST['colourChoice'])) { 
    $ageInput = $_POST['ageChoice']; 
    $colourInput = $_POST['colourChoice']; 

for ($i=0; $i >=0 ; $i++) { 
     if ($profileArray[i]['Age'] == $_POST['ageChoice'] && $profileArray[i]['Colour'] == $_POST['colourChoice']) { 
      echo "name -".$profileArray[i]['Name']; 
      break; 
     } 
    } 
} 
} 
+0

謝謝你的頭的上了'intval'其實我編輯它,你發送你的答案之前。我沒有收到錯誤,但在將值提交到HTML表單後沒有顯示任何內容?我試圖製作一個基本的過濾系統,它從'$ profileArray'中吐出包含在HTML表單中輸入的字符串數據的配置文件。 –

+0

@eainnem使用上述循環 – Sumit

+0

我做了,它不工作。它將我帶入連續循環。最後你還有一個} .... –

相關問題