I'm construted a php-class where I create and save some elements in an XMLfile. Outside that class I instantiate the class and save it in a _SESSION if the session has not been created yet, otherwise I want to get the document back and add new elements to the XMLfile. My problem is that I can not access the document after I have saved it. The essential parts of the code are:
<?php
session_start();
class Bovision {
var $dom; var $root; var $body;var $table;
function __construct(){
$html = '<html><head><title>Bovision</title></head><body><table></table></body></html>';
global $dom, $body;
$dom = new DomDocument('1.0','utf-8');
//header("Content-Type: text/plain");
$dom->loadHtml($html);
$root = $dom->createElement('root');
$dom->appendChild($root);
$body = $dom->getElementsByTagName('body')->item(0);
//$table = $dom->getElementsByTagName('table')->item(0);
$body->appendChild($table);
$root->appendChild($body);
}
function dbSearch($query){
$con = mysql_connect("localhost", "root", "pwd") or die("Error!!!!");
mysql_select_db("myDB", $con);
$fromDB = array();
$result = mysql_query($query, $con) or die(mysql_error());
$i = 0;
while($row = mysql_fetch_array($result)){
$fromDB[$i] = $row;
$i++;
}
mysql_close($con);
return $fromDB;
}
function createBoxes($args, $name){
global $dom, $body;
$select = $dom->createElement('select');
$select->setAttribute('id', $name);
$option = $dom->createElement('option');
//$option->setAttribute('name', $name);
$option->nodeValue = "Välj ".$name;
//$text = $dom->createTextNode("Välj ".$name);
//$option->appendChild($text);
$select->appendChild($option);
for($i = 0; $i < count($args); $i++){
$option = $dom->createElement('option');
$option->nodeValue= $args[$i][0];
//$text = $dom->createTextNode($args[$i][0]);
//$option->appendChild($text);
$select->appendChild($option);
}
$body->appendChild($select);
//echo $dom->saveXML();
}
function save(){
global $dom;
echo $dom->saveXML();
}
function getDom(){
global $dom;
return $dom;
}
function disp(){
global $dom;global $body;
$p = $dom->createElement('p');
$text = $dom->createTextNode("eefefefef");
$p->appendChild($text);
$body->appendChild($p);
}
}
?>
<?php
if(!session_is_registered('bo')){
$_SESSION['bo'] = new MyClass();
$query = "SELECT DISTINCT lan FROM bostader ORDER BY pris";
$result = $_SESSION['bo']->dbSearch($query);
$_SESSION['bo']->createBoxes($result, "lan");
$query = "SELECT DISTINCT objekttyp FROM bostader ORDER BY pris";
$result = $_SESSION['bo']->dbSearch($query);
$_SESSION['bo']->createBoxes($result, "typ");
$query = "SELECT DISTINCT rum FROM bostader ORDER BY pris";
$result = $_SESSION['bo']->dbSearch($query);
$_SESSION['bo']->createBoxes($result, "rum");
$_SESSION['bo']->save();
}
else{
/*Here I want to be able to change the elements in the XML-document or add some new elemens. The function disp() has just been created to test if I can change something in the document*/
/*
Tried to get $dom variable from the class by writing $_SESSION['bo']->dom but the does not work neither :S I also created a method called getDom that returns the $dom variable in MyClass but it seems to be empty.
*/
$_SESSION['bo']->disp();
$_SESSION['bo']->save();
}
?>
Could anyone take a look at the problem and give me some tips...