Page 1 of 1

Reading from a dynamic amount of text boxes

Posted: Thu Jul 15, 2004 11:05 am
by FreeBaGeL
Ok, let's see if I can break this down.

I'm building a site with php/Oracle for a university class so that students can go on and view their grades, and TA's can go on and enter them.

So, when adding a gradeset, I have it setup so that the TA can select an event (added by the admin), like Exam 1, Homework 4, etc, and then a screen will pop up with empty text boxes for each student ID currently in the database.

For instance, if only Student ID 1, Student ID 2, and Student ID 7 are in the database at the time, a form would come up with 3 empty text boxes, one for student 1, one for student 2, and one for student 7.

I have written this page, it's the handling file that's giving me problems.

Now normally, when I submit a form into php, you name the textbox and then extract it in php likeso:

$points_received=$_POST['name'];

However, the problem is that because the number of textboxes is dynamically changing, I had to name the textbox by the current student ID.

So, the textbox for student 1 is named "1", etc.

Now, how to go about extracting these into php variables since I don't know how many textboxes there will be or what they will be named beforehand?

I was thinking I'd use an array, but how do I get the actual name part for only the student ID's that a grade was entered for?


There is also a second part to this. Not being able to call the database twice at the same time leads to a bit of a problem for me as well.

Once I have the scores into a php array, I was going to simply do a while loop that ran as long as a StudentID could be fetched from the database, and in each iteration if that student ID contained a value in the points_received array that was not null I would do an insert statement into the database with that student_ID and that points_received value. However, my understanding is that I cannot do an insert statement inside a while loop that is being run on a fetch command. So I'm not quite sure as to how to go about this.


Lastly, a bit of a more minor question. When they selected an event and then were passed on to the 'enter grades' form, is there any way to also send the variable that stored the event they selected into the grade handling page for the insert statement I'm using there?


I know it's confusing, but any help would be greatly appreciated.

Posted: Thu Jul 15, 2004 11:12 am
by feyd
name="student[{$student_id}]"

As for storing the event they selected, it may be best to store it into a session variable.

Moving to PHP - Code

Reading from dynamic amount of textboxes

Posted: Thu Jul 15, 2004 11:15 am
by NateFurtwangler
As for the first part, it seems like you really don't need to know which ID's are being updated/submitted.

You could just foreach through $_POST (and filter for submit button etc so you're only looking at form data) and add each response to an array variable.

For instance,

Code: Select all

<?php
$scores = array();
foreach($_POST as $id=>$score)
{
    if($id != 'nameOfSubmitButton')
        $scores[$id] = $score;
}

if(!empty($scores))
{
//process $scores array in database
}

?>
or something to that effect.

Posted: Thu Jul 15, 2004 11:24 am
by FreeBaGeL
feyd wrote:name="student[{$student_id}]"

As for storing the event they selected, it may be best to store it into a session variable.

Moving to PHP - Code
Forgive me for my noobishness (new at this), but what is a session variable?

Posted: Thu Jul 15, 2004 11:26 am
by feyd
:arrow: [php_man]session[/php_man]

Posted: Thu Jul 15, 2004 11:27 am
by kettle_drum
http://uk2.php.net/manual/en/ref.session.php
Session support in PHP consists of a way to preserve certain data across subsequent accesses.

Re: Reading from dynamic amount of textboxes

Posted: Thu Jul 15, 2004 10:27 pm
by FreeBaGeL
Well, here's what I've got, I put the print statements in to try and error check, when I run it it just prints "ArrayArrayArrayArray" to the screen:

Code: Select all

//get grades and put into database//
  $scores = array(); 
  foreach($_POST as $id=>$score) 
  { 
    echo $ID;
    echo $EDID;
    //echo $scores;
    if($id != 'submit' && $id != 'annotation' && $id != 'date') 
    {
      $scores[$id] = $score; 
    } 

    if(!empty($scores)) 
    { 
      echo $ID;
      echo $EDID;
      echo $scores[$id];
       //process $scores array in database
      $query=OCIParse($connect,'insert into mgargano.student_grades values ( :bind1, :bind2, :bind3, :bind4, :bind5)');
      OCIBindByName($query, ":bind1", $ID);
      OCIBindByName($query, ":bind2", $EDID);
      OCIBindByName($query, ":bind3", $scores[$id]);
      OCIBindByName($query, ":bind4", $ANN);
      OCIBindByName($query, ":bind5", $DTE);
    } 
  }

Posted: Thu Jul 15, 2004 10:42 pm
by feyd
looks like $ID and $EDID are both arrays..

Posted: Fri Jul 16, 2004 4:18 pm
by FreeBaGeL
well $EDID shouldn't be an array, it's the value of the detail ID that I brought in from the grades page.

Any idea why $ID is just printing as "Array" rather than the value of that array element? I have a feeling that that has something to do with why the code isn't working.

Posted: Fri Jul 16, 2004 6:17 pm
by feyd
if it's an array, you need to specify an index value to retrieve the stored element. You may want to change error_reporting to E_ALL until you go into production..

Posted: Sat Jul 17, 2004 12:01 pm
by NateFurtwangler
Try using print_r($EDID) or print_r($ID) to see the array structure of those two objects, and then from there you should be able to figure out what you need to do to extract the actual data you want.