2016-06-22 11 views
1

私はPHPの本をフォローしており、いくつかの機能を理解していません。これら3つの機能はどのように連携して機能していますか?

特に、public関数DisplayMenu($ buttons)。私はこれがメニュー(家、連絡先などのためのテーブルを作成し、それらを除外する)を作成することを知っています。そして、ここでパラメータ

public function DisplayButton($width, $name, $url, $active = true) 

$width = 100/count($buttons); 
    Here is what I am not understanding. Im setting a variable called width to be 100/count ? lets say buttons are 4 so 100/4 25? What is the point of the 25? Also, 

$this -> DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url)); 

は、私は、彼らが画面上に作り出すか知っているが、これらのfucntiosnの重要な部分をunderstadningありません。ある人が小さな光を放つことができますか?あなたのクラス属性が宣言されている場合

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' 
        ); 

    // class Page's operations 
    public function __set($name, $value) 
    { 
    $this->$name = $value; 
    } 

public function DisplayMenu($buttons) 
    { 

    echo "<table width='100%' bgcolor='red' cellpadding='3' 
       cellspacing='4'\n"; 
    echo " <tr>\n"; 

    $width = 100/count($buttons); 

    while (list($name, $url) = each($buttons)) 
    { 

     $this -> DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url)); 
    } 
    echo " </tr>\n"; 
    echo "</table>\n"; 
    } 


    { 
    if ($active) 
    { 
     echo "<td width ='$width%'> 
      <a href ='$url'> 
      <img src ='s-logo' alt ='$name' border ='0' /></a> 
      <a href ='$url'><span class='menu'>$name</span></a></td>"; 
    } 
    else 
    { 
     echo "<td width ='$width%'> 
      <img src ='side-logo.gif'> 
      <span class='test'>$name</span></td>"; 
    } 
    } 





    public function IsURLCurrentPage($url) 
    //#determines weather a url for the button points to the current page. 
    // #the server[php_self,$url] returns a number if the string in $url is inside the superglobal variable $_server[pph] 
    { 
    if(strpos($_SERVER['PHP_SELF'], $url)==false) 
    { 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
    } 






public function DisplayButton($width, $name, $url, $active = true) 
    //outputs a single menu button if button is to point to the page to are on, you display an inactive button 
#I think width just gives the table cell a % value. $name involves a setter function declared up top? , 
    { 
    if ($active) 
    { 
     echo "<td width ='$width%'> 
      <a href ='$url'> 
      <img src ='s-logo' alt ='$name' border ='0' /></a> 
      <a href ='$url'><span class='menu'>$name</span></a></td>"; 
    } 
    else 
    { 
     echo "<td width ='$width%'> 
      <img src ='side-logo.gif'> 
      <span class='test'>$name</span></td>"; 
    } 
    } 
+0

ボタンの数に応じて表のセル幅を%で設定します。したがって、4つのボタンがある場合、各セルは1行に25%の幅で表示されます。 – TheDrot

+0

おおきにありがとう/ $ this-> DisplayButton($ width、$ name、$ url、!$ this-> IsURLCurrentPage($ url));実際には? –

+0

'$ width'%幅のテーブルセルとその中に画像を持つ 'button'を作成します。画像が見つからない場合、画像は '$ name'テキストを表示し、' button 'の中に '$ name'もテキストとして表示します。 '$ url'と '!$ this-> IsURLCurrentPage($ url)'へのリンクを持つ 'ボタン'は、現在のページへのリンクを持つボタンにはリンクがないことを意味します。現在のページへのリンクが無効になります。 – TheDrot

答えて

0

は、おそらくこれがclass宣言が欠落しているが、我々は上に移動...

以下

宣言

//変数が、あります。

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' 
       ); 

__set()は、PHPの魔法です。後でhereのすべてをチェックすることができます。我々は__set()に焦点を当てます。つまり、オブジェクトに属性を設定するたびに、クラスに宣言していなくても、この属性を定義して設定します。

// class Page's operations 
public function __set($name, $value) 
{ 
    $this->$name = $value; 
} 

次に、我々は、画面上のメニューをレンダリングDisplayMenuを、持っています。 $widthは、各ボタンの幅を意味します。したがって、$buttonsに4つのボタンがある場合、スペースが分割されます(この場合は100%です)。その結果、ボタンごとに25%になります。

public function DisplayMenu($buttons) 
{ 

    echo "<table width='100%' bgcolor='red' cellpadding='3' 
      cellspacing='4'\n"; 
    echo " <tr>\n"; 

    $width = 100/count($buttons); 

    while (list($name, $url) = each($buttons)) 
    { 
     // This will render the button on screen. 
     // Params: 
     // 1 The width of the button. 
     // 2 The name of the button. 
     // 3 The url the button points to. 
     // 4 Checks if the button url is the same url of the current page. 
     $this->DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url)); 
    } 
    echo " </tr>\n"; 
    echo "</table>\n"; 
} 

IsURLCurrentPage方法をチェックし、引数として渡された$urlは、現在のURLで、trueまたはfalseを返す場合。現在のページの場合はtrue、そうでない場合はfalse

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

最後に、DisplayButtonが実際のボタンをレンダリングします。それぞれは個別にレンダリングされます。

public function DisplayButton($width, $name, $url, $active = true) 
{ 
    // This will, in practice verify for you if 
    // the current page is the same of the button the user clicked. 
    // If it is, it will display the button without the link for that 
    // button, or with a disabled link. 
    // You'll notice it will show differently. Maybe 
    // also with some different color or style according to the CSS class. 
    // Remember that $active is the result of the IsURLCurrentPage(). 

    if ($active) 
    { 
     echo "<td width ='$width%'> 
       <a href ='$url'> 
       <img src ='s-logo' alt ='$name' border ='0' /></a> 
       <a href ='$url'><span class='menu'>$name</span></a></td>"; 
    } 
    else 
    { 
     echo "<td width ='$width%'> 
       <img src ='side-logo.gif'> 
       <span class='test'>$name</span></td>"; 
    } 
} 
+0

私はあなたを愛しています、またiscurrentのために、私のページを表示することができますか? contact.phpという名前の新しいファイルを作成した場合と同様に、連絡先をクリック可能にして新しいページに移動することができますか? –

+0

isURLCurrentPage'は、現在のページがレンダリングされているボタンの同じページであるかどうかをチェックします。それが本当であれば、ボタンは無効になります。同じページでない場合、ボタンをクリック可能にして、別のページに移動することができます。 「連絡先」をクリックするとそのページにリダイレクトされ、リンクは無効になります。 「連絡先」ページで「サービス」をクリックすると、サービスページで「連絡先」ボタンが再度有効になります。 – TiagoRL

+0

ありがとうございます。また、セッターメソッドはどこで使用されていますか?コードでは、while(list($ name、$ url)= each($ buttons)) これはどういうわけかsetメソッドを使って、実際の家や連絡先などのページを吐き出していますか?セットが使用されている例を教えてください。 –

関連する問題