Author |
Message |
Donovan
Client

Joined: Oct 07, 2003
Posts: 735
Location: Ohio
|
Posted:
Tue Dec 04, 2007 1:36 pm |
|
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.
'$SOMS_KEY','$Session_ID','$grade','$grade_type', '$Academic_Year'
Some of my code.
Code:foreach ($_POST['add_irat_gr'] as $row=>$add_irat_gr) {
$grade = $add_irat_gr;
$SOMS_KEY = $_POST['SOMS_KEY'];
$Session_ID = $_POST['Session_ID'];
$grade_type = 'IRAT';
//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'];
$sql = "INSERT INTO ".$prefix."_tl_session_grades (SOMS_KEY, Session_ID, grade, grade_type, Academic_Year)". "VALUES ('$SOMS_KEY','$Session_ID','$grade','$grade_type', '$Academic_Year')";
if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
$result = $db->sql_query($sql);
}
|
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'];
if (!$studentYearlist) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
//<!-- BEGIN Student Row -->"
echo "<tr><td align=\"center\">$SOMS_KEY</td>"
. " <td align=\"left\">$Name_First $Name_Last</td>"
. " <td><input type='text' name='add_irat_gr[]' size='3' maxlength='3'></td>"
. "</tr>";
}
//-- END Student Row -->"
echo "<tr><td colspan='3' align='center'><input type='submit' value='Add Grade(s)'></td></tr>\n";
echo "<input type='hidden' name='Session_ID' value='$Session_ID'>\n";
echo "<input type='hidden' name='SOMS_KEY' value='$SOMS_KEY'>\n";
echo "</form>\n";
|
|
|
|
 |
 |
Raven
Site Admin/Owner

Joined: Aug 27, 2002
Posts: 17088
|
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! for more information. |
|
|
|
 |
Donovan

|
Posted:
Tue Dec 04, 2007 9:50 pm |
|
How is this not an array?
Code:. " <td><input type='text' name='add_irat_gr[]' size='3' maxlength='3'></td>"
|
|
|
|
|
 |
Raven

|
Posted:
Tue Dec 04, 2007 10:10 pm |
|
Quote: |
foreach ($_POST['add_irat_gr']
|
That is what I am referring to. That is an array element. $_POST is an array. |
|
|
|
 |
Donovan

|
Posted:
Tue Dec 04, 2007 10:26 pm |
|
How do I move this into the table without _POST?
This all resides in a page called IRATGrades.php with form action = IRATGradesInsert
Code://<!-- BEGIN Student Row -->"
echo "<tr><td align=\"center\">$SOMS_KEY</td>"
. " <td align=\"left\">$Name_First $Name_Last</td>"
. " <td><input type='text' name='add_irat_gr[]' size='3' maxlength='3'></td>"
. "</tr>";
}
//-- END Student Row -->"
|
IRATGradesInsert.php is where I write to the table.
I have over 100 records that I enter grades for. |
|
|
|
 |
Raven

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

|
Posted:
Tue Dec 04, 2007 11:08 pm |
|
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)
I also capture the following:
Code:$SOMS_KEY = $_POST['SOMS_KEY'];
$Session_ID = $_POST['Session_ID'];
$grade_type = 'IRAT';
|
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. |
|
|
|
 |
xblader
Client

Joined: Aug 17, 2006
Posts: 28
|
Posted:
Wed Dec 05, 2007 4:55 am |
|
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 */
}
|
|
|
|
|
 |
Donovan

|
Posted:
Wed Dec 05, 2007 10:07 am |
|
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.
Code:$add_irat_gr_value= $_POST['add_irat_gr'];
$SOMS_KEY = $_POST['SOMS_KEY'];
$Session_ID = $_POST['Session_ID'];
$grade_type = 'IRAT';
//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'];
/* Do the for each statement */
foreach ($add_irat_gr_value as $add_irat_gr) {
$sql = "INSERT INTO ".$prefix."_tl_session_grades (SOMS_KEY, Session_ID, grade, grade_type, Academic_Year)". "VALUES ('$SOMS_KEY','$Session_ID','$add_irat_gr','$grade_type', '$Academic_Year')";
if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
$result = $db->sql_query($sql);
}
|
|
|
|
|
 |
xblader

|
Posted:
Wed Dec 05, 2007 10:14 am |
|
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'];
if (!$studentYearlist) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
//<!-- BEGIN Student Row -->"
echo "<tr><td align=\"center\">$SOMS_KEY</td>"
. " <td align=\"left\">$Name_First $Name_Last</td>"
. " <td><input type='text' name='add_irat_gr[]' size='3' maxlength='3'></td>"
. "</tr>";
echo "<input type='hidden' name='SOMS_KEY[]' value='$SOMS_KEY'>\n";
}
//-- END Student Row -->"
echo "<tr><td colspan='3' align='center'><input type='submit' value='Add Grade(s)'></td></tr>\n";
echo "<input type='hidden' name='Session_ID' value='$Session_ID'>\n";
echo "</form>\n";
|
You will also then need to do a foeach loop for this. |
|
|
|
 |
Donovan

|
Posted:
Wed Dec 05, 2007 11:51 am |
|
Could I also do this?
Code:echo "<input type='hidden' name='add_irat_gr[".$SOMS_KEY."]' value='$SOMS_KEY'>\n";
|
Then iterate thru like
Code:foreach ($add_irat_gr_value as $soms_key => $add_irat_gr)
|
|
|
|
|
 |
xblader

|
Posted:
Thu Dec 06, 2007 8:52 am |
|
to do Code:echo "<input type='hidden' name='add_irat_gr[".$SOMS_KEY."]' value='$SOMS_KEY'>\n";
|
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.
Keep it to the following.
Code:echo "<input type='hidden' name='add_irat_gr[]' value='$SOMS_KEY'>\n";
|
also i thought the field add_irat_gr will be a text field and not hold the value of the variable $SOMS_KEY?
after, then grab it like below. added in a foreach statement after the first foreach.
Code:
$add_irat_gr_value= $_POST['add_irat_gr'];
$SOMS_KEY = $_POST['SOMS_KEY'];
$Session_ID = $_POST['Session_ID'];
$grade_type = 'IRAT';
//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'];
/* Do the for each statement */
foreach ($add_irat_gr_value as $add_irat_gr) {
foreach ($SOMS_KEY as $SOMS_KEY_value) {
$sql = "INSERT INTO ".$prefix."_tl_session_grades (SOMS_KEY, Session_ID, grade, grade_type, Academic_Year)". "VALUES ('$SOMS_KEY','$Session_ID','$add_irat_gr','$grade_type', '$Academic_Year')";
if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
$result = $db->sql_query($sql);
}
}
|
|
|
|
|
 |
Donovan

