PHP Class error?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

PHP Class error?

Post by st89 »

In my PHP file I have the following (in order):

Code: Select all

$form = new form("myform", "POST", "sample.php");
where new form() =

Code: Select all

class form {
  private $id;
  private $method;
  private $action;
  private $content;
  private $output;
 
  public function __construct($id, $method, $action) {
    $this->id = $id;
    $this->method = $method;
    $this->action = $action;
  }
 
  public function add_button($type, $value) {
    $this->content .= "<input type=\"$type\" value=\"$value\"></input>";
  }
  
  public function create() {
    $this->output = "<form id=\"".$this->id."\" method=\"".$this->method."\" action=\"".$this->action.">";
    $this->output .= $this->content;
    $this->output .= "</form>";
    return($this->output);  
  }
 
  public function __destruct() {
    return(NULL);
  }  
}
And when I do this:

Code: Select all

$form->add_button('submit', 'submit');
$xsl->add_content($form->create());
I get this in my browser:

Code: Select all

Warning: DOMDocument::loadXML() [domdocument.loadxml]: attributes construct error in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Couldn't find end of Start Tag div line 1 in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Unescaped '<' not allowed in attributes values in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: attributes construct error in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Couldn't find end of Start Tag form line 1 in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Opening and ending tag mismatch: body line 1 and form in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Opening and ending tag mismatch: html line 1 and div in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Opening and ending tag mismatch: template line 1 and body in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Opening and ending tag mismatch: stylesheet line 1 and html in Entity, line: 1 in /var/www/html/home.php on line 28
 
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Extra content at the end of the document in Entity, line: 1 in /var/www/html/home.php on line 28
...which leads to the XSLT processor failing. I think this indicates a fail in using my xsl() class (shown here):

Code: Select all

class xsl {
  private $xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  private $content;
 
  public function add_content($var) {
    $this->content .= $var;
  }
    
  public function output() {
    $this->xsl .= "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" >";
    $this->xsl .= "<xsl:template match=\"/\">";
    $this->xsl .= "<html>";
    $this->xsl .= "<head>";
    $this->xsl .= "<title>Title</title>";
    $this->xsl .= "</head>";
    $this->xsl .= "<body>";
    $this->xsl .= "<h1>H1: header here</h1>";
    $this->xsl .= "<h2>H2: Menu</h2>";
    $this->xsl .= "<h3>H3: Sub-menu options</h3>";
    $this->xsl .= "<h4>H4: Page section identifiers</h4>";
    $this->xsl .= "<div id=\"content\"".$this->content."</div>";
    $this->xsl .= "<h5>H5: This page Copyright 2010, mysite.com</h5>";
    $this->xsl .= "</body>";
    $this->xsl .= "</html>";
    $this->xsl .= "</xsl:template>";
    $this->xsl .= "</xsl:stylesheet>";
    return($this->xsl);
  }
 
  public function __destruct() {
    return(NULL);
  }
}
I would be really grateful if someone could point out where I've gone wrong. I've tried escaping characters and using stripslashes() to no avail. Ideally what I want to happen is for a form to be created, a submit button to be added to it and for it to be inserted into an XSL object ready for XSLT.

Thanks in advance for any help/tips/pointers! :D
smixcer
Forum Newbie
Posts: 5
Joined: Fri Jan 30, 2009 5:09 am

Re: PHP Class error?

Post by smixcer »

Pls where did $xsl and the method/function - add_content() come from.
Specify it, cos your code does not show such.
bowtakwah
Forum Newbie
Posts: 4
Joined: Sat Dec 12, 2009 2:48 pm

Re: PHP Class error?

Post by bowtakwah »

I noticed:

Code: Select all

$this->xsl .= "<div id=\"content\"".$this->content."</div>";
Should be this:

Code: Select all

$this->xsl .= "<div id=\"content\">".$this->content."</div>";
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

Re: PHP Class error?

Post by st89 »

smixcer wrote:Pls where did $xsl and the method/function - add_content() come from.
Specify it, cos your code does not show such.
$xsl is a new xsl(). add_content is a method of xsl. It is in the class
st89
Forum Newbie
Posts: 17
Joined: Sat Nov 28, 2009 5:42 pm

Re: PHP Class error?

Post by st89 »

Found it:

Code: Select all

$this->output = "<form id=\"".$this->id."\" method=\"".$this->method."\" action=\"".$this->action.">";
Should be:

Code: Select all

$this->output = "<form id=\"".$this->id."\" method=\"".$this->method."\" action=\"".$this->action."\">";
I think it was causing the parser to interpret everything in my objects after that point as one giant attribute.
Post Reply