Jump to content

Way to remove current element from array?


gammaman

Recommended Posts

I have trouble explaining myself, thus it is rare if people answer my posts.

 

See this code.  It takes the first element of the array and does it for all conditions of the foreach.  I want the zero element for the first time the query executes, and the first element the second time the query executes and so on.  If there is no way to do this, is there a way to delete the current element from an array at the end of the foreach, before it goes back to the top.

 

foreach ($Grade as $itm){
         
     mysql_query("Update Rcourse set Grade = '$itm' where Grade < 'A' AND  StudentID='{$_SESSION['checkaddGrades']['stuid']}'")
          or die(mysql_error()); 
     } # end of foreach

sure

 

<?php

for($i=0;$i<count($Grade);$i++){
    $tim = $Grade[$i];
     mysql_query("Update Rcourse set Grade = '$itm' where Grade < 'A' AND  StudentID='{$_SESSION['checkaddGrades']['stuid']}'")
          or die(mysql_error()); 
    // i'm not entirely sure why you need to remove the variable from the array, but if you're sure you do this is how you would do it
   unset($Grade[$i]);
}
?>

Thanks, the idea for removing the element is because the way I had it coded before it was taking the zero element and putting it into all rows that satisfied the query condition.  I need the each successive element to go into the next spot that meets the query condition.

Here is my table see how to of the rows do not have grades.  Ok. Now look at these pieces of code. 

+----------+--------------+-----------+----------+-------+

| CourseID | CourseName  | StudentID | Password | Grade |

+----------+--------------+-----------+----------+-------+

| MTH200  | Calculus 3  |        1 | abC1%a  |    |

| ENG130  | Literature  |        1 | abC1%a  |    |  B+

| PHY300  | Nuclear Phys |        1 | abC1%a  |    |

+----------+--------------+-----------+----------+-------+

 

This is where the grades are entered.  Each of those blank rows is displayed in a table with a text box where the blank grade is.

 

<?php

  $conn=mysql_connect("localhost","fierm","13183");

  if(!$conn){
    echo "failed";
}else{

   mysql_select_db(fierm);

  // $StudentID = $_POST['id'];

   session_start();
   $_SESSION['admin']['admins'];

   $_SESSION['admin']['adminpass'];

   $_SESSION['checkaddGrades']=$_POST;

   $_SESSION['checkaddGrades']['stuid'];
   
  // echo "{$_SESSION['checkaddGrades']['stuid']}";
   
   echo '<table border="1">';
   echo "<tr><th>CourseID</th><th>CourseName</th><th>Grade</th></tr>"; 

   $result=mysql_query("select CourseID,CourseName,Grade From Rcourse
                  WHERE StudentID='{$_SESSION['checkaddGrades']['stuid']}'");

   $cou=mysql_num_rows($result);
   


   if ($cou==0){
      echo "Not in Any Courses";
   }else{
   
   
   while ($row=mysql_fetch_array($result))
   {

     $CourseID= $row['CourseID'];          
     $grade = $row['Grade'];
     $Course=$row['CourseName'];
     
     
   
         
     if (($grade) < "A"){
     $value =  "<form action='updateGrades.php'  method= 'post'>
                 <input name = 'grd[]' size='5' type='text' />";
                
             
     	     }else{ 
          	$value = "$grade";
           
     } #end if...else
       echo "<tr><td>$CourseID</td><td>$Course</td><td>$value</td></tr>";
       
       
   } #end while
   
    // "</form>";
   } #if...else
     
   echo "</table>";
     echo '<input name="submit" type="submit" value="submit" />';
   echo "</form>";   
   echo "<b>Return to Student Page</b>";
   echo "<a href = \"admin.php\">Return to Admin Page</a>";

   


}
?>

 

Here is where they get processed and we are back to the query giving me trouble because instead of taking each grade value in each of the array elements and updating the row.  It only uses element zero for both rows.

<?php

  $conn=mysql_connect("localhost","fierm","13183");

  if(!$conn){
   echo "failed";
}else{
   mysql_select_db(fierm);

  session_start();
   $_SESSION['admin']['admins'];

  // echo "{$_SESSION['admin']['admins']}";

   $_SESSION['admin']['adminpass'];

   $_SESSION['checkaddGrades']['stuid'];

   $Grade = ($_POST['grd']);

   

   print_r($Grade);


   $result=mysql_query("select CourseID,CourseName,Grade From Rcourse
                  WHERE Grade < 'A' AND StudentID='{$_SESSION['checkaddGrades']['stuid']}'");
    $cou=mysql_num_rows($result);
    echo $cou;
   // while ($row=mysql_fetch_array($result))
   // {
     
     //$CourseID= $row['CourseID'];          
     //$grade = $row['Grade'];
     //$Course=$row['CourseName'];
     
   while(!empty($Grade)){           

   //   foreach ($Grade as $itm){

    // for($i=0;$i<count($Grade);$i++){
    //$itm = $Grade[$i];
    //array_shift($Grade[$i]);


         
     mysql_query("Update Rcourse set Grade = '$Grade' where Grade < 'A' AND  StudentID='{$_SESSION['checkaddGrades']['stuid']}'")
          or die(mysql_error()); 
       array_pop($Grade);
       print_r($Grade);
     
   //  } # end of foreach
     } # end of while 
   // } # end of for 
  // }  #end of while     
} #end of conn
?>

 

I have tried several ways but none of them seem to work.   

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.