|
Posted:
Thu Dec 06, 2007 9:15 am |
|
Getting errors on the following:
Code:
foreach ($add_irat_gr_value as $add_irat_gr) {
foreach ($SOMS_KEY as $SOMS_KEY_value) {
$sql = "INSERT INTO ".$prefix."_tl_session_grades (SOMS_KEY, Session_ID, grade, grade_type, Academic_Year)". "VALUES ('$SOMS_KEY_value','$Session_ID','$add_irat_gr','$grade_type', '$Academic_Year')";
if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
$result = $db->sql_query($sql);
}
}
|
I don't think I have any values for
Code:foreach ($SOMS_KEY as $SOMS_KEY_value) {
|
The other page has the following:
Code:
//<!-- BEGIN Student Row -->"
echo "<tr><td align=\"center\">$SOMS_KEY</td>"
. " <td align=\"left\">$Name_First $Name_Last</td>"
. " <td><input type='text' name='add_irat_gr[]' size='3' maxlength='3'></td>"
. "<input type='hidden' name='add_irat_gr[]' value='$SOMS_KEY'>\n"
. "</tr>";
|
|
|
|
|
 |
xblader

|
Posted:
Thu Dec 06, 2007 9:19 am |
|
Post up the error i can then tell you, also if you can show us the code for for both pages i can tell you where you gone wrong. |
|
|
|
 |
Donovan

|
Posted:
Thu Dec 06, 2007 9:28 am |
|
Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/atlas/modules/Team_Learning/admin/TLSessionIratInsert.php on line 37
This is the TLSessionIratGrades.php
Code:if(!defined('TL_ADMIN')) { die("Illegal Access Detected!!!"); }
$modname = "Team_Learning";
include("header.php");
TLAdminMenu();
echo "<br />\n";
$Session_ID = $_GET['Session_ID'];
$coursename = $db->sql_query("SELECT Course_Name, Course_Year, Session_Name FROM atlas_tl_session s, atlas_courses c WHERE s.Course_ID = c.Course_ID AND s.Session_ID = '$Session_ID'");
while($info = $db->sql_fetchrow($coursename)) {
$Course_Name = $info[Course_Name];
$Session_Name = $info[Session_Name];
$Course_Year = $info[Course_Year];
}
OpenTable();
echo "<form method='post' action='".$admin_file.".php'>\n";
echo "<input type='hidden' name='op' value='TLSessionIratInsert'>\n";
echo "<table width='100%' border='1' cellspacing='0' cellpadding='2'>\n";
echo "<tr><td colspan='3' width='100%' bgcolor='$bgcolor2'><nobr><b>TL IRAT Session Name: $Session_Name for Course $Course_Name</b></nobr></td></tr>\n";
echo "<tr><td colspan=2 width='100%'><nobr>Year $Course_Year Students</td></tr>\n";
echo "<tr><td colspan=2 width='100%'><a href='".$admin_file.".php?op=TLImportScantron'\" class=\"gen\">Import Scantron</a></td></tr>\n";
echo "</table>\n";
echo "<br />\n";
echo "<table width='100%' align='center' border='1' cellspacing='0' cellpadding='2'>\n";
echo "<tr><td align='center' bgcolor='$bgcolor2' width='5%'><b>SOM Key</b></td><td align='left' bgcolor='$bgcolor2' width='50%'><b>Student Name</b></td><td align ='center' bgcolor='$bgcolor2' width='20%'><b>IRAT Grade</b></td></tr>\n";
$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'];
if (!$studentYearlist) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
//<!-- BEGIN Student Row -->"
echo "<tr><td align=\"center\">$SOMS_KEY</td>"
. " <td align=\"left\">$Name_First $Name_Last</td>"
. " <td><input type='text' name='add_irat_gr[]' size='3' maxlength='3'></td>"
. "<input type='hidden' name='add_irat_gr[]' value='$SOMS_KEY'>\n"
. "</tr>";
}
//-- END Student Row -->"
echo "<tr><td colspan='3' align='center'><input type='submit' value='Add Grade(s)'></td></tr>\n";
echo "<input type='hidden' name='Session_ID' value='$Session_ID'>\n";
//echo "<input type='hidden' name='add_irat_gr[".$SOMS_KEY."]' value='$SOMS_KEY'>\n";
echo "</form>\n";
echo "</table>\n";
CloseTable();
include("footer.php");
?>
|
This is the TLSessionIratInsert.php
Code:if(!defined('TL_ADMIN')) { die("Illegal Access Detected!!!"); }
$modname = "Team_Learning";
include("header.php");
TLAdminMenu();
$add_irat_gr_value= $_POST['add_irat_gr'];
$SOMS_KEY = $_POST['SOMS_KEY'];
$Session_ID = $_POST['Session_ID'];
$grade_type = 'IRAT';
//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'];
//foreach( $add_irat_gr_value as $SOMS_KEY => $add_irat_gr){
//echo "Key: $SOMS_KEY, Grade: $add_irat_gr Grade Type: $grade_type <br />";
//}
foreach ($add_irat_gr_value as $add_irat_gr) {
foreach ($SOMS_KEY as $SOMS_KEY_value) {
$sql = "INSERT INTO ".$prefix."_tl_session_grades (SOMS_KEY, Session_ID, grade, grade_type, Academic_Year)". "VALUES ('$SOMS_KEY_value','$Session_ID','$add_irat_gr','$grade_type', '$Academic_Year')";
if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
$result = $db->sql_query($sql);
}
}
//foreach ($_POST['add_irat_gr'] as $row=>$add_irat_gr) {
//$grade = $add_irat_gr;
//$SOMS_KEY = $_POST['SOMS_KEY'][$row];
//$Session_ID = $_POST['Session_ID'];
//$grade_type = 'IRAT';
//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'];
//$sql = "INSERT INTO ".$prefix."_tl_session_grades (SOMS_KEY, Session_ID, grade, grade_type, Academic_Year)". "VALUES ('$SOMS_KEY','$Session_ID','$grade','$grade_type', '$Academic_Year')";
//if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
// $result = $db->sql_query($sql);
//}
Opentable();
echo"Grades Added";
CloseTable();
//header("Location: ".$admin_file.".php?op=TLSessionList&Course_ID=$Course_ID");
?>
|
|
|
|
|
 |
