0

I'm trying to create an XML file here according to the user choice from the drop-down list , depending on the choice I;m running a query to get the specific data from phpmyadmin then I'm trying to put this data in the XML file , but all I get is an empty XML file !!

also , I want to know how to add the descriptions in the XML tags using PHP ? like this one for example

<Exam Number="2" Date="3/7/1433" Time="2 hour" Start="8:30 am" />

PHP Code:

<?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);
    ?>

the writing file :

     <?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);
     ?>

here is the XML file that I want to create :

<?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>
Sara S'h
  • 85
  • 3
  • 9
  • You have `foreach( $Exams as $Exam )` but nowhere earlier in the code do you actually create a variable `$Exams`, so this loop happens 0 times... inside a loop putting the rows from one query into a variable `$col` that you never use. Learn to do one thing at a time... show the form, query the database, and build the document. Don't try to do it all at once when you don't know how or you won't know where it's failing. – Dan Grossman Apr 29 '12 at 08:10
  • possible duplicate of [A simple program to CRUD node and node values of xml file](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) – hakre Apr 29 '12 at 08:12
  • what is your particular problem? – Your Common Sense Apr 29 '12 at 08:40
  • I want to create an XML file as the one above , using the data from $Exams=mysql_query("SELECT * FROM question_bank WHERE E_No=$examID "); but all i got is an empty XML file ! – Sara S'h Apr 29 '12 at 09:02
  • Read my answer please and tell what is your particular problem – Your Common Sense Apr 29 '12 at 12:02
  • I dont understand ur answer -_- , sorry – Sara S'h Apr 29 '12 at 12:19

1 Answers1

2

XML is not a rocket science.
It is as primitive thing as HTML or whatever else formatted text.
One who able to create an HTML table, is surely able to create an XML as well. One don't need fancy DOM parsers. to output simple texts.

Just get desired XML as an example and create it the same way you create HTML.

echo "<exam>".htmlspecialchars($exam)."</exam>\n";

is no more complicated than

echo "<td>".htmlspecialchars($exam)."</td>\n";
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345