Dynamic Review Rule System PHP & MySQL

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
barneyvaughan
Forum Newbie
Posts: 2
Joined: Fri May 13, 2016 8:21 am

Dynamic Review Rule System PHP & MySQL

Post by barneyvaughan »

So I've been tasked with a type of review system that has 4 types of reviews. The reviews will have 15-20 questions each in them and the questions will range from basic "Yes/No" through to a Date and Location answer. The system works on rules though where it takes into account the Yes/No answers and funnels them into a rule system.

The questions are stored in the database and pulled from it, I feel like I could do the same for the rules. In all of the prototypes I've been hard coding these rules but I wanted to move to a more user friendly dynamic method.

For example you do a review on What Movies You've Seen?:

Q1) Godfather Y/N
Q2) Journey To The Center Of The Earth Y/N
Q3) Snow White Y/N
Q4) What's your favourite film? Text Entry

Now lets say the Rules are:
Rule 1 = If Q1 & Q2 are answered both Yes and the Q3 is No - GOTO Page 2.
Rule 2 = If Q1 & Q3 Are Answered Yes And Q2 are No - GOTO Page 3.

There would never be any need to take note of textboxes or anything like that, just Yes/No Radio buttons which to me just means it's a case of having a lot of 1's & 0's saved.

My question is really how would this be displayed in a front end actionable system and how would it be saved in a MySQL Database nicely so that it's retrievable later. I know it's really broad question.

I was going to have a table where you had a set of "Pages" that were given a start question value and an end question value eg:
http://imgur.com/sBtC8TV

But that denormalizes the rule data and means I'll have to split it whenever I want to edit. I don't mind that too much as it's not really data to start with and it seems simple enough but I just wanted some input on this as I don't want to jump in for it to suddenly dawn on me that there's a better way!

Cheers in advance
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Dynamic Review Rule System PHP & MySQL

Post by Christopher »

If all you need to deal with are Yes and No, then all you need is a list of the questions that need to be Yes and a list of those that need to be No. So to restaye your rules:

Rule 1: Yes Q1, Q2; No Q3; GOTO Page 2;
Rule 2: Yes Q1, Q3; No Q2; GOTO Page 3;

The most condensed way to store those would be something like:

Rule 1: 1,2;3;2
Rule 2: 1,3;2;3

To parse

Code: Select all

$rule = explode(';', $row['rule']);
$yes = explode(',', $rule[0]);
$no = explode(',', $rule[1]);
$goto = explode(',', $rule[2]);
You could then generate SQL from $yes and $no. Then use $goto if you get back count($yes) + count($no) rows.
(#10850)
Post Reply