Cursussen/Courses Codesnippets     Top 
PHP - HTML klasse


1. HTML klasse
Met deze klasse kan je een HTML pagina samenstellen door de juiste methodes te gebruiken.
Zo kan je een formulier of lijst maken met labels, invoervelden, knoppen, keuzelijsten, enz...
Bestudeer eerst even de klasse zodat je weet welke argumenten je moet gebruiken bij de methodes.
<?php
class html {
	private $html;
	
	public function __construct($h="") {
		$this->html = $h;
	}
	public function toon() {
		return $this->html;
	}
	public function tag($name,$arg1="",$arg2="",$arg3="") {
		switch($name) {
			case 'meta':
				$this->html .= "<meta name='viewport' content='width=device-width,initial-scale=1,maximum-scale=1'>\n";
				break;
			case 'link':
				$this->html .= "<link rel='$arg1' type='$arg2' href='$arg3'>\n";
				break;
			case 'script':
				$this->html .= "<script src='$arg1'></script>\n";
				break;
			default:
				if ($arg1 == "start") $this->html .= "<$name>\n"; 
				if ($arg1 == "end") $this->html .= "</$name>\n";
				if (empty($arg1)) $this->html .= "<$name>\n"; 
				break;
		}
	}
	public function form_start($css="",$action="",$method="POST") {
		$this->html .= "<form class='".$css."' action='".$action."' method='".$method."'>\n";
	}
	public function form_end() {
		$this->html .= "</form>\n";
	}
	public function br() {
		$this->html .= "<br />\n";
	}
	public function h_start($nr,$css="") {
		$this->html .= "<h".$nr." class='".$css."'>\n";
	}
	public function h_end($nr) {
		$this->html .= "</h".$nr.">\n";
	}
	public function anchor($href,$css="",$caption="link",$par="") {
		if ($par === "p") {
			$this->html .= "<p><a href='".$href."' class='".$css."'>".$caption."</a><p>\n";
		} else {
			$this->html .= "<a href='".$href."' class='".$css."'>".$caption."</a>\n";
		}
	}
	public function label($css="",$caption="label") {
		$this->html .= "<label class='".$css."'><b>".$caption."</b></label>\n";
	}
	public function input($css="",$type,$name,$value="",$required="",$placeholder="") {
		$this->html .= "<input class='".$css."' type='".$type."' name='".$name."' value='".$value."' placeholder='".$placeholder."' ".$required." style='width:98%;'/>\n";
	}
 	public function div_start($css="",$style="",$id="") {
		$this->html .= "<div class='".$css."' style='".$style."' id='$id'>\n";
	}
	public function div_end() {
		$this->html .= "</div>\n";
	}
	public function div($css="",$caption="",$style="") {
		$this->html .= "<div class='".$css."' style='".$style."'>".$caption."</div>\n";
	}
	public function button($css="",$type,$caption="button",$click="") {
		$this->html .= "<button class='".$css."' type='".$type."' onclick='$click'>".$caption."</button>\n";
	}
	public function select_start($css="",$name) {
		$this->html .= "<p><select class='".$css."' name='".$name."'>\n";
	}
	public function select_end() {
		$this->html .= "</select></p>\n";
	}
	public function option($css="",$value,$caption,$selected="",$disabled="") {
		$this->html .= "<option class='".$css."' value='".$value."' ".$disabled." ".$selected.">".$caption."</option>\n";
	}

}


2. Gebruiksvoorbeeld
In dit voorbeeld wordt een standaard formulier samengesteld door gebruik te maken van de HTML klasse.
De labels en invulvelden worden samengesteld uit de eigenschappen van een object van de klasse persoon (= een tabel klasse).
Het bestand 'html_klasse.php' bevat de HTML klasse.
<?php
require_once('html_klasse.php');
require_once('persoon.php');
$persoon = new persoon();
$actie = "toevoegen";
$id = "";
$h = new html();
$h->tag("link","stylesheet","text/css","w3.css");
$h->form_start("w3-container","persoon_verwerking.php");
	$h->anchor("?c=personen&v=persoon_lijst","w3-btn w3-large w3-green w3-round","Lijst personen","p");
	$h->div_start("w3-container w3-pale-green w3-round");
		foreach ($persoon as $name => $value) {
			if ($name != "id") {
				$h->label("w3-label w3-text-teal",$name." *");
				$h->input("w3-input w3-border w3-light-grey","text",$name,$value,"required");
			}
		}
		$h->label("w3-left w3-text-teal","* = verplicht.");
		$h->label("w3-label","&nbsp;");
		$h->div_start("w3-center");
			$h->button("w3-btn w3-blue-grey w3-round w3-center","submit",$actie);
			$h->button("w3-btn w3-blue-grey w3-round w3-right","reset","Leegmaken");
		$h->div_end();
	$h->div_end();
	$h->input("","hidden","actie",$actie);
	$h->input("","hidden","id",$id);
$h->form_end();
echo $h->toon();