Different url display different div - header.php - php

I may be asking a silly or stupid question. But, i am curious as i am beginner in php. I would like to ask whether this way is possible to happen.
Here goes the question:
I want something do like how wordpress do. But, i'm not going to use wordpress in this site. Like wordpress has the header.php in its path. And then, we can use wordpress php code to show or hide the content we like to display or hide as below.
Below is the php code i copied from Wordpress as an example:
<?php if (is_front_page()) { ?><?php } elseif (is_home() || is_single() || is_page() || is_archive() || is_404() || is_search()) { ?>
<?php
/* We add some JavaScript to pages with the comment form
* to support sites with threaded comments (when in use).
*/
if ( is_singular() && get_option( 'thread_comments' ) )
wp_enqueue_script( 'comment-reply' );
/* Always have wp_head() just before the closing </head>
* tag of NCC, or you will break many plugins, which
* generally use this hook to add elements to <head> such
* as styles, scripts, and meta tags.
*/
wp_head();
?>
<?php } ?>
Because, i going to create a simple website implemented with simple php coding. I am going to create a few pages -> page1.php, page2.php, page3.php and header.php. The header.php is to manage the head section of webpage(like how wordpress doing). Is it possible to get it done with simple php code?
Example:
When i visiting page2.php, the header.php will only display the content that i wanted to display.
Is it possible to change the is_single() to an url? Any suggestion? Or any other better solution can do this easier?
Updated:
header.php
<?php
function includeFile($file){
if($file == "index.php"){
Page Title for index.php
}elseif($file == "page.php"){
Page Title for page.php
}elseif($file == "page1.php"){
Page Title for page1.php
}
}
?>
In my index.php
<?php session_start(); /* Starts the session */
if(!isset($_SESSION['UserData']['Username'])){
header("location:login.php");
exit;
}
?>
<?php include('functions.php'); ?>
<!doctype html>
<html>
<head>
<title>
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
</title>
<link href="style.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<link href='http://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' />
<link rel="shortcut icon" href="http://www.example.com/img/favicon.ico" type="image/x-icon">
</head>
<body>
Content...
</body>
</html>

Updated:
Yes, you can certainly do that using PHP magic constant __FILE__ and basename().
header.php
function includeFile($file){
if($file == "page1.php"){
// include stuff for page1.php
}elseif($file == "page2.php"){
// include stuff for page2.php
}elseif($file == "page3.php"){
// include stuff for page3.php
}
}
And in each one of your pages include the header file and call the function includeFile(), like this:
page1.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
page2.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>
page3.php
<?php
require_once('header.php');
includeFile(basename(__FILE__));
?>

Related

Run php function from index.php

This is on file : PurchaseSiteLoggedIn.php
<!DOCTYPE html>
<html lang="en">
<header>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palm User Login-Registration</title>
<link rel="stylesheet" href="">
</header>
<script src=""></script>
<body>
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
</body>
</html>
If the user is not logged in he/she will be redirected to another page.(the loginregister.html)
This code works fine. What I wanna do is replace:
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
with DoAnonymousCheck(); (a random name for the function) so that the code looks cleaner
//
Ideally i would want to have the body of the DoAnonymousCheck on a different file.
I tried somthing like:
I added
<script src='DoAnonymCheck.php'></script>
in the PurchaseSiteLoggedIn.php folder.
And in another folder that i called DoAnonymCheck.php I had
<?php
function DoAnonymCheck(){
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
}
?>
It didnt work though (i guess in <script src></script> you can only add a .js folder)
You can't "import" a PHP script with a script tag, because all PHP code is executed on the server side before it shows up in the client browser. However, you can use include or require to load other PHP scripts. Code Example

Need to call header and footer also a main div to call content

I'm building a site with html and css and i want to make a index.php to call all the parts for the page. Example Call Header and footer and also when click on nav links replace the content in same page, can i do it with php or what are my options.
Example of my index.php
Get header < ? php include("header.html"); ? >
Replace Content < div class=" MainContent "> < /div>
Get Footer < ? php include(" footer.html ") ;?>
Ditn't tested and didn't put any validation in thiscode but i hope you can get the idea how you can do this. with only index.php file. lmk if any query
header.php
navigation
<html>
<head></head>
<body>
navigation
home
About us
page.php
page.php
$page_name = $_GET['page'];
<?php include( $_SERVER['DOCUMENT_ROOT'] . "/path_or_folder_offile/".$page_name.".php" ); ?>
footer.php
</body>
</html>
index.php
<?php include('header.php'); ?>
<?php include('page.php'); ?>
<?php include('footer.php'); ?>
This is my usual page layout for the index.php
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Title</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div id="page">
<?php include_once('header.php'); ?>
<?php if (isset($_GET['page']) && !empty($_GET['page'])) { include_once('/pages/' . $_GET['page'] . '.php'); } else { include_once('/pages/index.php'); } ?>
<?php include_once('footer.php'); ?>
</div>
</body>
</html>
This uses a GET variable (?page=pageName) to select the page, which I usually use with a redirect rule in Apache.
Alternatively, you can fill the div content from a database. e.g.
<div id="content">
$pageId = $this->getPageId();
$db = new mysqli_connect('','','','');
$stmt = db->prepare("SELECT pageContent FROM pages WHERE page_id='$pageId'");
$stmt->execute();
$stmt->bind_result($curPageContent);
$stmt->fetch();
$stmt->close();
echo $curPageContent;
</div>
For an ajax call, you can do this:
$.get( "/get_page/",
{ page_id: '<?php echo $_GET['page']; ?>' })
.done(function(data) {
$('#content').html(data);
});
The folder structure I used for this is setup as:
root/
css/
js/
pages/
index.php
header.php
footer.php
index.php
Obviously you can use any structure you want, but this may help (although an assets/ folder is usually good :)
Good luck.

