Forum Moderators: coopster

Message Too Old, No Replies

DHTML (DOM) and PHP

         

LissaLou

5:58 pm on Feb 21, 2004 (gmt 0)

10+ Year Member



I have a dynamic form which lets you select a category and subcategory then you select how many students you want to nominate in this cateory and subcategory. This 'input' (ID tag) then writes that number of textboxes for your student name, address and grade level. My question is this: I need to now retrieve the student info into the database and am unsure how to get it there with an ID instead of a name. I know it will need to be set as an array to be able to build on the fly. The problem is if a teacher nominates 10 students, I'd like for it to insert into the DB:
Teacher Name
Title
Email
School Name
School Code
Category
Subcategory
Student Name
Student Address
Student Grade Level

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.

coopster

2:00 am on Feb 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, LissaLou!

Do you have a table of students with a PRIMARY KEY of

studentID
, or something along that line?

It might help if you give us your table(s) structure.

LissaLou

3:55 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



It is all done from the form. The database only gathers the information. The form asks for Teacher Info, then they choose a category and subcategory, then they pick a drop down number from 1 to 100. This is the number of students they wish to nominate. Here is where I get lost. I know it is an array but how do you insert all info into multiple rows of the database? Like teacher info and student 1 info
Teacher info again and student 2 info, etc. until all of the students are gathered into the database. I know I can't give a URL for it. But can I post the code for the page? I will and if it is not allowed then I will take it off. So here goes......I have condensed it because it is a long list of categories and subcategories. My problem is just with inserting multiple rows. I need to tell it to insert X number of rows and X='inputs' that the teacher picks but I am very new to PHP and don't know what to do here.

< 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]

LissaLou

3:59 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



DB table is set up to gather:
Teacher first name
Teacher last name
etc.
Category
Subcategory
Student info
Student first name
Student last name
Student address
Student grade level

But how do you get it to do the multiple row inserts?

coopster

9:21 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, I believe I understand what you are trying to do.

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

Then, in your PHP processing code, you simply need to use a control stucture [us4.php.net] to loop through the number of times specified and INSERT your rows:

$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);
}

Of course, you will have to add the logic to populate the student name and other information correctly during the loop.

LissaLou

5:55 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



OK That works! Thanks so much Coopster or should I call you Bugs! HAHAHA It does the multiple insert now! Now all I need to do is figure out the variable arrays. Thank you ever so much!

LissaLou

7:46 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



Ok This array stuff isn't like C++ at all. According to my PHP Bilble I have to set up ALL variables? So does that mean I have to make Student First Name 1 through Student First Name 100 and add a value or variable relation to each one? I'm confused again!...

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!...

webadept

3:56 am on Feb 26, 2004 (gmt 0)

10+ Year Member



Naw.. you use dynamic variables.. much easier and avoids all that typing stuff.. :-)

[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

coopster

2:22 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



webadept is right, you don't need to define a variable for each array element, PHP has actually already done that for you when it builds your $_POST variable,
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]

LissaLou

4:46 pm on Feb 26, 2004 (gmt 0)

10+ Year Member



So when you declare the student_first_name, etc all you do is:
$student_first_name = $_POST[student_first_name][$i];
And do that for all of the others the same way?
Then you do the loop, which works beautifully by the way. I need to add the student info list to the Database table today and test it to make sure it actually adds the next student and then the next student with the teachers name and not the same student 5 or 10 times! Thanks for the help. I may actually get somewhere today!

coopster

5:19 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, you can assign the value to a variable if you would like, but you can also use the data right straight out of the $_POST array as I showed you too. There is one fix here, arrays start numbering at 0, and the loop is starting on 1, I'll make the change in msg #9 to handle accordingly.

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>

LissaLou

2:54 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



OK that works but I still have 1 small problem. When it inserts 3 rows, it only adds the array twice and then 1 row with only the teacher info(non-array). I know I need to add +1 in there for each array to make it match the insert row number but where?

coopster

4:10 pm on Mar 1, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I hard-coded a variable assignment in the snippet, don't use that (notice my comment).

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;

The increment happens during the
for
loop (it's the
$i[url=http://php.net/language.operators.increment]++[/url]
that increments the index).