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
https://forums.phpfreaks.com/topic/103779-help-getting-rows/
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
https://forums.phpfreaks.com/topic/103779-help-getting-rows/#findComment-531346
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
https://forums.phpfreaks.com/topic/103779-help-getting-rows/#findComment-531350
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
https://forums.phpfreaks.com/topic/103779-help-getting-rows/#findComment-531353
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
https://forums.phpfreaks.com/topic/103779-help-getting-rows/#findComment-531359
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
https://forums.phpfreaks.com/topic/103779-help-getting-rows/#findComment-531362
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
https://forums.phpfreaks.com/topic/103779-help-getting-rows/#findComment-531532
Share on other sites

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.