【问题标题】:Want to use PHP include to place a page header (in Bootstrap 5) based on the file name (and location)Want to use PHP include to place a page header (in Bootstrap 5) based on the file name (and location)
【发布时间】:2022-11-09 11:19:10
【问题描述】:

I want to place an image, HTML or simply text as a page header on multiple pages, but it is specific to each page, based on the file name (and folder it is in).

So, for example, domain.com/portfolio/index.php gets one image (or text/HTML/CSS), domain.com/portfolio/about/index.php gets another, domain.com/portfolio/contact/index.php gets another and so on.

Basically, I want to update this common element from one file instead of updating a bunch of files. I will usually use either the same image or the same HTML/CC design with different text and/or image in it, so the code example below includes a simplified version of each, just in case.

I've successfully used this in the past for pageheaders on sites but it no longer seems to work (PHP updated or maybe Bootstrap 5 is mucking things up)... it sits in pageheader.php which is then included in the top.php that is used (included) on each page of the site. (And I am not a programmer :) )

Help is always appreciated - thanks!

<?php

$path = ("/portfolio");
$size = ("WIDTH=525 HEIGHT=41 BORDER=0");
$self = $_SERVER['PHP_SELF'];

    if (strstr($PHP_SELF,"$path/about.php")) {echo "<h1>About Page Header HTML/CSS Here!</h1>";}
    elseif (strstr($PHP_SELF,"$path/index.php")) {echo "Home Page Header Text Here";}
    elseif (strstr($PHP_SELF,"$path/design/index.php")) print ("<IMG SRC=$path/images/header_design.jpg $size>");
    elseif (strstr($PHP_SELF,"$path/articles/index.php")) print ("<IMG SRC=$path/images/header_articles.jpg $size>");
else {echo "<h1>Hello World.</h1>";}
?>
  • "it no longer seems to work" - is there an error message? Or an empty space? Or something else?
  • Every page gets the "Hello Word" (else line).

标签: php html css


【解决方案1】:

Note : $PHP_SELF in (strstr($PHP_SELF,"$path/about.php"))

Shouldn't it be $self, e.g. (strstr($self,"$path/about.php"))

<?php 
$path = ("/portfolio");
$size = ("WIDTH=525 HEIGHT=41 BORDER=0");
$self = $_SERVER['PHP_SELF'];

if (strstr($self ,"$path/about.php")) {echo "<h1>About Page Header HTML/CSS Here!</h1>";}
elseif (strstr($self ,"$path/index.php")) {echo "Home Page Header Text Here";}
elseif (strstr($self ,"$path/design/index.php")) print ("<IMG SRC=$path/images/header_design.jpg $size>");
elseif (strstr($self ,"$path/articles/index.php")) print ("<IMG SRC=$path/images/header_articles.jpg $size>");
else {echo "<h1>Hello World.</h1>";}

?>
    【解决方案2】:

    (similar to this topic),

    how would you include like a separate php file.. e.g. (include.php) that would have various functions.. includes... and standard html doctype header? this is what im perplexed with.. cause i use

      猜你喜欢
      • 2022-12-02
      • 2022-12-02
      • 2022-12-28
      • 2022-12-27
      • 2022-12-01
      • 2023-04-01
      • 2022-12-26
      • 2022-12-02
      • 2022-12-02
      相关资源
      最近更新 更多