1.11M Members

PHP class + DomDocument + SESSION

 
0
 

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...

 
0
 

as far as my knowledge is concern...

var $dom; $body, $root is global inside your function....if you use that inside the class use that in this format "$this->dom; $this->body; $this->root".

change all your variables with this "$this->[variables];

i hope it helps.

 
0
 

as far as my knowledge is concern...

var $dom; $body, $root is global inside your function....if you use that inside the class use that in this format "$this->dom; $this->body; $this->root".

change all your variables with this "$this->[variables];

i hope it helps.

Thanks, now the code looks a little bit better. I can't still change or add the XML-file created at the start of the session. Any tips?

 
0
 

tips you can extend your parent classes instead of inserting new object inside your class.

class Cart {
    var $items;  // Items in our shopping cart

    // Add $num articles of $artnr to the cart

    function add_item($artnr, $num) {
        $this->items[$artnr] += $num;
    }

    // Take $num articles of $artnr out of the cart

    function remove_item($artnr, $num) {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } elseif ($this->items[$artnr] == $num) {
            unset($this->items[$artnr]);
            return true;
        } else {
            return false;
        }
    }
}


[here's the extension :-)]

class Named_Cart extends Cart {
    var $owner;
  
    function set_owner ($name) {
        $this->owner = $name;
    }
}
 
0
 

tips you can extend your parent classes instead of inserting new object inside your class.

class Cart {
    var $items;  // Items in our shopping cart

    // Add $num articles of $artnr to the cart

    function add_item($artnr, $num) {
        $this->items[$artnr] += $num;
    }

    // Take $num articles of $artnr out of the cart

    function remove_item($artnr, $num) {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } elseif ($this->items[$artnr] == $num) {
            unset($this->items[$artnr]);
            return true;
        } else {
            return false;
        }
    }
}


[here's the extension :-)]

class Named_Cart extends Cart {
    var $owner;
  
    function set_owner ($name) {
        $this->owner = $name;
    }
}

Can I extend the class from the $_SESSION variable? Is it impossible to just get back the DomDocument object and just change it? I think of the document as an object with some methods and a container with data. Hope I haven't got it wrong. I would prefer not extending the class...

 
0
 

I am talking about your class not the session. I am suggesting put your domDocument as parent and extends some parts of your class code. But if your code is working fine never mind my suggestion.

 
0
 

Thanks for helping me phpbeginners :) I have solved the problem now but I will take a look at your suggestions and try to follow them. Thanks again and have a nice one :)

Question Answered as of 3 Years Ago by phpbeginners
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: