Skip to content Skip to sidebar Skip to footer

Php: Obtaining Data From Input In Html Form Then Output Using Filter_array

I am trying to create a filter system basically, that gets data from input in a HTML form and then uses that data in PHP to filter existing 'profiles' from $profileArray , outputti

Solution 1:

I didn't understand what your question was but i think you are not able to loop through you'r array. This loop will help you too loop through the multidimensional array. And in you array the age isnt in string, so you don't have to change the value of the user input from string to int.

<html><body><formaction="profileFilter.php"method="POST"><p>Age: <inputtype="text"name="ageChoice"></p><p>Colour: <inputtype="text"name="colourChoice"></p><inputtype="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;
        }
    }
 }
}

Post a Comment for "Php: Obtaining Data From Input In Html Form Then Output Using Filter_array"