I have a page displaying around 103 records. Each record is a student record that contains a unique ID called SOMS_KEY. I am adding a grade for each student.
(In the future I hope to import a scantron for this)
Here is an example of what I am trying to add to a table.
//Go get the academic year value from config table
$GetYear = $db->sql_fetchrow($db->sql_query("SELECT Academic_Year from ".$prefix."_tl_config"));
if (!$GetYear) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
$Academic_Year = $GetYear['Academic_Year'];
What is happening is the SOMS_KEY is the last student record in the SELECT statement from the previous page. SOMS_KEY = 407 and it keep repeating for all 103 records. Each record needs to have it's own unique SOM_KEY.
Where did I go wrong?
Here is the code from the previous page.
Code:
$studentYearlist = $db->sql_query("SELECT * FROM atlas_tl_students WHERE Class_Year = $Course_Year");
while ($row = $db->sql_fetchrow($studentYearlist)) {
$SOMS_KEY = $row['SOMS_KEY'];
$Name_First = $row['Name_First'];
$Name_Last = $row['Name_Last'];
Joined: Aug 27, 2002 Posts: 15024 Location: Kansas
Posted:
Tue Dec 04, 2007 9:26 pm
Steve, The foreach construct is meant for handling arrays, not single array elements. ($_POST as opposed to $_POST['element']).
Code:
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
The first form loops over the array given by array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element).
The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop.
See
Only registered users can see links on this board! Get registered or login to the forums!
Joined: Aug 27, 2002 Posts: 15024 Location: Kansas
Posted:
Tue Dec 04, 2007 10:42 pm
Quote:
foreach ($_POST['add_irat_gr'] as $row=>$add_irat_gr) {
I think you are either missing my point or I am missing yours. You cannot use the foreach construct the way you have it written. $_POST['add_irat_gr'] is a single value/element of the $_POST array. It is not an array in and of itself. So your foundation is flawed from the beginning. Any logic after that will not work.
Quote:
How do I move this into the table without _POST?
I do not understand exactly what you are trying to do by looking at your code. Try explaining the process logic in pseudo code and not php code.
I query the student table and get all the students who are in a certain Class_Year, (for example Class_Year = 1 is all first year medical students.)
I will have over 100 students in the query and these students are listed one after the other on a page where I have a text box for entering in their IRAT grade for that session of "Team Learning". (In the future I hope to do all this by importing a scantron)
SOMS_Key is unique to each student and is the primary key in the student table. I need to write these 100 records to a session_grade table.
I thought if I capture the text boxes named add_irat_gr into an array I can loop through them and write them to the table. I was writing them to the table but I was missing all the SOMS_KEY's or at least it had captured the very last SOMS_KEY in the query and wrote that single value 407 into ALL the records.
The way i see it is that you are doing a query on the database, to extract details of students and echo them to the page, for each of those students you want text box to appear next to each of them?
You then insert their IRAT grade and click submit and all the students IRAT grade will be update within the database?
If that is correct for that happen, you will need to insert something similar to this.
Code:
$add_irat_gr_value= $_POST['add_irat_gr'];
/* Then do your for each statement */
foreach ($add_irat_gr_value as $add_irat_gr) {
$sql = mysql_query("INSERT INTO your_table (column1) VALUE ('$add_irat_gr')");
/* Maybe add in some debugging info here like, $student has been updated */
Well this is what I have now and it is still doing the same thing. The SOMS_KEY is repeating the last value read from the students table and inserting it for every record.
//Go get the academic year value from config table
$GetYear = $db->sql_fetchrow($db->sql_query("SELECT Academic_Year from ".$prefix."_tl_config"));
if (!$GetYear) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
$Academic_Year = $GetYear['Academic_Year'];
One question, each student has there own unique SOMS_KEY?
If so the reason you only getting the last one is because you did not put that hidden field into array and in between the while statement. Try the following below.
Code:
$studentYearlist = $db->sql_query("SELECT * FROM atlas_tl_students WHERE Class_Year = $Course_Year");
while ($row = $db->sql_fetchrow($studentYearlist)) {
$SOMS_KEY = $row['SOMS_KEY'];
$Name_First = $row['Name_First'];
$Name_Last = $row['Name_Last'];
Will be very pointless as you are putting the value in the name element and into the value element, so you will then need to grab all the different valued fields as each hidden field will be named differently, unless you use register globals on your script, which i would recommend not doing, your only making more work for yourself.
//Go get the academic year value from config table
$GetYear = $db->sql_fetchrow($db->sql_query("SELECT Academic_Year from ".$prefix."_tl_config"));
if (!$GetYear) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
$Academic_Year = $GetYear['Academic_Year'];
//Go get the academic year value from config table
$GetYear = $db->sql_fetchrow($db->sql_query("SELECT Academic_Year from ".$prefix."_tl_config"));
if (!$GetYear) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
$Academic_Year = $GetYear['Academic_Year'];
Couple of steps i would do now would be check the HTML output off the first page (TLSessionIratGrades.php ), to see if the page is displaying the hidden field values correctly, if so then i would check if the
Code:
$SOMS_KEY = $_POST['SOMS_KEY'];
is actully holding anything. I've commented the foreach loop out and added print_r, this will print the array for you.
//Go get the academic year value from config table
$GetYear = $db->sql_fetchrow($db->sql_query("SELECT Academic_Year from ".$prefix."_tl_config"));
if (!$GetYear) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
$Academic_Year = $GetYear['Academic_Year'];