Results 1 to 7 of 7

Thread: Newbie question

  1. #1
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Question Newbie question

    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();
    }
    }

    ?>
    Last edited by xtiano77; 02-04-2009 at 01:21 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    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.

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You dont need all that. HTML can go with php like for instance:

    Code:
    <!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>

  4. The Following User Says Thank You to bluewalrus For This Useful Post:

    xtiano77 (02-03-2009)

  5. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    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

  6. The Following User Says Thank You to JasonDFR For This Useful Post:

    xtiano77 (02-03-2009)

  7. #5
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    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"?

  8. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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

    PHP Code:
    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:

    PHP Code:
    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.

  9. The Following User Says Thank You to Schmoopy For This Useful Post:

    xtiano77 (02-04-2009)

  10. #7
    Join Date
    Sep 2008
    Location
    Midland, Texas
    Posts
    52
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •