Drop-down filter menu
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
phpflixnewbie
- Forum Contributor
- Posts: 132
- Joined: Fri Nov 17, 2006 11:46 am
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Something to ponder for tomorrow then... what would the following HTML produce?
And what should a select list look like when it is built properly?
Code: Select all
<form>
<select name="selectorMan">
<option value="1"</option>
</select>
</form>-
phpflixnewbie
- Forum Contributor
- Posts: 132
- Joined: Fri Nov 17, 2006 11:46 am
Looking at it again with fresh eyes, its obvious, the form field names are missing, your example code should read:
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?
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?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Think about what you are doing a little bit. This PHP code:
Produces this HTML:
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.
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>';
?>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>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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
phpflixnewbie
- Forum Contributor
- Posts: 132
- Joined: Fri Nov 17, 2006 11:46 am
Miracles do happen, just tried this and it worked 
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>';
?>
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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
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>'; - RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
phpflixnewbie
- Forum Contributor
- Posts: 132
- Joined: Fri Nov 17, 2006 11:46 am
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'];
}
?>
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA