Jump to content

PHP class to include content in Div


Recommended Posts

I have created a class to build up the content in my page. I want to use this a broad as possible. Where I run into, is how to enclose certain content within (for example) a div, without the need for calling a closing tag.

 

Below is a section of my class, which does the following:

__construct -> Creates the "<article>" section (closed when ->Display() is called). This to keep each section in their own <article> tag

AddHeader -> creates the header within the <article>

AddContent -> creates the div in which all the content should be kept.

Display -> closes the <div> and </article> and allows it to be echo'd.

 

What I don't want is to keep calling the AddContent for every piece of content, but would like to add the content via various options like 'AddGraph', 'AddTable', which will be added to the content section. Also besides the header, potential tabs could be added, so adding the <div> after the header tag is not an option.

 

Any good suggestions and/or source which would help me further?

 

With kind regards

Ralf

<?php
class PageContent {
	var $content;
	
	function __construct($class= NULL) {
		$this->content = "\n<article". (!empty($class) ? " class='". $class ."'" : "") .">\n";
		//$this->content = "\n<div". (!empty($class) ? ' class=\"$class\"' : '') .">\n";
    }

	function AddHeader($naam, $class= NULL){
		$this->content .= "<header><h3". (!empty($class) ? " class='". $class ."'" : "") .">". $naam ."</h3></header>\n";
	}
	
	function AddContent($class= NULL){
		$this->content .= "<div". (!empty($class) ? " class='". $class ."'" : "") .">\n";
	}
	
	function Display() {
		$this->content .= "<div class='clear'></div>\n";
		$this->content .= "</div>\n";
		$this->content .= "</article>\n";

        return $this->content;
    }
}
?>
Link to comment
https://forums.phpfreaks.com/topic/284291-php-class-to-include-content-in-div/
Share on other sites

Instead of calling the AddContent method directly, the AddGraph method could perform the call.

 

Side note: some of the basics for creating classes in PHP have changed. For example, "var" is no longer required. More information can be found here:

http://php.net/manual/en/language.oop5.php

 

 

If you declare a property using var instead of one of public, protected, or private, then PHP 5 will treat the property as if it had been declared as public.

Edited by cyberRobot
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.