1

How do I trigger a div based on the page name.

I would like to show a different php response in the same div based on the page name.

I am thinking something like:

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

?>

This is because the structure of all the pages is:

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

I want to use this structure to all the pages and I want the div "content" to change based on the page title.

Any help will be appreciated.

Thank you

---------UPDATE 2---------- This is my code:

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

This code is for all the pages except that this 2 sections changes, thats why i want to trigger the the div "internally".

   <?php  include("includes/secondary-menu.php");?>
   <?php  include("includes/working-space.php");?>
MisterNubb
  • 55
  • 8

1 Answers1

1

Actually, from the point of view of efficiency you could get by with just one page instead of multiple pages and just change the content div, as follows:

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

The dynamic content script of course could be used to extract content from a database. Note: this solution rests upon a PHP .INI configuration that permits allow_url_include = On which presents security issues on a shared server.

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • The above solution was in response to the original posted issue. Since the problem is evidently more complicated, you may need to use different templates as well as store content in a database. The main principle is that the template you use and the content retrieved from the database will be on the basis of the query string for a given webpage's URL. – slevy1 May 27 '17 at 09:23
  • Yes, your answer it is anyway useful for me. SO thank you so much. I will anyway go for the solution of using template with php smarty engine which I think is the most appropriate for the usage I do – MisterNubb May 27 '17 at 18:22