ドロップダウン リストからのユーザーの選択に従って、ここで XML ファイルを作成しようとしています。選択に応じて、クエリを実行して phpmyadmin から特定のデータを取得し、このデータをXML ファイルですが、空の XML ファイルしか取得できません !!
また、PHP を使用して XML タグに記述を追加する方法を知りたいですか? たとえば、このような
<Exam Number="2" Date="3/7/1433" Time="2 hour" Start="8:30 am" />
PHP コード:
<?php
$connectdb = mysql_connect('localhost','root','sara') or die ("Not Connect");
if (!$connectdb) die('Could not connect :'. mysql_errno());
echo "<div align='center' style='direction: ltr' style='position: relative'>
Choose the exam ID that you want to create it: <br />
<form action='createxam.php' method='post'> <select name=\"examID\">
<option value=\"0\">Exam ID </option> \n";
$selestdb = mysql_select_db('iexa', $connectdb) or die ("not selected database");
$qu = mysql_query("SELECT E_No FROM question_bank ") or die ("mysql error");
while ($row = mysql_fetch_assoc($qu))
echo "<option value=\"{$row["E_No"]}\">{$row["E_No"]}</option>\n";
$examID = $_REQUEST['examID'];
echo "</select> </div> <br /> ";
echo "The date of the exam : <textarea name:'Date'></textarea><br />
It will start on (In 24 hours format) : <textarea name:'Start'></textarea><br />
The time of the exam : <textarea name:'Time'></textarea><br />";
echo "<div align='center' style='direction: ltr' style='position: relative'> <input type='submit' value='Create Exam' />
</form></div>";
mysql_close($connectdb);
?>
書き込みファイル:
<?php
$connectdb = mysql_connect('localhost','root','sara', true ) or die ("Not Connect");
if (!$connectdb)
{
die('Could not connect :'. mysql_errno());
}
$selestdb = mysql_select_db('iexa', $connectdb) or die ("not selected database");
$Date = array();
$Start = array();
$Time = array();
$Date['Date']= mysql_real_escape_string($_POST['Date']) ;
$Time['Time']= mysql_real_escape_string($_POST['Time']) ;
$Start['Start']= mysql_real_escape_string($_POST['Start']) ;
if (isset($_POST['examID'])) {
$examID = $_POST['examID'];
}
$query ="INSERT INTO exam (Exam_Number ,Date ,Start ,Time )
VALUES
('$examID', '{$Date['Date']}','{$Start['Start']}','{$Time['Time']}')
";
$cors =mysql_query("SELECT C_ID FROM question_bank WHERE E__No = '$examID'");
$upd ="UPDATE exam SET C_ID=$cors
WHERE Exam_Number='$examID'";
$Exams=mysql_query("SELECT * FROM question_bank WHERE E_No=$examID ");
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "Exams" );
$doc->appendChild( $r );
$sql=mysql_query("SELECT * FROM question_bank WHERE E_No=$examID ");
while ($col = mysql_fetch_assoc($sql)){
foreach( $Exams as $Exam )
{
$b = $doc->createElement( "Exam" );
$E_No = $doc->createElement( "E_No" );
$E_No->appendChild(
$doc->createTextNode( $Exam['E_No'] )
);
$b->appendChild( $E_No );
$C_ID = $doc->createElement( "C_ID" );
$C_ID->appendChild(
$doc->createTextNode( $Exam['C_ID'] )
);
$b->appendChild( $C_ID );
$Question = $doc->createElement( "Question" );
$Question->appendChild(
$doc->createTextNode( $Exam['Question'] )
);
$b->appendChild( $Question );
$r->appendChild( $b );
}
}
echo $doc->saveXML();
$doc->save("mytry.xml");
if (!mysql_query($sql,$connectdb))
{
die ('Error :'.mysql_error());
}
echo "The Exam is created !!";
echo ' <br />
<a href="exam-xml.php" >Create Another Exam</a> <br />
<a href="Instructor.htm">Home</a>
';
mysql_close($connectdb);
?>
作成する XML ファイルは次のとおりです。
<?xml version="1.0"?>
<Exams>
<Exam>
<Exam Number="1" Date="21/6/1433" Time="1 hour" Start="10:00 am" />
<Course Name="Graduation Project" Code="CS 492" Credit="3" />
<Questions ID="1" Type="Multiple-choice" Question="Who has to complete the Graduation Project?" Choice1="All Student." Choice2="Some student." Choice3="Teacher." Choice4="Doctor." Correct="All Student." />
<Questions ID="2" Type="Multiple-choice" Question="When do students begin to work on the Graduation Project?" Choice1="After 2 years from studing." Choice2="Last year." Choice3="High schools" Choice4="Befoer graduation year." Correct="High schools" />
<Student ID="2853641" Name="Maram Abdullah" password="910" />
<Student ID="2853615" Name="Maha Al-soyan" password="911" />
</Exam>
<Exam>
<Exam Number="2" Date="3/7/1433" Time="2 hour" Start="8:30 am" />
<Course Name="Computer Graphics" Code="CS 447" Credit="3" />
<Questions ID="1" Type="Multiple-choice" Question="........ may be defined as a pictorial representation or graphical representation of objects in a computer." Choice1="GUI" Choice2="Computer graphics." Choice3="represent graphics" Choice4="Computer representation." Correct="Computer graphics." />
<Questions ID="2" Type="Multiple-choice" Question="What are the advantages of laser printers?" Choice1="High speed, precision and economy." Choice2="Cheap to maintain." Choice3="Quality printers." Choice4="All of the above." Correct="All of the above." />
<Student ID="2853611" Name="Ro'a Al-turki" password="912" />
<Student ID="2850742" Name="Sara Al-hejily" password="913" />
</Exam>
</Exams>