|
|
|
This is nice Trivia Quiz program using XML, XSLT and PHP. The XML is transformed and displayed as HTML using XSLT. Then the submitted answers are handled by PHP. Nice coding. Will help those who want to know the power of these three.
One more thing, in the XSL file, if you add this line in first section <xsl:for-each select="questions/question[2]"> then it will display only 2nd number question. No need to parse by PHP.
I hope you would like it and vote for me.
--Hasin
handler.php
| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Total marks</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<?php
//print_r($_POST);
$loop_counter = count($_POST)/2 +1;
$marks=0; $wrong =0;
for ($i=1;$i<$loop_counter;$i++)
{
if($_POST['qon'.$i]==$_POST['cor'.$i]) $marks++;
}
echo "You Obtained : ".$marks."<br>";
?>
<body>
</body>
</html> | |
question.xml
| <?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="question.xsl"?>
<questions>
<question>
<number>1</number>
<text>What is the capital of Bangladesh</text>
<answers>
<answer num="1">Dhaka</answer>
<answer num="2">Manilla</answer>
<answer num="3">Delhi</answer>
<answer num="4">Piong Young</answer>
<correct>1</correct>
</answers>
</question>
<question>
<number>2</number>
<text>What is Zebra Crossing</text>
<answers>
<answer num="1">A kind of Zebra</answer>
<answer num="2">A system for pedestrian to cross the road</answer>
<answer num="3">A tool</answer>
<answer num="4">Traffic system</answer>
<correct>2</correct>
</answers>
</question>
<question>
<number>3</number>
<text>What is the highest mountain in Mars</text>
<answers>
<answer num="1">Blogers</answer>
<answer num="2">Mount Olimpus</answer>
<answer num="3">Icarus</answer>
<answer num="4">Rocky</answer>
<correct>2</correct>
</answers>
</question>
<question>
<number>4</number>
<text>Who is the inventor of Theory of Gravity</text>
<answers>
<answer num="1">Einstein</answer>
<answer num="2">Wilver Smith</answer>
<answer num="3">Willium Rontgen</answer>
<answer num="4">Newton</answer>
<answer num="5">George Lucas</answer>
<correct>4</correct>
</answers>
</question>
</questions> | |
question.xsl
| <?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<title>A Trivia using XML, XSLT and PHP</title>
<body>
<form action="handler.php" method="post">
<xsl:for-each select="questions/question">
<p>
<b><xsl:value-of select="number" />. </b>
<xsl:apply-templates/>
</p>
</xsl:for-each>
<input type="submit" value="Submit"/>
</form>
</body>
</xsl:template>
<xsl:template match="question/text">
<font color="#FF0000"><xsl:value-of select="."/><br /></font>
</xsl:template>
<xsl:template match="answers">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="answer">
<xsl:variable name="qno"><xsl:value-of select="../../number"/></xsl:variable>
<font color="#000099">
<input type="radio">
<xsl:attribute name="name"><xsl:value-of select="concat('qon',$qno)"/></xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="@num"/></xsl:attribute>
</input>
<xsl:value-of select="."/>
<br/>
</font>
</xsl:template>
<xsl:template match="correct">
<xsl:variable name="qno"><xsl:value-of select="../../number"/></xsl:variable>
<input type="hidden">
<xsl:attribute name="name"><xsl:value-of select="concat('cor',$qno)"/></xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute>
</input>
</xsl:template>
<xsl:template match="number">
<font color="#000099"></font>
</xsl:template>
</xsl:stylesheet> | | |
|
| XPath for PHP without the DOM XML extension Categories : DOM XML, XML, XSLT, PHP Classes, PHP | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | XML To Array Categories : PHP, PHP Classes, XML, Arrays | | | A very basic and fast XML parser Categories : PHP, PHP Classes, XML | | | RSS parser using PHP5 and simpleXML Categories : Rich Site Summary (RSS), PHP, XML | | | Glossword - glossary compiler Categories : Content Management, PHP, MySQL, XML | | | XML easy parser Categories : PHP, XML | | | Simple newsreader script Categories : PHP, XML, Rich Site Summary (RSS) | | | Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +) Categories : PHP, Ecommerce, XML, Web Services, CURL | | | MySQL or SQL Query to XML Output Categories : PHP, MySQL, XML, Databases | | | XML Menu Categories : PHP, PHP Classes, Navigation, XML, XSL | | | xslt_closelog -- Clear the logfile for a given instance of Sablotron Categories : PHP, PHP Functions, XSLT | | | Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript. Categories : PHP, Java Script, DOM XML, XML, Arrays | | | On-the-fly drop down menu from a txt or xml file Categories : PHP, XML, HTML and PHP | | | Freshmeat.net XML-RPC - This class is meant to query Freshmeat for information about registered projects. Categories : PHP, PHP Classes, XML, Web Services | |
| | | | matthew waygood wrote : 1210
Got a link to see it working online? as I cant see xsl running locally
| | | | Hasin Hayder wrote :1211
Here is a working example of the above project.
http://www.srishty.org/xslt/question.xml
Pls browse using IE, Netscape and Opera
Thanks
--Hasin
| |
|
|
|