Jump to content

Help getting rows


gammaman

Recommended Posts

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>";
}
?>

 

 

 

Link to comment
Share on other sites

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){
?>

Link to comment
Share on other sites

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.  :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.";
}
}
?>

Link to comment
Share on other sites

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>";
}
?>

 

 

 

 

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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. ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.