allocate/reject student to a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
trtcom1
Forum Newbie
Posts: 14
Joined: Mon Dec 01, 2008 10:51 am

allocate/reject student to a class

Post by trtcom1 »

Hi,
I need somebody to have a look at my code below and help to see what I have done wrong.
I have 3 tables:Student (studentNum,firstName,lastName), course (courseID,courseTotalCapacity), course_allocation (studentNum,courseID).
The AIM is to allocate students to 2 classes (courses 101 and 102) as long as there is available space in the class,if a class is full an error message is shown.
As it is the code adds student to a class even after reaching the courseTotalCapacity.
And sometimes I am also getting duplicate errors.
The else part of the code appears as TRUE whether the class is empty or full.
Please advise!

Code: Select all

<html>
<body>
<center>
 
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<table>
<tr><td>Student ID:</td><td><input type="text" name="studentNum"/></td></tr>
 
<tr><td>FirstName:</td><td><input type="text" name="firtName"/></td></tr>
<tr><td>LastName:</td><td><input type="text" name="lastName"/></td></tr>
 
 
<tr><td>Course ID:</td><td><input type="text" name="courseID"/></td></tr>
 
<tr><td colspan="2" align="centre"><input type="submit" value="SUBMIT"/></td></tr>
</table>
</form>
 
<?php
 
// Connect to the database server
 $dbcnx = @mysql_connect('localhost', 'root', 'geco');
 if (!$dbcnx) {
   exit('<p>Could not connect to the ' .
       'database server.</p>');
 }//end of if-statement
 
 // Select the trainee_allocation database
 if (!@mysql_select_db('trainee_allocation')) {
   exit('<p>Cannot locate the trainee_allocation ' .
       'database.</p>');
 }//end of if-statement
 
 
//Traverse through the COURSE_ALLOCATION table and count the number of entries for
//each user id, aka: how many students per course.
 
$result = mysql_query('SELECT courseID, courseTotalCapacity FROM course WHERE courseID IN (101, 102)') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) 
{
    $course_array[$row['courseID']]['courseTotalCapacity']] = $row['courseTotalCapacity'];
}//end of while-loop
 
$result = mysql_query('SELECT courseID, COUNT(*) AS count FROM course_allocation WHERE courseID IN (101, 102) GROUP BY courseID') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) 
{
    $course_array[$row['courseID']]['count']] = $row['count'];
}//end of while-loop
 
echo "<br />";
 
   $studentNum = $_POST['studentNum'];
   $firtName = $_POST['firtName'];
   $lastName = $_POST['lastName'];
 
   $courseID = $_POST['courseID'];
 
  foreach ($course_array as $key => $value)
  {
      if ($value['count'] < $value['courseTotalCapacity'])
      { 
        $sql = "INSERT INTO student SET
            studentNum='$studentNum',
            firtName='$firtName',
            lastName='$lastName'";
 
 
        if (@mysql_query($sql)) {
             echo '<p>Submitted student has been added.</p>';
        } else {
             echo '<p>Error adding submitted student: ' .
                 mysql_error() . '</p>';
        }
 
 
        $sql1 = "INSERT INTO course_allocation SET
        studentNum='$studentNum',
    
        courseID='$courseID'";
 
        if (@mysql_query($sql1)) {
             echo '<p>Submitted student has been allocated.</p>';
        } else {
             echo '<p>Error allocating submitted student: ' .
             mysql_error() . '</p>';
        }
    }//end of if-statement
    else
    {
        echo 'sorry. you have reached the limit for course #' . $key;
        echo "<br />";
    }//end of else
 
  }//end of foreach-loop 
 
   
?>
 
 
</center>
</body>
</html>
trtcom1
Forum Newbie
Posts: 14
Joined: Mon Dec 01, 2008 10:51 am

Re: allocate/reject student to a class

Post by trtcom1 »

When I run

Code: Select all

print_r($course_array);
inside both while-loops I get the following results but I don't seem to see what is wrong with it.Any advices?

Code: Select all

Array
(
    [101] => Array
        (
            [courseTotalCapacity] => 10
        )
 
)
 
Array
(
    [101] => Array
        (
            [courseTotalCapacity] => 10
        )
 
    [102] => Array
        (
            [courseTotalCapacity] => 10
        )
 
)
 
 
Array
(
    [101] => Array
        (
            [courseTotalCapacity] => 10
            [count] => 9
        )
 
    [102] => Array
        (
            [courseTotalCapacity] => 10
        )
 
)
 
 
Array
(
    [101] => Array
        (
            [courseTotalCapacity] => 10
            [count] => 9
        )
 
    [102] => Array
        (
            [courseTotalCapacity] => 10
            [count] => 9
        )
 
)
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: allocate/reject student to a class

Post by novice4eva »

what you should have done was

Code: Select all

 
if($course_array[$courseID]['count']<$course_array[$courseID]['courseTotalCapacity'])
{
//DO THE INSERT
}
else
{
//SAY CLASS IS FULL
}
 
avoid whole for loop - not needed
trtcom1
Forum Newbie
Posts: 14
Joined: Mon Dec 01, 2008 10:51 am

Re: allocate/reject student to a class

Post by trtcom1 »

Hi novice4eva!
Thank you for the idea about using only the if-statement and dropping the for-loop.
It works fine now.
But I lost a way of letting the user know which class is full.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: allocate/reject student to a class

Post by novice4eva »

Code: Select all

 
else
{
//SAY CLASS IS FULL
 echo 'sorry. you have reached the limit for course #' . $courseID;
        echo "<br />";
}
 
Post Reply