Page 5 of 10

Posted: Tue Jun 05, 2007 2:58 pm
by phpflixnewbie
Whats the simplest way to loop them using an if-else statement?

Posted: Tue Jun 05, 2007 3:00 pm
by RobertGonzalez
foreach loop, in my opinion.

Posted: Tue Jun 05, 2007 3:23 pm
by phpflixnewbie
not sure how to loop multiple arrays is it simply:

Code: Select all

foreach ($rlsdate, $avgrating, $genre)

Posted: Tue Jun 05, 2007 3:24 pm
by TheMoose
foreach ($array as $value)

Or

foreach ($array as $key => $value) for named pairs.

Posted: Tue Jun 05, 2007 3:30 pm
by phpflixnewbie
So:

Code: Select all

foreach ($rlsdate, $avgrating, $genre as $arrayresults)

Posted: Tue Jun 05, 2007 3:35 pm
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
}

Posted: Tue Jun 05, 2007 3:49 pm
by RobertGonzalez
You're getting there phpflixnewbie. Keep on trucking, you're getting there.

Posted: Tue Jun 05, 2007 4:25 pm
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) {

} 


?>

Posted: Tue Jun 05, 2007 4:28 pm
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.

Posted: Tue Jun 05, 2007 4:31 pm
by feyd
I usually just use implode() for that type of job.

Posted: Tue Jun 05, 2007 4:53 pm
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>';

?>


Posted: Tue Jun 05, 2007 5:02 pm
by RobertGonzalez
Check you case on your third loop. Other than that, is it working the way you want it to?

Posted: Tue Jun 05, 2007 5:17 pm
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.

Posted: Tue Jun 05, 2007 5:36 pm
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.

Posted: Tue Jun 05, 2007 5:45 pm
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>