gammaman Posted April 27, 2008 Share Posted April 27, 2008 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 Link to comment https://forums.phpfreaks.com/topic/103157-way-to-remove-current-element-from-array/ Share on other sites More sharing options...
p2grace Posted April 27, 2008 Share Posted April 27, 2008 Try using a for statement instead with the unset function Link to comment https://forums.phpfreaks.com/topic/103157-way-to-remove-current-element-from-array/#findComment-528419 Share on other sites More sharing options...
gammaman Posted April 27, 2008 Author Share Posted April 27, 2008 I am sorry, I am very new to php and have never seen the unset statement. Could you show me what that would look like, along with the for loop. Thanks Link to comment https://forums.phpfreaks.com/topic/103157-way-to-remove-current-element-from-array/#findComment-528422 Share on other sites More sharing options...
p2grace Posted April 27, 2008 Share Posted April 27, 2008 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]); } ?> Link to comment https://forums.phpfreaks.com/topic/103157-way-to-remove-current-element-from-array/#findComment-528425 Share on other sites More sharing options...
gammaman Posted April 27, 2008 Author Share Posted April 27, 2008 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. Link to comment https://forums.phpfreaks.com/topic/103157-way-to-remove-current-element-from-array/#findComment-528427 Share on other sites More sharing options...
gammaman Posted April 27, 2008 Author Share Posted April 27, 2008 Your code, with or without the unset is still putting the zero element value into all rows that meet the query condition. Link to comment https://forums.phpfreaks.com/topic/103157-way-to-remove-current-element-from-array/#findComment-528434 Share on other sites More sharing options...
p2grace Posted April 27, 2008 Share Posted April 27, 2008 Let me see your array along with details of exactly what it is you're trying to save Link to comment https://forums.phpfreaks.com/topic/103157-way-to-remove-current-element-from-array/#findComment-528436 Share on other sites More sharing options...
gammaman Posted April 27, 2008 Author Share Posted April 27, 2008 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. Link to comment https://forums.phpfreaks.com/topic/103157-way-to-remove-current-element-from-array/#findComment-528453 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.