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
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Whats the simplest way to loop them using an if-else statement?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

foreach loop, in my opinion.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

not sure how to loop multiple arrays is it simply:

Code: Select all

foreach ($rlsdate, $avgrating, $genre)
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

foreach ($array as $value)

Or

foreach ($array as $key => $value) for named pairs.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

So:

Code: Select all

foreach ($rlsdate, $avgrating, $genre as $arrayresults)
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

You'll need to loop on each one separately, ie:

Code: Select all

foreach($rlsdate as $dateItem) {
// do stuff with $dateItem here
}
foreach($avgrating as $ratingItem) {
// do stuff with $ratingItem here
}
foreach($genre as $genreItem) {
// do stuff with $genreItem here
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You're getting there phpflixnewbie. Keep on trucking, you're getting there.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

OK so far we have the following code. But I dont know what im supposed to do in the foreach loops.

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');

foreach($rlsdate as $date) {

}
foreach($avgrating as $arating) {

}
foreach($genre as $genreItem) {

} 


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

Post by RobertGonzalez »

You're supposed to echo the value of each member of the array as a select menu option.

Code: Select all

<?php
foreach ($array as $value) {
  echo '<p>' . $value . '</p>';
}
?>
That was pseudo code, to give you an idea of what we are talking about.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I usually just use implode() for that type of job.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

OK makes sense now, thx for the tip feyd, but I'd like to keep things as simple as possible for now, once i've grasped the basics and got the code working I could perhaps look into functions like implode which will make the code more efficient. Heres my attempt at echoing the form menus:

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 '<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>';

?>

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

Post by RobertGonzalez »

Check you case on your third loop. Other than that, is it working the way you want it to?
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Corrected case. Er, im not sure if im supposed to be including all the HTML form code as well but without the HTML form code I just get 3 drop down menus but they dont have any select names or values.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

For testing, just wrap the form echos in an opening and closing form tag. Make sure to view the source to see what is being generated by the code.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Was'nt sure if this is what you meant, but Ive included the output source that it produces:

PHP:

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>';

?>

Prodcues:

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>
Post Reply