How pages render in a home page with PHP? - php

In ASP.NET MVC applications can use dynamic pages as follows:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div id="menu">
<ul>
<li>Home</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
<div id="content">
#RenderBody()
</div>
</body>
</html>
with RenderBody () method that renders the content of a page in another.
How I can render the content pages on a main (index.php) page PHP Case 1, with the Codeigniter Framework and Case 2, normally without the framework?
Excuse my English, as it is not my native language!!
Regards,
Mauriciohz

In Normal PHP,
You can use any of include, require , include_once or require_once.
In Codeigniter,
You can load content part alone with use of below code:
$this->load->view("view_file_name");
here view_file_name is your view file which exist inside the applications/view folder.
You have to give without extension.
For ex:
your content file name is "content.php". applications/view/content.php
You can load as:
$this->load->view("content");// without ".php" extension.

You could use the include or require function in php.
Example
include('path');
require('path');
include_once('path');
require_once('path');
The _once would make sure that the file is never included multiple times throughout the script.

Related

Is it possible to link to a header.php and footer.php from different folder sources?

I'm a beginner at PHP.
I have multiple webpages residing in different locations. So when I wish to link to header.php and footer.php from the webpages in different folders, is it possible to do so? As shown in the picture, I have to create three different folders, containing same files, header.php and footer.php, to be able to link from three different sources.
With Best regards!
Yes it is possible to use a single footer.php and single header.php files and load them anytime you need.
What I would suggest you can do is that you create an include folder, then inside the include folder create another folder called common where by you will place website that elements that are always the same throughout the website ie, footer and header.
then I would also place a functions file inside the includes where I will place my website functions. Included in this function file is a function that I will use anytime I want to use the header.php and footer.php files.
Functions.php
<?php
function loadView($viename,$meta=[]){
//load footer/header page
include_once "common/$viename.php";
}
//any other functions
The loadView() function is used anytime you want to load these two dynamic files. This functions takes two parameters 1 optional. The first parameter is the name of the view you want to load which is header or footer then the second optional is the meta information important for the header file, as the page title and meta description needs to be dynamic and change according to the page.
header.php
<!DOCTYPE html>
<html>
<head>
<title><?=$meta['pagetitle']?><!-- Dynamic page title --></title>
<meta name="description" content="<?=$meta['pagedescription']?>"><!-- Dynamic description -->
<!-- load your styles -->
</head>
<body>
<header>
<nav>
<!-- Your page navigation -->
<ul>
<li>Home</li>
<li>About</li>
<li>Another Page
</ul>
</nav>
</header>
footer.php
<footer>
footer content
<p>© website name <?=date('Y')?>
</footer>
</body>
</html>
Main website pages
Your main website pages are pages such as index, about,services etc.
In these pages you would load the functions file, then be able to load the header and footer.
index.php
<?php
include 'includes/functions.php';
//meta info
$meta = array(
'pagetitle' => 'Welcome to my site | site | bla bla',
'pagedescription' => 'This is your website description'
);
loadview('header',$meta); //load heade
?>
<section>
<div id="content">
<p>Page Content</p>
</div>
</section>
<?php
loadview("footer"); //load footer
?>
About Page
<?php
include 'includes/functions.php';
$meta = array(
'pagetitle' => 'About Us',
'pagedescription' => 'This is about page'
);
loadview('header',$meta);
?>
<section>
<div id="content">
<p>Page Content</p>
</div>
</section>
<!-- load footer -->
<?php
loadview("footer");
?>
Hope this gives you the idea on how you could achieve your goal, there are many ways you can achieve this.
Let me know when you need any help
Assign values for $h_path and $f_path dynamically.
<?php
$h_path = '';
$f_path = '';
include($h_path.'header.php');
include($f_path.'footer.php');
?>
My apologies for not providing enough information about the issues. My issue is that, when the index.php refers to the header and footer by "includes/header.php" and "includes/footer.php" respectively, and other webpages are located inside another folder which needs to access the includes folder via "../includes/header.php". There is no problem while referring to the files but the issue occurs when headers.php targets the webpages inside when it is written to only work with index.php. For example, would only work on index.php but not on the php files inside the folder which needs , But I'll try with $h_path = ''; and $f_path = '' soon.

