0

I have a SQL database within which I have a series of different tables. Basically, I am creating an exam day and adding exams to it.

Example:

(examtable) Exam 1

(exams) Physics, Biology, Chemistry, P.E

These both are linked.

Now currently how this works is when I create an exam in the examtable, I can add an unlimited amount of exams to it, but in real life that obviously does not make sense.

So what I am trying to do is have some sort of input when creating the exam that can limit the amount of exams that can be entered to it.

  • So for example if I create Exam 2 and limit input to 3, then only 3 exams can be added to it.

I have tried to find something online but cannot seem to get anywhere, could someone please help?

Thanks.

4
  • is the input from html ? then you can show only limited input options Commented Mar 24, 2014 at 16:31
  • yes input is from html Commented Mar 24, 2014 at 16:33
  • but how would I stop it going into the examtable? If I click on Exam 1 and go to enter another exam, how can I stop it from going in Commented Mar 24, 2014 at 16:33
  • If an exam in examtable can have a variable limit to how may exams are assigned to it then you can just add a database field in examtable to keep count of what that limit is. Commented Mar 24, 2014 at 16:34

2 Answers 2

1

Add another column to your table called mostExams

when creating a new exam just,

$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

$stmnt = $db->query("SELECT `mostExams` FROM `exams` WHERE `classId`='" . $thisClassId . "'");

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $mostExams = $row['mostExams'] ;
}

//php to only allow $mostExams number of exams.

$stmnt = $db->query("SELECT `id` FROM `exams` WHERE `classId`='" . $thisClassId . "'");
if ($stmnt->rowCount() >= $mostExams){
    echo 'You cannot add any more exams.  Change mostExams in the SQL database to allow more.';
}else{
    //HTML form to add an exam

}

You may want to add some code to check and see if you already have some exams and subtract that from most exams. You just need to think outside the box a little bit!

Sign up to request clarification or add additional context in comments.

3 Comments

Though I'd like to mention as well, it doesn't seem smart to limit the number of exams. Maybe you want to add new exams later on? Or maybe you want to edit exams? It seems smart to allow an unlimited number of exams but only display active exams. You might consider adding a bool active column to your table and checking to see if its true or false. What if curriculum changes and you need 6 exams instead of 5? instead of changing your source code you could just add a new exam and make it active. Then when curriculum changes again and your only allowed 4, you can just deactivate 2.
From what I got from the question examtable rows actually represent days that exams are taken on. Therefor due to the limited number of hours within a day (and the variable time an exam may be allocated to complete) there must be a limit.
@JamesT Oh, that makes sense. But still, you have plenty of variables there that would have it make sense to allow an unlimited number of exams. What if next week you get students? Your neighboring teacher has a stroke and you need to work with his class for a week because there are no subs available. The institute of Shanghai asks you to give your exam to their student body. I dont see a good reason to limit it, while I could rattle on for ever with reasons not to.
0

I don't understand very well your question but here it goes:

You can use sql in php to count the number of exams from examstable: 'SELECT COUNT(*) FROM examstable;' And then in PHP : 'if ($no_exams > 3 ) { echo "No more exams can be added."} else insert into the database the exam.'

5 Comments

This is sort of what I am looking for but I wouldn't hard code it in, I want to try to limit it when I create it. So if I create an exam and limit it to 5, I will only be able to add 5 exams in
What's the point of doing that? I don't think something like that exists. The only way to interact with the inputs and outputs is the php page you create for this so you make a simple function to verify it.
I think he wants the limit (i.e. the 3 in your answer) to be variable. So probably the limit needs to be put into the DB of examtable?
But where would I set a limit? If on Exam2 there is only going to be 5 exams how can I verify that only 5 are entered and not 10
@user3455546 You look for a complicated solution. If you use php to verify the count of the rows you can easily block the input in the database. something like if ($count>=5) echo "The exam list is full"; else echo "<input type='text' name='nameofexam'>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.