0

How can I convert the below array to look like a quiz question format:

    Array
(
    [content] => Array
        (
            [@attributes] => Array
                (
                    [id] => 20
                ) [quest] => <FONT FACE="Arial">If K represents kinetic energy, V velocity and T time and these are chosed as the fundamental units then , the unit of surface tension will be</FONT>
            [opt1] => <FONT FACE="Arial"><img src="Images/Phys1/XLA840.gif" WIDTH=56 HEIGHT=18 align="absmiddle"></FONT>
            [opt2] => <FONT FACE="Arial"><img src="Images/Phys1/XLB840.gif" WIDTH=58 HEIGHT=18 align="absmiddle"></FONT>
            [opt3] => <FONT FACE="Arial"><img src="Images/Phys1/XLC840.gif" WIDTH=62 HEIGHT=18 align="absmiddle"></FONT>
            [opt4] => <FONT FACE="Arial"><img src="Images/Phys1/XLD840.gif" WIDTH=58 HEIGHT=18 align="absmiddle"></FONT>
            [ans] => a
            [ans_desc] => <FONT FACE="Arial">
            <P><img src="Images/Phys1/XLAD840.gif" WIDTH=157 HEIGHT=22 align="absmiddle"></P>
            <P><img src="Images/Phys1/XLAD841.gif" WIDTH=312 HEIGHT=44 align="absmiddle"></P>
            <P><img src="Images/Phys1/XLAD842.gif" WIDTH=160 HEIGHT=44 align="absmiddle"></P>
            <P><img src="Images/Phys1/XLAD843.gif" WIDTH=74 HEIGHT=22 align="absmiddle"></P> </FONT>
        )
        )

Format
Question
Option1
Option2
Option3
Option4
<submit button>
<descritpion shown on submission>

Edited by Dani: Fixed formatting

2
Contributors
2
Replies
6
Views
5 Years
Discussion Span
Last Post by javedshaikm
0

You really need to avoid all that formatting within the array itself. Try to make the data as easy as possible. There are many, many ways to do this. If I were to create such an array, I would probably do something like this:

$content[20] = array(
    "q"     => array("If K represents kinetic energy, V velocity and T time and these are chosed as the fundamental units then , the unit of surface tension will be"),
    "opt1"  => array("A - blah","/Phys1/XLA840.gif",56,18),
    "opt2"  => array("B - blah",/Phys1/XLA841.gif",58,18),
    "opt3"  => array("C - blah","/Phys1/XLA842.gif",62,18),
    "opt4"  => array("D - blah","/Phys1/XLA843.gif",58,18),
    "ans"   =>  1,
    "desc"  =>  array("Blah blah blah", "/Phys1/XLA840.gif", 120, 50)
);

The arrays within the may array here relate to text, image, imagewidth, imageheight. Simply check to see if the second array item exists to know whether to include an image in that aspect of your question.

<?php
$key = 20; //probably from session/get or post when quiz being taken
$output = "<p class=\"qnum\">$key.</p>"; //question number (20.)
$output .= "<p class=\"qtext\">{$content[$key]['q'][0]}</p>";
$output .= (isset($content[$key]['q'][1])) ? "<img src=\"{$content[$key]['q'][1]}\" width=\"{$content[$key]['q'][2]}\" height=\"{$content[$key]['q'][3]}\" />" : "";
$output .= "<ol id=\"answers\">";
for($x=1;$x<=4;$x++){
    $output .= "<li>";
    $output .= (isset($content[$key]["opt$x"][1])) ? "<img src=\"" . $content[$key]["opt$x"][1] . "\" width=\"" . $content[$key]["opt$x"][2] . "\" height=\"" . $content[$key]["opt$x"][3] . "\" /><br />" : "";
    $output .= $content[$key]["opt$x"][0] . "</li>";
}
$output .= "</ol>";
?>

You then echo out $output in your page, followed by the form. You could of course have a radiobutton (or checkboxes for multiple answers per question) for each option, so that it all forms part of the form itself. In addition, you may find it easy to store the answer and the description in a session variable. Storing hidden values in forms is a bit awkward as it's visible in the browser's View Source.

Anyway, just a few thoughts.
Do your formatting / styling with CSS.
BTW, I've gone double quote/concatentation happy with this - it'll be neater with single quotes in places. Not tested - off top of head.

Edited by diafol

0

I have tried to create a quiz format question with the below code, but it does't work. Not sure how to get it working for multiple questions...

<?php
function get_answer($input){
if ($input == ($att['ans']) )
    {echo 
    "Correct Answer . {$att['ans_desc']}";
    }
    else {
        echo "Wrong Answer . {$att['ans_desc']}";
    }
}
$xml = <<<XML
<dataroot>
<content id= "1">

                            <quest><![CDATA[<FONT FACE="Arial">Which of the following is not the measure of a physical quantity?</FONT>]]></quest>

                            <opt1><![CDATA[<FONT FACE="Arial">kilogram</FONT>]]></opt1>

                            <opt2><![CDATA[<FONT FACE="Arial">impulse</FONT>]]></opt2>

                            <opt3><![CDATA[<FONT FACE="Arial">energy</FONT>]]></opt3>

                            <opt4><![CDATA[<FONT FACE="Arial">density</FONT>]]></opt4>

                            <ans><![CDATA[a]]></ans>

                            <ans_desc><![CDATA[<FONT FACE="Arial">kilogram is the name of the fundamental unit of mass</FONT>]]></ans_desc>

              </content>



</dataroot>
XML;
$data = new SimpleXMLElement($xml,LIBXML_NOCDATA);
$array = json_decode(json_encode($data),true);


foreach($array as $content => $att){
    echo $att['quest'];


}
echo "<br />";

echo "<form name='quiz'> .
<input type=\"radio\" id='options' onclick = \"getAnswer({$att['ans']})\" />. {$att['opt1']} . <br />" . 
"<input type=\"radio\" id='options' onclick = \"getAnswer({$att['ans']})\"/>. {$att['opt2']} . <br />" . 
"<input type=\"radio\" id='options' onclick = \"getAnswer({$att['ans']})\"/>. {$att['opt3']} . <br />" . 
"<input type=\"radio\" id='options' onclick = \"getAnswer({$att['ans']})\"/>. {$att['opt4']} . <br /> . 
</form>"; 

?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.