What is the best practice to include header and footer in PHP?

everytime I create a website I usually end up by creating a simple index.php file that will load the requested pages.
Example:
include ('header.php');
if(isset($_GET['page']))
{
$page = $_GET['page'];
$display = $page.'.php';
}
else
{
include ('homepage.php');
}
include ('footer.php'); ?>
THE PROBLEM:
If I want to create a connection.php file that will access my database usally it won't work in other pages beacuse I have to rewrite "include('connection.php')", in every single file that isn't the index.php.
THE REQUEST:
How can I embed header, footer, connection, etc... In a proper and safe way ? So I don't have to include it in every other file ?
How do you usually include the header and the footer in every page, in order to create a dynamic website ?
There's multiple ways to fix this.
Use a templating engine, like Blade or Twig.
Create an autoloader
Blade will allow you to make a layout, which can look like this:
<html>
<head>
<title>App Name - #yield('title')</title>
#yield('css')
</head>
<body>
<!-- Include all your files -->
#php
include('myfile.php');
#endphp
<div class="container">
#yield('content')
</div>
#yield('js')
</body>
</html>
Then for every other page you can create a blade file that extends the layout.
#extends('layout.file')
#section('title', 'Page Title')
#section('content')
<p>This content will be placed in the container in the layout.</p>
#endsection
You can write an auto-loader by yourself or use a pre-written one. I'm not going to attempt writing one because I usually go with the pre-written ones.
This should give you an idea though.

Sidemenu In codeigniter

I just try to bulid control panel using MVC (codeigniter), I just need way to how make a side menu in home page and when I click in the link in menu the page open in the right side, and the menu stay in the left side.
I need coorect way to do this using MVC.
I use this simple way to make Views to make exactly what your asking.
<?php
//This is mainpage.php
$data['page_id'] = $_GET['page_id'];
?>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<div>
<!-- HERE Comes Side Bar -->
<?php $this->load->view('sidebar'); ?>
</div>
<div>
<!-- HERE Comes Main Content -->
<?php $this->load->view('maincontent',$data) ?>
</div>
</body>
</html>
Mainpage.php is a master page who holds code for both, side menu as well as the content page
This code if for side bar sidebar.php in view folder
<div>
<ul>
<li>Dashboard</li>
<li>Settings</li>
<li>Logout</li>
</ul>
</div>
sidebar.php page only holds different links, but all links have GET variable named "page_id" which decides which page is to be displayed in the content page.
Now, in mainpage.php you can notice that maincontent.php is loaded as view with passing $data which has page_id as variable, which is drived from sidebar. this will help to display the content in content side.
This code is for maincontent.php in view folder
<?php
if(file_exists(APPPATH.'views/'.$page_id.'.php')){
$this->load->view($page_id);
}else{
show_404();
}
this has always worked for me in non ajax page displays...so will work for you to.
Thanks...

how to insert php code in one-page bootstrap template

actually am working on a php website, and am finished with it, and I want to make a good style to my project, I found many templates and am interested to “one-page bootstrap templates”, i have downloaded ones and don't know how to use them, I want to put my php pages in one of them but I don't know how to do that.
if you are coding php without any framework and template engine you can combine php and html( here bootstrap template) like this :
<html>
<body class="container">
text
<?php if $list=true ?>
<ul>
...
<ul>
<?php endif ?>
</body>
<html>

Get URL of current page using php and ssi

So I got a webpage written in HTML. Menu, Header and Footer are included using SSI (server side includes; .shtml). The website is running on an Apache server.
Now I want to build a language changer that changes the page from [server]/de/index to [server]/en/index in case you change the language from German to English.
Therefore I want to include this into the header. I already found out, that using PHP for getting the current URL is the best way to do it and within the index.html file for example the code is working.
But when I include the code into the SSI, it is returning nothing.
Any clues?
PHP-Code:
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
Index.html:
<body>
<!--#include file="res/Common/header.shtml" -->
<nav>
<div id='cssmenu'>
<!--#include file="res/Common/menu.shtml" -->
</div>
</nav>
<!--#include file="res/Common/footer.shtml" -->
</body>
</html>
header.shtml:
<div align="right"><p style="margin-top:5px; margin-bottom:5px"><font size="2">Language Selector</font></p></div>

Categories