2012-05-30 112 views
1

我想學習PHP,所以我很可能會犯一個非常初學者級別的錯誤。這個PHP有什麼問題?什麼都沒有顯示

基本上,我有一個page.php文件,它是我想要創建的頁面的類文件,並且我想將它包含在我的home.php頁面中(我使用require函數執行此操作)。但是,什麼也沒有顯示我正在做這個基於我正在閱讀的書,它似乎在那裏工作得很好。

page.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>"; 
    } 
} 

?> 

home.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(); 

?> 

任何瞭解,將不勝感激。

+1

您是否打開了錯誤報告?你有什麼錯誤嗎? – j08691

+6

也許是因爲home.php中的這一行沒有分號。 '$首頁 - >內容=「

您好,我喜歡藍莓greatlly

我希望你能和我一起愛藍莓

!」' – sbeliv01

+0

解析錯誤:語法錯誤,在page.php文件上意外T_OBJECT_OPERATOR行86 – Lobo

回答

4

兩個問題:

  • this應該$thispage.php 86線。
  • 您在...blueberries!</p>"

到底需要一個分號,您可能沒有看到的錯誤,如果error reporting未打開。

+0

哦,這工作完美。我仍然在想Java的思維方式。我現在要在我手背上畫一個大的美元符號。還添加了分號以及錯誤報告。歡呼徹底的答案。 –

+0

其實我用的服務器是我們大學的。我無法改變這一點,我可以嗎? –

+0

只要嘗試在'home.php'的頂部放置'error_reporting(E_ALL);'。 –

1

在home.php文件你在國家統計局10

缺少分號在page.php文件文件你就行的丟失字符「$」 86

$this -> displayButton($width, $name, $url, !$this->IsURLCurrentPage($url)); 
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;"; 
    } 
?> 
+0

或使用輸出緩衝。 – nickb

+0

我以爲這看起來很奇怪,但這本書告訴我如何去做。這是一個截圖。 http://i.imgur.com/Uqfuz.png –