Using diff. stylesheets with diff. pages in Wordpress HTML5Blank template

I am making a site on Wordpress and plugging in my custom html and css to the HTML5Blank template. The css file is enqueued using WordPress functions into wp_head... I have tried inserting a conditional php if statement into head and it has no effect because
<?php wp_head(); ?>
is still being called. Removing this and keeping the conditional statement
<?php if( is_home() ) { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/front-style.css" />
<?php } else { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( '/style.css' ); ?>" />
<?php } ?>
still doesn't fix it. I want my homepage to call a different css file than the rest of the website. I'll only need potentially 2 css files and have already made another one called frontstyle.css in the same directory as the original css file.
Thanks in advance!
EDIT: have also been trying this but it isn't working either but actually seems the most promising.
?php if( is_home() ) { ?>
<?php get_header(); ?>
<?php } else { ?>
<?php get_header( 'other' ); ?>
<?php } ?>
with files header.php and header-other.php respectively. All I really need to change is the header. I plugged this into the page template and didn't get the response I needed but it did change a bit.

How to add css files using PHP inside the <head> tag?

Ok, let me explain:
I have a some files, something basic like this:
index.php
<html>
<head>
<title>Simple page</title>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php
<div class="thisWillBeBlue">Still not blue</div>
style.css
.thisWillBeBlue {background: blue}
Now the question: Using php I want to insert the style.css inside the head tag, calling it from the file home.php. Well, I came out with a solution, but it was not very effective:
index.php
<?php $css = array();
$css[] = 'linktothecss.css'
?>
<html>
<head>
<title>Simple page</title>
<?php
foreach($css as $item){
echo "<link rel='stylesheet' href='".$item."' />";
}
?>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
But the problem it is, If I call the css from home.php it will be added to the array later, therefore it will not be echoed inside the head tag. Any ideas?
You could do it using ob_start() and ob_end_flush() functions
e.g.
index.php
<?php
$csspage = "default.css";
function loadCSS($buffer) {
global $csspage;
return (str_replace('{{ css }}', $csspage, $buffer));
}
ob_start("loadCSS"); ?>
<html>
<head>
<!-- the string {{ css }} is just a placeholder that will be replaced
with the new value of $csspage defined later in the code, otherwise
it will replaced with its initial value (default.css)
-->
<link href="{{ css }}" />
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
<?php ob_end_flush(); ?>
home.php
<?php $csspage = "custom_style.css"; ?>
<div class="thisWillBeBlue">blue</div>
Further reference: http://it1.php.net/ob_start
I think you are looking for something like this ..(include a piece of code in their header files, so that it will allow you to add more stylesheets )
This will allow you to add more stylesheets to it on each page.
(add this to <head>)
<?php
if (!empty($styles) && is_array($styles)) {
foreach ($styles AS $style) {
echo '<link rel="stylesheet" href="/assets/css/'. $style .'">';
}
}
?>
You can put a variable at the top of an individual script if you need a specific stylesheet:
<?php
$styles = array('custom_style.css');
?>
CSS file references can be placed in the body of your code, if needed.
<body>
<link href="linktothecss.css" rel="stylesheet" type="text/css" />
<div class="thisWillBeBlue">
I'll be blue as soon as linktothecss.css finishes loading!
</div>
</body>
The only difference is that when in the HEAD, they are guaranteed to be loaded before the page is rendered. When they are in the BODY, there may be a split-second where they are still loading and the styles haven't been applied yet.
If you definitely want them in the HEAD, you could define the css requirements in a separate folder with the same file name, like so:
index.php:
<html>
<head>
<?php
include('css-requirements/home.php');
?>
</head>
<body>
<?php include('home.php'); ?>
</body>
</html>
and
css-requirements/home.php:
<link href="mycss.css" rel="stylesheet" type="text/css" />
<link href="myothercss.css" rel="stylesheet" type="text/css" />

PHP Include With Variety

I have a php include statement for the "head" of my website.
I am using the following code to call head.php...
<?php
include '../components/head.php'
?>
And in my head.php I have the following code...
<head>
<link rel="stylesheet" type="css" href="/style.css">
<title>Dummy Code</title>
</head>
How can I make it change the title by having a variable in my page on my page like Dummy Code | About being the title if I have $title = "About" on my webpage.
Is there anyway to do this?
They all belong to the global namespace, so you just can do this:
<?php
$title = 'about';
include '../components/head.php'
?>
head.php:
<head>
<link rel="stylesheet" type="css" href="/style.css">
<title>Dummy Code | <?=$title; ?></title>
</head>
But make sure that you understand: this is very simplified code and should not be used on the production projects.

Categories