Jump to content

page class, how to change the content ?


Rommeo

Recommended Posts

I have a page class like

<?php
class page {
public title
public keywords

public topic
public content

public function displayPage()
{
$this->diplayHead();
echo $this->content;
$this->diplayFooter();
}
public function displayHead()
{
echo "<!DOCTYPE HTML PUBLI.. ";
}
}

So when I want to print "user registeration" form. I assign the code(registeration-form) to my $content variable

like;

// in index.php file 
$mypage = new page();
$mypage->content = "<form name=\"form1\"........";
$mypage->displayPage();

but I m really tired or putting escape chars and writing the second line like this;

$mypage->content = "<form name=\"form1\"........";

it's really difficult to print different types of forms and tables everytime I m assigning the code to the content.

 

Also for the result of a query, I have to use a code like this to print the content from the DB:

(sql queries )
while ( ) 
{
$username=mysql_result($result,$i,"username");
$content .= "<a href=\"/id=$username\">$username</a><br/>";
}
$mypage->content = $content;
$mypage->displayPage();

 

I m wondering if there is any other/ easy solution to do this ?

How do you change the content of your page,when you are using a page class ?

 

Thanx in advance.

Link to comment
https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/
Share on other sites

You could use the heredoc syntax instead of single/double quotes.

 

Or you could write the html to a page ( or have a template html page that you use with str_replace/regex to change the contents of) and use file_get_contents()

instead of double-quotes., use single-quotes:

 

$mypage->content = '<form name="form1"........';

 

now escaping double-quotes within the HTML isn't necessary.  however, if you need to have a single-quote within the HTML markup, you will need to then escape that, but that's not that often, if at all.

Thank you for the answers;

I m still wondering which one would be easier for my architecture ?

 

also ;

Nightslyr : Cause I was not aware of the function "file_get_contents()". I was trying to use the include function. At that time it would be easier I think. But how can I use the include function here ? including the file in the class ? or in the index.php file ?

 

Well, if you want to store the content of some page into the "content" attribute of your class, than file_get_contents() would work without much changes to your code.

 

Using includes is very simple tho. If I wanted to include, say, form.php in my page on some condition, i could do

if (something)
     include "form.php";

and if something was true, it would include the form

 

There is a weird problem with "file_get_contents()" method

let's say I want to print the keywords in the page by using my page_class and "file_get_contents"  method

//index.php file
$mypage = new page ();
$mypage->keywords = "hello, world";
$mypage->content = file_get_contents(entry.php);
$mypage->displayPage();

here is my entry page as a content

//entry.php file :
The keywords of this entry are <?php echo $this->keywords;?>

and the output which is weird:

keywords;?>

it's not printing the values of keywords

also not "$this->keywords;"

it's just printing

"keywords;?>"

:S How can I solve this problem ?

file_get_contents will just return the contents of a file being read as plain text. It wont parse the PHP code within the file being read.

So is there any special function that I can use for to print the value of a variable in any file declared in the page class ?

Archived

This topic is now archived and is closed to further replies.

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