2017-05-26 11 views
1

ページ名に基づいてdivをトリガーするにはどうすればよいですか?ページ名に基づいてdivをトリガーする方法

ページ名に基づいて同じdivに異なるPHP応答を表示したいと思います。

私はのようなものを考えています:

<html lang="en"> 
<head> 
    <?php include("includes/head.php");?> 
</head> 
<body> 
    <?php include("includes/top");?> 

    <div id= content> 
     <?php include("includes/content.php");?> 
    </div> 

    <?php include("includes/footer");?> 
</body> 
</html> 

私はすべてのページに、この構造を使用したいと私はDIVをしたい:全てのページの構造があるためです

<?php 

    $pageName = basename($_SERVER['PHP_SELF']); 
    if ($pageName == 'index.php') { 
     include("http://website/reponse-from-script-1); 
    } else if ($pageName == 'index2.php') { 
     include("http://website/reponse-from-script-2); 
    } else if ($pageName == 'index3.php') { 
     include("http://website/reponse-from-script-3); 
    } 

?> 

ページのタイトルに基づいて変更する "コンテンツ"。

ご協力いただければ幸いです。

はありがとう

--------- UPDATE 2 ---------- は、これは私のコードです:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <?php include("includes/head.php");?> 
</head> 
<body> 
    <?php include("includes/scripts.php");?> 
    <div id="wrapper"> 
    <?php include("includes/navandsidebar.php");?> 
    <?php include("includes/main-menu.php");?> 
    <?php include("includes/secondary-menu.php");?> 
    <?php include("includes/working-space.php");?> 

    </div> 
</body> 
</html> 

このコードはすべてのためにありますこの2つのセクションが変更されていることを除いて、私はdivを「内部的に」トリガーしたいのです。以下のように、

<?php include("includes/secondary-menu.php");?> 
    <?php include("includes/working-space.php");?> 
+0

あなたはおそらく魔法の定数である '__FILE__'を使うことができます。[http://php.net/manual/en/language.constants.predefined.php] – jkys

+0

これを使ったことはありません。それを見てください。ありがとうございました – MisterNubb

答えて

1

実際には、効率性の観点から、あなたの代わりに複数のページの1ページだけをすることによって得ることができるとちょうどコンテンツのdivを変更します。

an_index.php:

<?php 
include("getContent.php"); 
echo "<a href=\"./an_index.php?page=$page\">next</a>"; 
?> 

getContent.php:

<?php 
$page = htmlentities($_GET['page']); 
if (is_numeric($page)) { 

    include("http://localhost/exp/dyn_content.php?page=$page"); 
    $page++; 
    if ($page > 3) { 
    $page = 1; 
    } 

} 

dyn_content.php:

<?php 
$p = htmlentities($_GET["page"]); 
if (is_numeric($p)) 
{ 
    switch($p) { 
    case 1: 
     echo "one<br>\n"; 
     break; 
    case 2: 
     echo "two<br>\n"; 
     break; 
    case 3: 
     echo "three<br>\n"; 
     break; 
    default: 
     echo "one<br>\n"; 
    break; 
    } 
} 

もちろん、動的コンテンツスクリプトを使用してデータベースからコンテンツを抽出することもできます。注:この解決方法は、共有サーバーにセキュリティ上の問題を示すallow_url_include = Onを許可するPHP .INI構成に依存します。

+0

私の更新2を見てください。 – MisterNubb

+0

上記の解決策は、最初に掲載された問題に対するものです。問題は明らかに複雑なので、異なるテンプレートを使用するだけでなく、データベースにコンテンツを格納する必要があります。主な原則は、使用するテンプレートとデータベースから取得したコンテンツが、指定されたWebページのURLのクエリ文字列に基づいていることです。 – slevy1

+0

はい、あなたの答えはとにかく私にとっては有益です。あなたには本当にありがとうございます。私はとにかく、PHPのスマートエンジンを使ってテンプレートを使うという解決法に行きます – MisterNubb

関連する問題