Forum Moderators: coopster
all of this for each of the 10 students that he/she nominates. I do have the form up and testing for cross-browser compatibility and everything up to the 'input'(# of student to nominate) is working with mySQL Database.
I can get:
Teacher Info, Category, and subcategory but now I'm stuck!
Any help at all would be greatly appreciated.
< way too much code, see guidelines for posting code [webmasterworld.com] - jatar_k >
[edited by: jatar_k at 9:46 pm (utc) on Feb. 24, 2004]
First, you need to assign a
name attribute to your "Number of Students to Nominate in this Subcategory" select list and make it so that only one option can be selected: <select name="nbr_nom" size="1" onChange='showInputs(this.value)'>
$nbr_nom = (isset [php.net]($_POST [us4.php.net]['nbr_nom'])) ? [us4.php.net] $_POST['nbr_nom'] : 0;
// Loop:
for [php.net] ($i = 1; $i <= $nbr_nom; $i++) {
// Build the INSERT statement:
$sql = "
INSERT INTO mytable (
teacherfname,
teacherlname,
etc.
)
VALUES(
"'".$_POST['teacherfname']."',".
"'".$_POST['teacherlname']."',".
etc.
)
";
mysql_query($sql);
}
Could I just call it:
$student_first_name[]=$_POST['student_first_name[]'];
Or maybe set it up as a single variable like
$IString = "INSERT INTO Table_Name SET
Student_First_Name ='".$Student_First_Name."'
Student_Last_Name = '".$Student_Last_Name."'
etc...;
Then during the insert just call
Teacher info, cat, subcat...then '".$IString."'
But how do you do that with these arrays?
Either way leaves me with the same problem... that darn array!...
[php.net...]
You can do dynamic variables, and dynamic function names and dynamic page names and all kinds of stuff gaurenteed to make you loose your mind :-)
Glenn
student_first_name, etc. Simply add to the loop described earlier:
$nbr_nom = (isset($_POST['nbr_nom']))? $_POST['nbr_nom'] : 0;
// Loop:
for ($i = 0; $nbr_nom <> 0 and $i < $nbr_nom; $i++) {
// Build the INSERT statement:
$sql = "
INSERT INTO mytable (
teacherfname,
teacherlname,
etc.
student_first_name,
student_last_name,
etc.
)
VALUES(
"'".$_POST['teacherfname']."',".
"'".$_POST['teacherlname']."',".
etc.
"'".$_POST['student_first_name'][$i]."',". // <-- notice use of index $i
"'".$_POST['student_last_name'][$i]."',".
etc.
)
";
mysql_query($sql);
}
[edited by: coopster at 5:20 pm (utc) on Feb. 26, 2004]
Here is a little snippet of code that shows you How to create arrays in a HTML <form> [us4.php.net]:
<?php
$nbr_nom = 3; // hard-coded here for explanation
if (isset($_POST['submit'])) {
for ($i = 0; $nbr_nom <> 0 and $i < $nbr_nom; $i++) {
print $_POST['student_first_name'][$i] . '<br />';
}
}
?>
<html><head><title>Students</title></head><body>
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="post">
<label>Student1 First Name:<input type="text" name="student_first_name[]" /></label>
<label>Student2 First Name:<input type="text" name="student_first_name[]" /></label>
<label>Student3 First Name:<input type="text" name="student_first_name[]" /></label>
<input type="submit" name="submit" value="submit"/></form>
</body></html>
Go back to msg #9 to get the correct loop assignment value from the form response:
$nbr_nom = (isset($_POST['nbr_nom'])) ? $_POST['nbr_nom'] : 0;
for loop (it's the $i[url=http://php.net/language.operators.increment]++[/url] that increments the index).