gammaman Posted May 1, 2008 Share Posted May 1, 2008 I know I already posted this but I want to try to rexplain what I want to try and do. I have to queries, one assigned to result and the other result1. I am fetching the rows from result and storing the CourseID filed into a variable each pass of the while loop. I also fetch the rows from result one and store a field called CourseID into a $variable. What I am trying to do is check each pass through the loop if those to variables are equal. You will see in my code my attempt but it does not work. <?php include("code.php"); $conn=mysql_connect("localhost","fierm","13183"); if(!$conn){ echo "failed"; }else{ mysql_select_db("fierm"); echo "Choose a Course to Register"; $result=mysql_query("select CourseID,CourseName,SeatLimit from Course"); $result1=mysql_query("Select CourseID,StudentID from Rcourse where StudentID='{$_SESSION['user']}'"); echo "<table border = \"1\">"; echo "<tr><td>CourseID</td><td>CourseName</td><td>Seats</td><td>Register</tr>"; while($row=mysql_fetch_array($result)) { $CourseID = $row['CourseID']; $CourseName= $row['CourseName']; $SeatLimit= $row['SeatLimit']; $r=mysql_fetch_array($result1)) $Course = $r['CourseID']; foreach($row as $value){ if($CourseID ==$Course){ $value = "Already Took Course"; } } elseif($SeatLimit == 0){ $value = "Full"; } else{ $value="<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; } //$strUrl = "<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; echo "<tr><td>$CourseID</td><td>$CourseName</td><td>$SeatLimit</td><td>$value</td></tr>\n"; } echo "</table>"; } ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 1, 2008 Share Posted May 1, 2008 Any errors, no information being pulled, not loading...? What's the actual problem? Quote Link to comment Share on other sites More sharing options...
gammaman Posted May 1, 2008 Author Share Posted May 1, 2008 It is only woking meaning finding a match for the first occurance, and not the rest. Quote Link to comment Share on other sites More sharing options...
benphp Posted May 1, 2008 Share Posted May 1, 2008 You need to cycle through your array - or not. I'm trying to figure out where this else goes - else{ $value="<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; <?php <?php include("code.php"); $conn=mysql_connect("localhost","fierm","13183"); if(!$conn){ echo "failed"; } else { mysql_select_db("fierm"); echo "Choose a Course to Register"; $result=mysql_query("select CourseID,CourseName,SeatLimit from Course"); $result1=mysql_query("Select CourseID,StudentID from Rcourse where StudentID='{$_SESSION['user']}'"); echo "<table border = \"1\">"; echo "<tr><td>CourseID</td><td>CourseName</td><td>Seats</td><td>Register</tr>"; while($row=mysql_fetch_array($result)) { $CourseID = $row['CourseID']; $CourseName= $row['CourseName']; $SeatLimit= $row['SeatLimit']; $r=mysql_fetch_array($result1)) $Course = $r['CourseID']; foreach($row as $value){ if($CourseID==$Course){ $value = "Already Took Course"; } } elseif($SeatLimit == 0){ $value = "Full"; } //foreach else { $value="<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; } //????????? //$strUrl = "<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; echo "<tr><td>$CourseID</td><td>$CourseName</td><td>$SeatLimit</td><td>$value</td></tr>\n"; } //while echo "</table>"; } //if(!$conn){ ?> Quote Link to comment Share on other sites More sharing options...
gammaman Posted May 1, 2008 Author Share Posted May 1, 2008 Yeah I know your right about needing to cycle through the array, however your code does not work. I think it is because I have an if, elseif,else and you have me putting a foreach before the if and closing the foreach just after the if but before the elseif which if I am not mistaken is why I am getting an error and the page is loading blank. Quote Link to comment Share on other sites More sharing options...
gammaman Posted May 1, 2008 Author Share Posted May 1, 2008 That else is a link to another page, so basically if the if condition is not met which means that $CourseID and $Course it checks the elseif to see if there are available seats and if not it displays full, the else will display a link that the user can click to sign up for a course. Quote Link to comment Share on other sites More sharing options...
gammaman Posted May 1, 2008 Author Share Posted May 1, 2008 maybe I need a foreach before the while loop ??? Quote Link to comment Share on other sites More sharing options...
benphp Posted May 1, 2008 Share Posted May 1, 2008 What about doing it this way: <?php $select = "SELECT Course.CourseID, Course.SeatLimit FROM Course, Rcourse WHERE Course.CourseID = Rcourse.CourseID LIMIT 1 "; $result = mysql_query($select) or trigger_error("SQL", E_USER_ERROR); if($row = mysql_fetch_row($result)) { print "You have already taken this course."; } else { $SeatLimit = $row['SeatLimit']; if ($SeatLimit == 0) { print "Class is full."; } else { print "Register me."; } } ?> Quote Link to comment Share on other sites More sharing options...
gammaman Posted May 1, 2008 Author Share Posted May 1, 2008 I would love to except my teach won't let us do i that way. However I now have some progress. Instead of just getting the first occurance (match) it is simply saying "already took course" on the first three rows period. <?php include("code.php"); $conn=mysql_connect("localhost","fierm","13183"); if(!$conn){ echo "failed"; }else{ mysql_select_db("fierm"); echo "Choose a Course to Register"; $result=mysql_query("select CourseID,CourseName,SeatLimit from Course"); $result1=mysql_query("Select CourseID,StudentID from Rcourse where StudentID='{$_SESSION['user']}'"); $cou=mysql_num_rows($result1); echo "<table border = \"1\">"; echo "<tr><td>CourseID</td><td>CourseName</td><td>Seats</td><td>Register</tr>"; while($row=mysql_fetch_array($result)) { $CourseID = $row['CourseID']; $CourseName= $row['CourseName']; $SeatLimit= $row['SeatLimit']; $r=mysql_fetch_array($result1); $Course = $r['CourseID']; if(($cou > 0) && ($courseID==$course)){ $value = "Already Took Course"; } elseif($SeatLimit == 0){ $value = "Full"; } else{ $value="<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; } $cou--; //$strUrl = "<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; echo "<tr><td>$CourseID</td><td>$CourseName</td><td>$SeatLimit</td><td>$value</td></tr>\n"; } echo "</table>"; } ?> Quote Link to comment Share on other sites More sharing options...
sasa Posted May 2, 2008 Share Posted May 2, 2008 try <?php include("code.php"); $conn = mysql_connect("localhost","fierm","13183"); if(!$conn) { echo "failed"; } else { mysql_select_db("fierm"); echo "Choose a Course to Register"; $result=mysql_query("select CourseID,CourseName,SeatLimit from Course"); $result1=mysql_query("Select CourseID,StudentID from Rcourse where StudentID='{$_SESSION['user']}'"); // put all studen's courses in array while ($r = mysql_fetch_array($result1)) $Courses[] = $r['CourseID']; echo "<table border = \"1\">"; echo "<tr><td>CourseID</td><td>CourseName</td><td>Seats</td><td>Register</tr>"; while($row=mysql_fetch_array($result)) { $CourseID = $row['CourseID']; $CourseName= $row['CourseName']; $SeatLimit= $row['SeatLimit']; /* $r=mysql_fetch_array($result1)) $Course = $r['CourseID']; foreach($row as $value){ if($CourseID ==$Course){ */ if (in_array($CourseID, $Courses)) $value = "Already Took Course"; // check is course in student courses array elseif($SeatLimit == 0) $value = "Full"; else $value="<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; //$strUrl = "<a href=\"checkRegCourses.php?ci=$row[0]&cn=$row[1]\">Register</a>"; echo "<tr><td>$CourseID</td><td>$CourseName</td><td>$SeatLimit</td><td>$value</td></tr>\n"; } echo "</table>"; } ?> Quote Link to comment Share on other sites More sharing options...
gammaman Posted May 2, 2008 Author Share Posted May 2, 2008 Thank you, I should have been smart enough to realize that doing it the way I was, was wrong. I realized after you showed me the correct way that with the way I was doing it, the pointer in my program was at the end of the file and had no way back to the top. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.