2

我正在尝试学习 PHP,所以我很可能会犯一个非常初级的错误。

基本上,我有一个 page.php 文件,它是我要创建的页面的类文件,我想将它包含在我的 home.php 页面中(我使用 require 函数来执行此操作)。然而,什么也没有出现。我是根据我正在阅读的一本书来做这件事的,它似乎在那里工作得很好。

页面.php:

<?php

class Page {
    // attributes
    public $content;
    public $title = "TLA Consulting Pty Ltd";
    public $keywords = "TLA Consulting, Three Letter Abbreviation, some of my best friends are search engines";
    public $buttons = array("Home" => "home.php", "Contact" => "contact.php", "Services" => "services.php", "Site Map" => "map.php");

    public function __set($name, $value) {
        $this->$name = $value;
    }

    public function display() {
        echo "<html>\n<head>\n";
        $this->displayTitle();
        $this->displayKeywords();
        $this->displayStyle();
        echo "</head>\n<body>\n";
        $this->displayHeader();
        $this->displayMenu($this->buttons); // try this line without giving it the parameter
        echo $this->content;
        $this->displayFooter();
        echo "</body>\n</html>";
    }

    public function displayTitle() {
        echo "<title>$this->title</title>";
    }

    public function displayKeywords() {
        echo "<meta name=\"keywords\" content=\"$this->keywords\" />";
    }

    public function displayStyle() {
?>
        <style>
            h1 {
                color: white;
                font-size: 24px;
                text-align: center;
                font-family: helvetica, arial, sans-serif;
            }

            .menu {
                color: white;
                font-size: 12px;
                text-align: center;
                font-family: helvetica, arial, sans-serif;
                font-weight: bold;
            }

            td {
                background-color: black;
            }

            p {
                color: black;
                font-size: 12px;
                text-align: justify;
                font-family: helvetica, arial, sans-serif;
            }
        </style>
<?php
    }

    public function displayHeader() {
?>
        <table width="100%" cellpadding="12" cellspacing="0" border="0">
            <tr bgcolor="black">
                <td aling="left"><img src="http://i.imgur.com/xBrUZ.png" alt="Iron Man's finger" /></td>
                <td><h1>TLA Consulting Pty Ltd</h1></td>
            </tr>
        </table>
<?php
    }

    public function displayMenu($buttons) {
        echo "<table width=\"100%\" bgcolor=\"white\">\n";
        echo "<tr>\n";

        // calculate button size
        $width = 100 / count($buttons);

        while (list($name, $url) = each($buttons)) {
            $this -> displayButton($width, $name, $url, !this->IsURLCurrentPage($url));
        }
        echo "</tr>\n</table>\n";
    }

    public function IsURLCurrentPage($url) {
        if (!strpos($_SERVER['PHP_SELF'], $url)) {
            return false;
        }
        else {
            return true;
        }
    }

    public function displayButton($width, $name, $url, $active = true) {
        if ($active) {
            echo "<td width=\"".$width."%\">
            <a href=\"$url\"><img src=\"http://i.imgur.com/xBrUZ.png\" alt=\"$name\" /></a>
            <a href=\"$url\"><span class=\"menu\">$name</span></a>
            </td>";
        }
        else {
            echo "<td width=\"".$width."%\">
            <img src=\"http://i.imgur.com/xBrUZ.png\" />
            <span class=\"menu\">$name</span>
            </td>";
        }
    }

    public function displayFooter() {
        echo "<p>I am a footer ololz</p>";
    }
}

?>

主页.php:

<?php

    require("page.php");

    $homepage = new Page();

    $homepage->content = "<p>Hi there, I like blueberries greatlly.</p>
                          <p>I hope you'll join me in loving blueberries!</p>"

    $homepage->display();

?>

任何见解将不胜感激。

4

3 回答 3

5

两个问题:

  • this应该$this在第 86 行page.php
  • 你需要一个分号在末尾...blueberries!</p>"

如果未打开错误报告,您可能看不到错误。

于 2012-05-30T15:20:07.390 回答
2

您不一致地使用 php 的开头和结尾。

例如这里:

    public function displayStyle() {
?>
    <style>
        h1 {
            color: white;
            font-size: 24px;
            text-align: center;
            font-family: helvetica, arial, sans-serif;
        }

        .menu {
            color: white;
            font-size: 12px;
            text-align: center;
            font-family: helvetica, arial, sans-serif;
            font-weight: bold;
        }

        td {
            background-color: black;
        }

        p {
            color: black;
            font-size: 12px;
            text-align: justify;
            font-family: helvetica, arial, sans-serif;
        }
    </style>
  <?php  
  }

您正在启动一个函数,然后通过关闭 php 块“?>”来打破它并粘贴一个样式表部分,该部分将在页面加载时回显。如果要使用此功能显示样式,则必须将其设置为:

<?php
 public function displayStyle() {

 echo   "<style>".
        "h1 {".
            "color: white;";   
  }
 ?>
于 2012-05-30T15:30:58.033 回答
1

在 home.php 文件中,ine 10 中缺少分号

在 page.php 文件中,您在第 86 行缺少字符“$”

$this -> displayButton($width, $name, $url, !$this->IsURLCurrentPage($url));
于 2012-05-30T15:22:37.640 回答