Drop-down filter menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, look closely at that ouput and think about what you really want the output to be.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

I cant see the problem, my brain aches, can we pick up again from here tomorow, thanks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Something to ponder for tomorrow then... what would the following HTML produce?

Code: Select all

<form>
  <select name="selectorMan">
    <option value="1"</option>
  </select>
</form>
And what should a select list look like when it is built properly?
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Looking at it again with fresh eyes, its obvious, the form field names are missing, your example code should read:

Code: Select all

<form>
  <select name="selectorMan">
    <option value="1">1</option>
  </select>
</form>

Working full-time and then coming home and learning all this is kinda hard to take in sometimes, please bare with me.
This comes back to the problem I had before of not knowing how to integrate more of the form code into the PHP, so how would I go about adding the actual form field names to the php or does that part of the code need to be added seperately as HTML?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Think about what you are doing a little bit. This PHP code:

Code: Select all

<?php
$rlsdate = array('last30', 'last60', 'last90', 'last6mth', 'lastyr', 'overayr');
$avgrating = array('4ormore', '3ormore', '2ormore', '1ormore');
$genre =  array('action', 'animation', 'kids', 'comedy', 'docu', 'drama', 'horror', 'musical', 'scifi', 'thriller');

echo '<form>';
echo '<select name="genre">';
foreach($rlsdate as $date) {
    echo '<option value=' . $date . '</option>';
}
echo '</select>';
echo '<select name="avgrating">';
foreach($avgrating as $arating) {
    echo '<option value=' . $arating . '</option>';
}
echo '</select>';
echo '<select name="genre">';
foreach($genre as $genreItem) {
    echo '<option value=' . $genreItem . '</option>';
}
echo '</select>';
echo '</form>';
?>
Produces this HTML:

Code: Select all

<form><select name="genre"><option value=last30</option><option value=last60</option><option value=last90</option><option value=last6mth</option><option value=lastyr</option><option value=overayr</option></select><select name="avgrating"><option value=4ormore</option><option value=3ormore</option><option value=2ormore</option><option value=1ormore</option></select><select name="genre"><option value=action</option><option value=animation</option><option value=kids</option><option value=comedy</option><option value=docu</option><option value=drama</option><option value=horror</option><option value=musical</option><option value=scifi</option><option value=thriller</option></select></form>
Now you are tasked with making the HTML output usable (which you stumbled upon in your last post) and then we can move on to capturing the form data.

PS Working full time and learning this stuff after a full days work is hard to do, but it can be done. I went from newbie to professionally employed PHP developer in about three years. ;)
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

I'm a bit stumped here, do I need to make some arrays for the field names?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No, you've already made the field names in the echo. Now you just need to make sure the form elements are being output correctly (hint: look at my last few emails and your revelation from a few posts up).
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Miracles do happen, just tried this and it worked :D

Code: Select all

<?php

$rlsdate = array('last30', 'last60', 'last90', 'last6mth', 'lastyr', 'overayr');
$avgrating = array('4ormore', '3ormore', '2ormore', '1ormore');
$genre =  array('action', 'animation', 'kids', 'comedy', 'docu', 'drama', 'horror', 'musical', 'scifi', 'thriller');

echo '<form>';

echo '<select name="releasedate">';

foreach($rlsdate as $date) {
    echo '<option value=' . $date . '>' .$date.'</option>';

}

echo '</select>';

echo '<select name="avgrating">';

foreach($avgrating as $arating) {
    echo '<option value=' . $arating . '>' . $arating . '</option>';

}

echo '</select>';

echo '<select name="genre">';

foreach($genre as $genreItem) {
    echo '<option value=' . $genreItem . '>' . $genreItem . '</option>';

}

echo '</select>';

echo '</form>';

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I knew you'd get it.

Now move on to the part where the form data is collected. You are going to be using empty(), in_array() and a few if checks inside the foreach loops for checking if what the user submitted is the one to select.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

I've attempted the start of an if statement for the first form select but not sure if the syntax is right and not sure what the code should do if the variable is found to be empty.

Code: Select all

<?php

$rlsdate = array('last30', 'last60', 'last90', 'last6mth', 'lastyr', 'overayr');
$avgrating = array('4ormore', '3ormore', '2ormore', '1ormore');
$genre =  array('action', 'animation', 'kids', 'comedy', 'docu', 'drama', 'horror', 'musical', 'scifi', 'thriller');

echo '<form>';

echo '<select name="releasedate">';

foreach($rlsdate as $date) {
    echo '<option value=' . $date . '>' .$date.'</option>';

     if(empty(in_array $rlsdate)   

}

echo '</select>';

echo '<select name="avgrating">';

foreach($avgrating as $arating) {
    echo '<option value=' . $arating . '>' . $arating . '</option>';
    
}

echo '</select>';

echo '<select name="genre">';

foreach($genre as $genreItem) {
    echo '<option value=' . $genreItem . '>' . $genreItem . '</option>';

}

echo '</select>';

echo '</form>'; 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

empty() and in_array() will be used for validation on the post vars:

Code: Select all

<?php
$releasedate = '';
if (!empty($_POST['releasedate']) && in_array($_POST['releasedate'], $rlsdate)) {
  // The posted form field is valid
  $releasedate = $_POST['releasedate'];
}

// Echo form tags and such here

foreach ($rlsdate as $v) {
  // Search the manual or these boards for the ternary operator
  $selected = $releasedate == $v ? ' selected="selected"' : '';
  echo '<option value="' . $v .'"' . $selected . '>' . $v . '</option>';
}
?>
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Not sure how to integrate this code into the code we have already. If we echo the form again in this code as well as the previous code wont we end up with two forms?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you don't need to form the second time, don't echo it. Just use the posted form data.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

How's this?:

Code: Select all

<?php

$rlsdate = array('last30', 'last60', 'last90', 'last6mth', 'lastyr', 'overayr');
$avgrating = array('4ormore', '3ormore', '2ormore', '1ormore');
$genre =  array('action', 'animation', 'kids', 'comedy', 'docu', 'drama', 'horror', 'musical', 'scifi', 'thriller');

echo '<form>';

echo '<select name="releasedate">';

foreach($rlsdate as $date) {
    echo '<option value=' . $date . '>' .$date.'</option>';

}

echo '</select>';

echo '<select name="avgrating">';

foreach($avgrating as $arating) {
    echo '<option value=' . $arating . '>' . $arating . '</option>';

}

echo '</select>';

echo '<select name="genre">';

foreach($genre as $genreItem) {
    echo '<option value=' . $genreItem . '>' . $genreItem . '</option>';

}

echo '</select>';

echo '</form>';

$releasedate = '';
if (!empty($_POST['releasedate']) && in_array($_POST['releasedate'], $rlsdate)) {
  // The posted form field is valid
  $releasedate = $_POST['releasedate'];
}

$averagegrating = '';
if (!empty($_POST['avgrating']) && in_array($_POST['avgrating'], $avgrating)) {
  // The posted form field is valid
     $averagegrating = $_POST['avgrating'];
}

$dvdgenre = '';
if (!empty($_POST['genre']) && in_array($_POST['genre'], $genre)) {
  // The posted form field is valid
     $dvdgenre = $_POST['genre'];
}


?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Did you try it?

I did, and it doesn't work. I am just wondering if you are trying this stuff as we go along.
Post Reply