简体   繁体   中英

HTML,PHP select filter

I have a small problem. I can't get this to filter date. I don't know if I'm missing something but I can't get it to filter ZSC_b_date I'm not a pro programmer but still. Looking for help. Thanks.

This is the HTML code.

<form method="get" action="">
<select id="training_session" name="wb btn">
<option value="">Date</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
</form>
<input type="submit" value="Filter">

PHP code

    array(
        'training_partner' => true,
        'trainer_id' => 'alves',
        'trainer_name' => 'Luciano Alves',
        'training_company_link' => '#unirede',
        'training_company_name' => 'Unirede Soluções Corporativas',
        'city_country_link' => 
        'city_country_name' => 'Porto Alegre, Brazil',
        'training_language' => 'Portuguese',
        'ZCS_b_date' => '24 November 2014',
        'ZCS_e_date' => '26 November 2014',
        'ZCS_link' => 
        'ZLE_b_date' => '27 November 2014',
        'ZLE_e_date' => '28 November 2014',
        'ZLE_link' => 
    ),
);

if (isset($_GET['lang']) && $_GET['lang']) {
     foreach ($training_sessions as $key => $session) {
        if ($session['training_language'] !== $_GET['lang']) {
            unset($training_sessions[$key]);
        }           
     }
}

"I Can Has Cheezburger" is right, your array isn't written properly. You should use an empty string if you don't want anything for "ZCS_link" (although I don't understand why you would want to do that). And you forgot to put a coma at the end of that line, so PHP is very confused now ;) Is your debugger activated?

Also, you wrote the following condition:

isset($_GET['lang'])

But I can't see any element called lang in your HTML, so I assume that this condition is never true, so PHP won't run your validation.

Change

<select id="training_session" name="wb btn">

to

<select id="training_session" name="lang">

that way PHP knows that $_GET['lang'] is the value provided for your drop-down.

Ok, several things:

Avoid using space in name attribute, so we are going to change it to:

<select id="training_session" name="wb_btn">

Give your submit button a name and place it inside the <form></form> tags

<input type="submit" name="filter_submit" value="Filter">

Now for your PHP:

if (isset($_POST['filter_submit']) && isset($_GET['wb_btn']) && $_GET['lang']!='') {
     foreach ($training_sessions as $key => $session) {
         foreach($session as $k=>$v){

            //this if checks for the date only in these keys,
            //you can remove it if you want to check for all keys
            if($k=='ZCS_b_date' || $k=='ZCS_e_date' || $k=='ZLE_b_date' || $k=='ZLE_e_date')
            {
               //e.g., check if 'december' is present in string '28 November 2014'
               //it would be false, so remove it
                if(stripos($v,$_GET['lang'])==false){ 
                     unset($training_sessions[$key][$k]);
                }
            }
        } 
    }
}

Demo

Additional:

I have a question now how do i make it without filter button, so that it would work only onclick. Should i use javascript?

Yes, you can do this with simple javascript using the onchange element of <select> tag such as:

<select id="training_session" name="wb_btn" onchange="this.form.submit()">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM