Page 6 of 10

Posted: Tue Jun 05, 2007 6:02 pm
by RobertGonzalez
Ok, look closely at that ouput and think about what you really want the output to be.

Posted: Tue Jun 05, 2007 6:09 pm
by phpflixnewbie
I cant see the problem, my brain aches, can we pick up again from here tomorow, thanks.

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

Posted: Wed Jun 06, 2007 1:37 pm
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?

Posted: Wed Jun 06, 2007 3:07 pm
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. ;)

Posted: Wed Jun 06, 2007 3:36 pm
by phpflixnewbie
I'm a bit stumped here, do I need to make some arrays for the field names?

Posted: Wed Jun 06, 2007 3:43 pm
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).

Posted: Wed Jun 06, 2007 4:01 pm
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>';

?>

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

Posted: Wed Jun 06, 2007 4:43 pm
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>'; 

Posted: Thu Jun 07, 2007 3:43 pm
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>';
}
?>

Posted: Fri Jun 08, 2007 10:07 am
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?

Posted: Fri Jun 08, 2007 10:39 am
by RobertGonzalez
If you don't need to form the second time, don't echo it. Just use the posted form data.

Posted: Fri Jun 08, 2007 11:18 am
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'];
}


?>

Posted: Fri Jun 08, 2007 12:37 pm
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.