xblader

|
Posted:
Thu Dec 06, 2007 9:31 am |
|
Code: echo "<tr><td align=\"center\">$SOMS_KEY</td>"
. " <td align=\"left\">$Name_First $Name_Last</td>"
. " <td><input type='text' name='add_irat_gr[]' size='3' maxlength='3'></td>"
. "<input type='hidden' name='add_irat_gr[]' value='$SOMS_KEY'>\n"
. "</tr>";
|
You got to fields named the same here, you will need to change the hidden field name to SOMS_KEY[] |
|
|
|
 |
Donovan

|
Posted:
Thu Dec 06, 2007 9:56 am |
|
Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/atlas/modules/Team_Learning/admin/TLSessionIratInsert.php on line 37
same line
Code: foreach ($SOMS_KEY as $SOMS_KEY_value) {
|
|
|
|
|
 |
xblader

|
Posted:
Thu Dec 06, 2007 10:16 am |
|
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.
Code:if(!defined('TL_ADMIN')) { die("Illegal Access Detected!!!"); }
$modname = "Team_Learning";
include("header.php");
TLAdminMenu();
$add_irat_gr_value= $_POST['add_irat_gr'];
$SOMS_KEY = $_POST['SOMS_KEY'];
$Session_ID = $_POST['Session_ID'];
$grade_type = 'IRAT';
//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'];
//foreach( $add_irat_gr_value as $SOMS_KEY => $add_irat_gr){
//echo "Key: $SOMS_KEY, Grade: $add_irat_gr Grade Type: $grade_type <br />";
//}
print_r($SOMS_KEY);
/* foreach ($add_irat_gr_value as $add_irat_gr) {
foreach ($SOMS_KEY as $SOMS_KEY_value) {
$sql = "INSERT INTO ".$prefix."_tl_session_grades (SOMS_KEY, Session_ID, grade, grade_type, Academic_Year)". "VALUES ('$SOMS_KEY_value','$Session_ID','$add_irat_gr','$grade_type', '$Academic_Year')";
if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
$result = $db->sql_query($sql);
}
} */
//foreach ($_POST['add_irat_gr'] as $row=>$add_irat_gr) {
//$grade = $add_irat_gr;
//$SOMS_KEY = $_POST['SOMS_KEY'][$row];
//$Session_ID = $_POST['Session_ID'];
//$grade_type = 'IRAT';
//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'];
//$sql = "INSERT INTO ".$prefix."_tl_session_grades (SOMS_KEY, Session_ID, grade, grade_type, Academic_Year)". "VALUES ('$SOMS_KEY','$Session_ID','$grade','$grade_type', '$Academic_Year')";
//if (!$sql) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
// $result = $db->sql_query($sql);
//}
Opentable();
echo"Grades Added";
CloseTable();
//header("Location: ".$admin_file.".php?op=TLSessionList&Course_ID=$Course_ID");
?>
|
|
|
|
|
 |
Donovan

|
Posted:
Thu Dec 06, 2007 10:53 am |
|
First step checked out. I viewed source and could see all the SOM_KEYS for each row.
Second step only displays one value... the very last SOM_KEY for the SELECT clause.
407 |
|
|
|
 |
xblader

|
Posted:
Fri Dec 07, 2007 3:41 am |
|
just sounds like it not putting the SOM_KEY into an array, can you actully confirm that you have the hidden field SOMS_KEY is in the array format? like below?
[code]//<!-- BEGIN Student Row -->"
echo "<tr><td align=\"center\">$SOMS_KEY</td>"
. " <td align=\"left\">$Name_First $Name_Last</td>"
. " <td><input type='text' name='add_irat_gr[]' size='3' maxlength='3'></td>"
. "<input type='hidden' name='SOMS_KEY[]' value='$SOMS_KEY'>\n"
. "</tr>";
} |
|
|
|
 |
|