Simple Conditional Comparison with Text Result

Introduction

Conditional comparisons are one of the most versatile functions in MGI. The conditional tags allow you to perform logical comparisons between two values and display a result based on whether the comparison is true or false.

The mgiInlineIf tag is used when there is only one conditional comparison and the result is a string of text. The mgiIf tag is used when there are multiple conditional comparisons or the result of the comparison contains code in addition to a string of text (e.g., MGI tags). The mgiSwitch tag is used when there are multiple comparisons and multiple matching results. Text, HTML tags, and MGI tags may be included in the result of an mgiIf or mgiSwitch comparison. See the advanced tutorials for examples of mgiIf and mgiSwitch.

More often than not, one of the values in a conditional comparison is embedded in the tag's parameter from another source (page or site variables, post arguments, path arguments, etc.). Read more about embedding.

In this example, a conditional comparisons is used to determine a passing or failing grade from an online quiz. The example consists of a hard-coded quiz form and a grade processing page. On the processing page, the numeric quiz score is tabulated from the quiz answers using an mgiMath tag and the total quiz score is compared with a the passing grade to determine which text displays.

MGI Tags

Steps

  1. Create a quiz form.
  2. Create a grade processing page and open it in a text editor.
  3. Insert the mgiInlineIf tag.
  4. Save the grade processing page.
  5. FTP the quiz form and grade processing page to the web server running MGI.
  6. View the quiz form in a web browser and take the quiz.


Step 1: Create a quiz form.

Create a quiz page named "quiz.mgi" with form elements. In this example, the quiz is multiple choice and is constructed of radio buttons. Name each form element uniquely and assign a grade value to each answer. Enclose all form elements with HTML <FORM> tags and post the form to the grade processing page (grade.mgi)

This is an example quiz form.
<FORM action="grade.mgi" method="post">

<H2><CENTER>Grade 1 Quiz</CENTER></H2>

<P><CENTER><FONT COLOR="#006600" SIZE="-1">
<mgiDate></FONT></CENTER></P>

<P>Student Name: 
<INPUT NAME="StudentName" TYPE="text" SIZE="30"></P>

<P>1. What is the result of 1 + 2?</P>

<P><INPUT TYPE="radio" VALUE="0" NAME="Q1">1<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q1">2<BR>
<INPUT TYPE="radio" VALUE="20" NAME="Q1">3<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q1">4</P>

<P>2. What noise does a cow make?</P>

<P><INPUT TYPE="radio" VALUE="0" NAME="Q2">oink<BR>
<INPUT TYPE="radio" VALUE="20" NAME="Q2">moo<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q2">bark<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q2">meow</P>

<P>3. <FONT COLOR="#ff0000">
What color is this sentence?</FONT></P>

<P><INPUT TYPE="radio" VALUE="0" NAME="Q3">green<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q3">blue<BR>
<INPUT TYPE="radio" VALUE="20" NAME="Q3">red<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q3">white</P>

<P>4. What holiday is on December 25th?</P>

<P><INPUT TYPE="radio" VALUE="20" NAME="Q4">Christmas<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q4">Halloween<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q4">Thanksgiving<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q4">Easter</P>

<P>5. What is your teacher's name?</P>

<P><INPUT TYPE="radio" VALUE="0" NAME="Q5">Mrs. Smith<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q5">Mrs. Arnold<BR>
<INPUT TYPE="radio" VALUE="0" NAME="Q5">Ms. Pepperdyne<BR>
<INPUT TYPE="radio" VALUE="20" NAME="Q5">Mr. Clemmons</P>

<P><INPUT NAME="name" TYPE="submit" 
VALUE="Submit Quiz for Grading">
<INPUT NAME="name" TYPE="reset" 
VALUE="Reset Quiz Answers">

</FORM>

Step 2: Create a grade processing page and open it in a text editor.

Create a page named "grade.mgi" to process the quiz grade and display the passing or failing grade message. Open the grade processing page in a text editing program that allows you to view and modify the HTML and code of the page.

Step 3: Insert the mgiInlineIf tag.

Insert your cursor in the HTML where you want the quiz grade result to display. Enter the mgiMath tag with mgiPostArgument tags to calculate the total quiz score. Set the total quiz score value in a variable using the mgiSet tag. Below the variable, enter the mgiInlineIf tag, lhs parameter, relationship parameter, rhs parameter, then parameter and else parameter.
 
In the lhs parameter, embed the quiz score total from the variable using an mgiGet tag. In the relationship parameter, enter the type of comparison to perform. In this case, test whether the lhs (left-hand side) value is greater than or equal to the rhs (right-hand side) value. In the rhs parameter, enter the passing quiz score. In the "then" parameter, enter the text that displays if the student's quiz score is greater than or equal to the passing quiz score (e.g., "Congratulations, you pass and receive 1 gold quiz star"). In the "else" parameter, enter the text that displays if the student's quiz score is less than the passing quiz score (e.g., "You did not pass. Please see the teacher.").
 
This is an example grade processing page.
<H2><CENTER>Grade 1 Quiz Score</CENTER></H2>

<P><CENTER><FONT COLOR="#006600" SIZE="-1">
<mgiDate></FONT></CENTER></P>

<mgiSet name="QuizScore">
<mgiMath resultPrecision="0">
<mgiPostArgument name="Q1">+<mgiPostArgument name="Q2">+
<mgiPostArgument name="Q3">+<mgiPostArgument name="Q4">+
<mgiPostArgument name="Q5">
</mgiMath>
</mgiSet>

<CENTER>

<P><mgiPostArgument name="StudentName">, 
your quiz score is <mgiGet name="QuizScore">.</P>

<P>
<mgiInlineIf lhs={mgiGet name="QuizScore"} 
relationship="greaterThanOrEqualTo" rhs="60" 
then="Congratulations, you pass and 
receive 1 gold quiz star." 
else="You did not pass. Please see the teacher.">
</P>

</CENTER>

Step 4: Save the grade processing page.

Save the changes you have made to the grade processing page.

Step 5: FTP the quiz form and grade processing page to the web server running MGI.

Upload the quiz form and grade processing page from your local computer to the web server using an FTP program.

Step 6: View the quiz form in a web browser and take the quiz.

View the quiz form in a web bowser. Complete and submit the quiz. If your score is 60 or greater, the message "Congratulations, you pass and receive 1 gold quiz star." displays. If your score is less than 60, the message "You did not pass. Please see the teacher." displays.


[Return to the If, Then, Else Menu]


[Understanding MGI Menu] [Using MGI Menu] [Referencing MGI Menu]


[MGI Guides Main Menu] [User Guide Main Menu]