What is the reason for this outputted XML file

<?xml version="1.0"?>
<table>
  <order>
    <id/>
    <name/>
    <age/>
  </order>
<order><id>1</id><name>baven</name><age>44</age></order><order><id>1</id><name>baven</name><age>44</age></order></table>

this is my database.xml page

my full code is:

<?php
$url='database.xml';
$xml = simplexml_load_file($url);
?>

<html>
    <head>

        <script type="text/javascript" src="jquery-latest.js"></script> 
        <script type="text/javascript" src="jquery.tablesorter.js"></script>

        <script  type="text/javascript">   
        $(document).ready(function() 
    { 
         $("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} ); 
    } 
); 
 </script>
    </head>

    <body>
        <div align="right">
            <form action="parser1.php" method="POST">
            <table border="0">
                <tr>
                    <td>id </td>
                    <td><input type="text" name="id"></td>
                </tr>
                <tr>
                    <td>name</td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td>age</td>
                    <td><input type="text" name="age"></td>
                </tr>
                <tr>
                    <td></td>
                    <td align="right"><input type="submit"  value="submit"></td>
                </tr>
            </table>
            </form>
        </div>
        <div style="cursor: pointer">
     <table border='1' id='myTable' class='tablesorter' name='myTable'>
         <thead>
        <tr>
                <th>id</th>
                <th>name</th>
                <th>age</th>

        </tr>
           </thead>
           <tbody>
               <?php
               foreach($xml as $xmls) // loop through our books
{
                   ?>
               <tr>
        <?php echo "<td>".$xmls->id."</td>";?>
              <?php echo "<td>".$xmls->name."</td>";?>
              <?php echo "<td>".$xmls->age."</td>";?>
               </tr>
               <?php
                }
               ?>
           </tbody>

                  <?php

$id =(isset($_POST['id']) ? strip_tags($_POST['id']) : '');
$name = (isset($_POST['name']) ? strip_tags($_POST['name']) : '');
  $age =(isset($_POST['age']) ? strip_tags($_POST['age']) : '');

  $doc= new DOMDocument();
$doc->formatOutput = true;
$doc->load("database.xml");
if ($doc->getElementsByTagName("table")->length == 0) {


 $library = $doc->createElement("table");
  $doc->appendChild($library);
$book = $doc->createElement("order");

$prop = $doc->createElement('id', $id);
    $book->appendChild($prop);
$prop = $doc->createElement("name", $name);
    $book->appendChild($prop);
                $prop = $doc->createElement('age', $age);
                $book->appendChild($prop);
                $library->appendChild($book);
                    $doc->save('database.xml');
}
 else {
    $library = $doc->documentElement;
    $book = $doc->createElement('order');

$prop = $doc->createElement('id', $id);
    $book->appendChild($prop);
$prop = $doc->createElement('name', $name);
    $book->appendChild($prop);
                $prop = $doc->createElement('age', $age);
                $book->appendChild($prop);
                $library->appendChild($book);
                    $doc->save('database.xml'); 
}

?>

<!--           <thead>
        <tr>
                <th>id</th>
                <th>name</th>
                <th>age</th>

        </tr>
           </thead>-->
<!--           <tbody>
               </tbody>-->
     </table>
        </div>

    </body>
       </html>          

Any help would be appreciated...

Recommended Answers

All 4 Replies

What is the reason for this outputted XML file

<?xml version="1.0"?>
<table>
  <order>
    <id/>
    <name/>
    <age/>
  </order>
<order><id>1</id><name>baven</name><age>44</age></order><order><id>1</id><name>baven</name><age>44</age></order></table>
this is my database.xml page

my full code is:

<?php
$url='database.xml';
$xml = simplexml_load_file($url);
?>

<html>
    <head>

        <script type="text/javascript" src="jquery-latest.js"></script> 
        <script type="text/javascript" src="jquery.tablesorter.js"></script>

        <script  type="text/javascript">   
        $(document).ready(function() 
    { 
         $("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} ); 
    } 
); 
 </script>
    </head>

    <body>
        <div align="right">
            <form action="parser1.php" method="POST">
            <table border="0">
                <tr>
                    <td>id </td>
                    <td><input type="text" name="id"></td>
                </tr>
                <tr>
                    <td>name</td>
                    <td><input type="text" name="name"></td>
                </tr>
                <tr>
                    <td>age</td>
                    <td><input type="text" name="age"></td>
                </tr>
                <tr>
                    <td></td>
                    <td align="right"><input type="submit"  value="submit"></td>
                </tr>
            </table>
            </form>
        </div>
        <div style="cursor: pointer">
     <table border='1' id='myTable' class='tablesorter' name='myTable'>
         <thead>
        <tr>
                <th>id</th>
                <th>name</th>
                <th>age</th>

        </tr>
           </thead>
           <tbody>
               <?php
               foreach($xml as $xmls) // loop through our books
{
                   ?>
               <tr>
        <?php echo "<td>".$xmls->id."</td>";?>
              <?php echo "<td>".$xmls->name."</td>";?>
              <?php echo "<td>".$xmls->age."</td>";?>
               </tr>
               <?php
                }
               ?>
           </tbody>

                  <?php

$id =(isset($_POST['id']) ? strip_tags($_POST['id']) : '');
$name = (isset($_POST['name']) ? strip_tags($_POST['name']) : '');
  $age =(isset($_POST['age']) ? strip_tags($_POST['age']) : '');

  $doc= new DOMDocument();
$doc->formatOutput = true;
$doc->load("database.xml");
if ($doc->getElementsByTagName("table")->length == 0) {


 $library = $doc->createElement("table");
  $doc->appendChild($library);
$book = $doc->createElement("order");

$prop = $doc->createElement('id', $id);
    $book->appendChild($prop);
$prop = $doc->createElement("name", $name);
    $book->appendChild($prop);
                $prop = $doc->createElement('age', $age);
                $book->appendChild($prop);
                $library->appendChild($book);
                    $doc->save('database.xml');
}
 else {
    $library = $doc->documentElement;
    $book = $doc->createElement('order');

$prop = $doc->createElement('id', $id);
    $book->appendChild($prop);
$prop = $doc->createElement('name', $name);
    $book->appendChild($prop);
                $prop = $doc->createElement('age', $age);
                $book->appendChild($prop);
                $library->appendChild($book);
                    $doc->save('database.xml'); 
}

?>

<!--           <thead>
        <tr>
                <th>id</th>
                <th>name</th>
                <th>age</th>

        </tr>
           </thead>-->
<!--           <tbody>
               </tbody>-->
     </table>
        </div>

    </body>
       </html>          

Any help would be appreciated...

also i want to eliminate duplicate elements in xml file using php..In my xml file i am getting as

<?xml version="1.0"?>
<Order>
  <title>
    <id>1</id>
    <name>hacker</name>
    <age>23</age>
  </title>
<title>
<id>2</id><name>jack</name><age>45</age></title>
<title><id>2</id><name>jack</name><age>45</age></title>
<title><id>3</id><name>houston</name><age>36</age></title>
<title><id>3</id><name>houston</name><age>36</age>
</title>
</Order>

output them into an array first before outputting them to the page, eg you must have something like this looping them out onto the page:

foreach($row as $v){
echo "<order><id>{$v['id']}</id><name>{$v['name']}</name><age>{$v['age']}</age></order>";
}

change it to:

$xmldata = array();
foreach($row as $v){
$xmldata[$v['id']} = $v;
}
foreach($xmldata as $v){
echo "<order><id>{$v['id']}</id><name>{$v['name']}</name><age>{$v['age']}</age></order>";
}

that way the duplicate id's will overwrite each other and you will only get 1 per id

output them into an array first before outputting them to the page, eg you must have something like this looping them out onto the page:

foreach($row as $v){
echo "<order><id>{$v['id']}</id><name>{$v['name']}</name><age>{$v['age']}</age></order>";
}

change it to:

$xmldata = array();
foreach($row as $v){
$xmldata[$v['id']} = $v;
}
foreach($xmldata as $v){
echo "<order><id>{$v['id']}</id><name>{$v['name']}</name><age>{$v['age']}</age></order>";
}

that way the duplicate id's will overwrite each other and you will only get 1 per id

the above code will not output the result

it displays
Trying to get property of non-object
Undefined variable: row

i want to display the result in a table...not as an array
table contains field names like id,name,age.I had put my codes in my first post.Any help would be appreciated.

the above code will not output the result

it displays
Trying to get property of non-object
Undefined variable: row

i want to display the result in a table...not as an array
table contains field names like id,name,age.I had put my codes in my first post.Any help would be appreciated.

I didnt write it to match your code, i've never done much OOP programming in php except one shopping basket i had to edit once so i saw that and just thought id give a general example, $row was meant to be replaced by whatever loop you have outputting the table.

an array is a very useful way of temporarily storing and manipulating data in php, its not outputting as an array. Think of an array as being like a mysql table with as many columns as it wants - it can even have arrays inside it's columns.

looking at your code it might be this bit:

<tbody>
<?php
foreach($xml as $xmls) // loop through our books
{
?>
<tr>
<?php echo "<td>".$xmls->id."</td>";?>
<?php echo "<td>".$xmls->name."</td>";?>
<?php echo "<td>".$xmls->age."</td>";?>
</tr>
<?php
}
?>
</tbody>

$xml is an array, foreach loops through all entries in it using the variable $xmls to reference each book and what php should do for each book it finds

This would be changed to

<tbody>
<?php
$xmldata = array();
foreach($xml as $xmls){
	$xmldata[$xmls->id] = $xmls;
}
foreach($xmldata as $xmls){// loop through our books
	?>
	<tr>
		<?php echo "<td>".$xmls->id."</td>";?>
		<?php echo "<td>".$xmls->name."</td>";?>
		<?php echo "<td>".$xmls->age."</td>";?>
	</tr>
	<?php
}
?>
</tbody>

all the duplicate entries will have the same id so when creating the $xmldata array they will overwrite each other so you will only have 1 entry per unique id.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.