Rommeo Posted December 10, 2009 Share Posted December 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/ Share on other sites More sharing options...
mikesta707 Posted December 10, 2009 Share Posted December 10, 2009 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() Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-974890 Share on other sites More sharing options...
KevinM1 Posted December 10, 2009 Share Posted December 10, 2009 Or, you could break things up into various files and include them where necessary.... Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-974896 Share on other sites More sharing options...
mrMarcus Posted December 10, 2009 Share Posted December 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-974897 Share on other sites More sharing options...
Rommeo Posted December 10, 2009 Author Share Posted December 10, 2009 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-974902 Share on other sites More sharing options...
mikesta707 Posted December 10, 2009 Share Posted December 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-974913 Share on other sites More sharing options...
Rommeo Posted December 10, 2009 Author Share Posted December 10, 2009 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-974970 Share on other sites More sharing options...
wildteen88 Posted December 10, 2009 Share Posted December 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-974981 Share on other sites More sharing options...
Rommeo Posted December 10, 2009 Author Share Posted December 10, 2009 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-975004 Share on other sites More sharing options...
Rommeo Posted December 10, 2009 Author Share Posted December 10, 2009 Does anyone knows such a function ? I m stuck ? Quote Link to comment https://forums.phpfreaks.com/topic/184658-page-class-how-to-change-the-content/#findComment-975055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.