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.

user3455546
  • 5
  • 1
  • 4

2 Answers2

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!

Nazca
  • 112
  • 1
  • 7
  • 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. – Nazca Mar 24 '14 at 17:06
  • 1
    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. – James T Mar 24 '14 at 17:11
  • @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. – Nazca Mar 24 '14 at 17:50
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.'

Alex7
  • 560
  • 3
  • 19
  • 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 – user3455546 Mar 24 '14 at 16:36
  • 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. – Alex7 Mar 24 '14 at 16:38
  • 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? – James T Mar 24 '14 at 16:41
  • 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 Mar 24 '14 at 16:42
  • @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 ""; – Alex7 Mar 24 '14 at 16:51