I have a foreach($_POST[''] as $Key => $value) problem

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   I have a foreach($_POST[''] as $Key => $value) problem
Donovan
CZ Active Member
 Codezwiz Site Donator
Donovan has been a member for over 14 year's 14 Year Member
usa.gif ohio.gif
Occupation: Web Developer
Fav. Sports Team: St Louis Cardinals
Status: Offline
Joined: Dec 03, 2003
0.03 posts per day
Posts: 160
Points: 9,216
   
I have a student Peer Evaluation I'm working on that allows a team member to complete a peer evaluation on their team members. Probably 5-6 to a group so they would have all their members on the same page excluding themselves. They assign a rating or score and a comment for each then I insert this to a peereval table.

My variable are:

$SOMS_KEY = Unique to each student
$UID = University ID unique to each student
$Course_Number = self explanatory
$rater = team member who is logged in a evaluating
$evalcomment = comments for each team member
$evalscore = score given to each team member.

Here is my form.

OpenTable();
  echo "<form name = 'PeerEval' method='post' action='".$_server['php_self']."'>\n"; 
  echo "     <table width='100%' border='1' cellpadding='3'>"
  . "       <tr><td colspan='3' align='center'>Group: $UserGroup</td><tr>"
  . "      <tr>"
  . "         <td>Team Member</td>"
  . "         <td>Score</td>"
  . "         <td>Helpful Feedback</td>"
  . "       </tr>";
  $getgroupmembers = $db->sql_query("SELECT s.Name_First, s.Name_Last, s.SOMS_KEY, s.UID, gs.LDAP_USER FROM ".$prefix."_tl_group_students gs LEFT JOIN ".$prefix."_tl_students s ON s.SOMS_KEY = gs.SOMS_KEY 
WHERE gs.Group_ID = '$UserGroup'
AND gs.LDAP_USER != '$authuser'");
while($row = $db->sql_fetchrow($getgroupmembers)) {
$SOMS_KEY = $row['SOMS_KEY'];
$UID = $row['UID'];
$Name_First = $row['Name_First'];
$Name_Last = $row['Name_Last'];
echo"<tr>"
    ."<td align='center'>&nbsp;$Name_First $Name_Last &nbsp;</td>"
    ."<td>"   
    ."<select name='evalscore[{$SOMS_KEY}]'>"
    ."<option value='7' selected>7</option>"
   ."<option value='8'>8</option>"
   ."<option value='9'>9</option>"
   ."<option value='10'>10</option>"
   ."<option value='11'>11</option>"
   ."<option value='12'>12</option>"
   ."<option value='13'>13</option>"
   ."</select>"
   ."</td>"   
    ."<td align='left'><textarea name ='evalcomment' cols='100' colspan='1' rows='2'></textarea></td>"   
    ."</tr>";
   
}
$countgroupmembers = $db->sql_query("SELECT * FROM ".$prefix."_tl_group_students WHERE gs.Group_ID = '$UserGroup'");
$grouptotal = $db->sql_numrows($countgroupmembers);
if ($grouptotal = 7) {
$Total_Points = 60;
} elseif ($grouptotal = 6) {
$Total_Points = 50;
} elseif ($grouptotal = 5)  {
$Total_Points = 40;
}
echo "<tr><td align='left'> Total Points Must = $Total_Points</td>";
echo "<td align='left'>$SUM_Points</td></tr>";
echo "<input type='hidden' name='rater' value='$rater'>\n";
echo "<input type='hidden' name='UID' value='$UID'>\n";
echo "<input type='hidden' name='Course_Number' value='$Course_Number'>\n";
echo "<input type='hidden' name='op' value='TLInsertPeerEvals'>\n";
echo "<tr><td colspan=\"3\" align=\"center\"><input type=\"Submit\" name=\"TLInsertPeerEvals\" value=\"Submit\"></td></tr>";   
  echo"</table>";
  echo"</form>"; 



and here is the function I use to insert my values.

function TLInsertPeerEvals() {
global $prefix, $db, $authuser;
include("header.php");

print_r($_POST);
die();

foreach($_POST['evalscore'] as $Key => $value) {   
   $UID = $_POST['UID'];
   $Course_Number = $_POST['Course_Number'];
   $rater = $_SERVER['PHP_AUTH_USER'];   
   $evalcomment = $_POST['evalcomment'];
   $sql = "INSERT INTO ".$prefix."_tl_peereval (Eval_ID, UID, SOMS_KEY, rater, evalscore, evalcomment, Course_Number)". "VALUES ('NULL', '$UID', '$Key', '$rater', '$value', '$evalcomment', '$Course_Number')";
$result = $db->sql_query($sql);
if (!$result) {echo("<p>Error performing query: " . mysql_error() . "</p>");}
  }
  header("Location: modules.php?name=Your_Account"); 
  }



As you can see I was using a print_r($_POST); to see what my array contained.



Array ( [evalscore] => Array ( [383] => 7 [350] => 8 [315] => 9 [309] => 10 [306] => 11 [387] => 13 ) [evalcomment] => xxcxxcxcxcxc [UID] => U00347959 [rater] => [Course_Number] => 572 [op] => TLInsertPeerEvals [TLInsertPeerEvals] => Submit )


Looks like each SOMS_KEY has its own evalscore. I only capture the last evalcomment and UID. I'm also missing the rater.

I think I need a multi dimensional array, or structure this differently. Can anybody help?


Back to top Reply with quote
#2   
Telli
Site Admin
Telli has been a member for over 15 year's 15 Year Member
Occupation: Self Employed
Age: 40
Fav. Sports Team: Detroit Red Wings
Website: www.codezhost.com
Status: Offline
Joined: May 26, 2003
1.48 posts per day
Posts: 8090
Points: 494,325
   
Before this loop start a count:


$a = 0;
while($row = $db->sql_fetchrow($getgroupmembers)) {


On ALL of your POST variables add an:


_$a


Example:


name="evalscore_$a"
name="evalcomment_$a"
name="UID_$a"
name="rater_$a"
name="Course_Number_$a"


Add an additional hidden variable that is the total number of scores being recorded

Just before the loop ends increment the counter:


$a++;


Then in your function to insert the data get the total number of scores submitted:


$total_scores = intval($_POST['total_scores']);


Then make a for loop and insert the data:


for ($i = 0; $i <= $total_scores; $i++) {
   $UID = $_POST['UID_'.$i];
      $evalscore = $_POST['evalscore_'.$i];
      $evalcomment = $_POST['evalcomment_'.$i];
      $Course_Number = $_POST['Course_Number_'.$i];
       $rater = $_POST['rater_'.$i];;   
   //do some sql work here
}


You may have to add your SOMS_KEY variable also if its required to pass into the POST.



_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
#3   
Telli
Site Admin
Telli has been a member for over 15 year's 15 Year Member
Occupation: Self Employed
Age: 40
Fav. Sports Team: Detroit Red Wings
Website: www.codezhost.com
Status: Offline
Joined: May 26, 2003
1.48 posts per day
Posts: 8090
Points: 494,325
   
So how did this work out?




_________________
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee. Ezekiel 25:17
Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum