Log in

View Full Version : Resolved Newbie question



xtiano77
02-02-2009, 03:25 AM
Hello. I am new to PHP and I must say that I like it. I am trying to make a php page that echoe its code from a PHP class. However, I am having trouble getting the page content to show. I am including the code for both files below. Thanks in advance for your help.

Main page, "index.php":

<?php
require('pageclass.php');
$newpage = new pageclass();
$newpage -> $contents = "<p>Test<br />Test</p>";
$newpage -> displaypage();
?>

****************

PHP class file:

<?php

class pageclass{
public $contents;
public function __set($a,$b){
$this -> $a = $b;
}
public function opening1(){
echo "<!DOCTYPE html PUBLIC \"-//W3C/DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml/DTD/xhtml11-transitional.dtd\">\n";

echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";

echo "<head>\n";

echo "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\n";
}
public function css(){
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/default.css\" />\n";
}
public function javascript(){
echo "<script type=\"text/javascript\" src=\"../scripts/default.js\"></script>\n";

echo "<script type=\"text/javascript\">\n";

echo "\tif(top != self){\n";

echo "\t\ttop.href = \"http://www.xtianos.com/test/index.php\"\n";

echo "\t}\n";

echo "</script>\n";
}
public function opening2(){

echo "<title>Indes PHP Page</title>\n";

echo "</head>\n";

echo "<body>\n";
}
public function navigationbar(){

echo "\t<div class=\"navbuttons center\">\n";

echo "\t\t<ol>\n";

echo "\t\t\t<li><a href=\"pages/familyphotos.php\">Family Photos</a></li>\n";

echo "\t\t\t<li><a href=\"#\">Recipes</a></li>\n";

echo "\t\t\t<li><a href=\"#\">New Home Photos</a></li>\n";

echo "\t\t\t<li><a href=\"#\">Supplements</a></li>\n";

echo "\t\t</ol>\n";

echo "\t</div>\n";
}
public function pagecontents(){
echo $contents;
}
public function closing(){

echo "</body>\n";

echo "</html>\n";

}
public function displaypage(){
$this -> opening1();
$this -> css();
$this -> javascript();
$this -> opening2();
$this -> navigationbar();
echo $this -> contents;
$this -> closing();
}
}

?>

xtiano77
02-02-2009, 03:56 AM
Too late not to sound stupid. I found the problem. I was including a $ when assigning a value to the variable that would display the contents. The $ only applies to the class and not to the index page. Thanks anyway.

bluewalrus
02-02-2009, 04:04 AM
You dont need all that. HTML can go with php like for instance:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
anything you'd put in your head css links java links meta tags title etc.</head>
<body>
<div id="contain">
<div id="banner"></div>
<div id="content">
<div id="menu">
</div>
Other page content
<?php
$count = "hit.txt";
$read = fopen($count, 'r');
$rad = fread($read, 99999999999);
fclose($read);
$value = fopen($count, "w+");
$entervalue = $rad + 1;
fwrite($value, $entervalue);
echo "<div id=\"counter\">" . $entervalue . "</div><div id=\"outercount\"></div>";
fclose($value);
?>
</div>
</div>
</body>
</html>

JasonDFR
02-02-2009, 06:54 PM
Hey blue,

In his case he can't do what you suggest because he is using object oriented php. Which by the way is great.

J

xtiano77
02-03-2009, 01:30 AM
Thanks for both of your replies. While we are on the subject of OO PHP, I have a question. When I call the PHP class file "require('pageclass.php')", does the page have to be in the same directory or can I call it from another directory without having to modify any of the "ini" file settings? For example, can I do the following:

require('pageclass.php');

or can I call the page in the same way that I make reference to the CSS and JS documents on the HTML page?

require('../scripts/pageclass.php');

Thanks in advance for your help and patience. Hopefully in the not too distant future I will be able to give advise on PHP at your level.

Oh, before I forget, how do I mark or change a thread to "Solved"?

Schmoopy
02-03-2009, 02:26 AM
You will have to change the path depending on where your files are in relation to the PHP file. If you have a folder called "Sites" and in there you have your index.php and the file you want to include is in "sites/includes/pageclass.php" then you will have to put



require("includes/pageclass.php");


As long as all your other files are in the same folder then you won't need to change this, but if you have another file called "about.php" in Sites -> About -> about.php then you will need to change the require path to:



require("../includes/pageclass.php");


As for the "Resolved" prefix. Simply click the Edit button on your first post and the click go advanced and then in a drop down box next to the title will be the prefix "Resolved", just click it and save to say that your problem has been resolved.

xtiano77
02-04-2009, 01:20 AM
Thanks a bunch Schmoopy. I am going to give it a try today. Thanks again to you and everyone who took the time to reply.