how to include the dynamic page in the template php? - php

I have 3 pages template.php and page1.php and page2.php , and a page1 and page2 menu in template, I want when I click on page1 in the template menu it includes the content of page1 in template and when i click on page2 it included contents of page 2.
template.php
<html>
<head>
<title>template</title>
</head>
<body>
<h1>TEMPLATE</h1>
page1
page2
<?php include($content); ?>
<h1>TEMPLATE</h1>
</body>
</html>
page1.php
<h2>page 1</h2>
page2.php
<h2>page 2</h2>

You can use include.
So in your example, it will be:
<html>
<head>
<title>template</title>
</head>
<body>
<h1>TEMPLATE</h1>
page1
page2
<?php echo $content; ?>
<?php include('page1.php') ?>
</body>
</html>
You can also use require. The difference is that if you use require, your code will fail if the file is not found. With include, the code still executes no matter what. So is up to you here.

Related

How to display one view in another view page

i have this below code where i want to display only "Welcome to my 3rd Blog!" of "blogvieww.php" in
"blogview.php" using codegniter. But the below code what i tried with is that, even "Welcome to my 2nd Blog!" of "blogvieww.php" is getting displayed in "blogview.php".
actually i just want to display only "Welcome to my 3rd Blog!", how to do this can any one tel me please im not getting where im going wrong.
Blogcontroller.php(controller file)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blogcontroller extends CI_Controller {
public function index()
{
$data['blogvieww'] = $this->load->view('blogvieww', '', TRUE);
$this->load->view('blogview', $data);
}
public function blogvieww()
{
$this->load->view('blogvieww');
}
}
?>
blogview.php (view file)
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<div><?php echo $blogvieww; ?></div>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>
blogvieww.php (view file)
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<h1>Welcome to my 2nd Blog!</h1>
</div>
<div>
<h1>Welcome to my 3rd Blog!</h1>
</div>
</body>
</html>
Your can do simply using $this->load-view('view_name'), like following :
view1.php
<p>View 1</p>
view2.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
// Load View 1
<?php $this->load->view('view1'); ?>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>
I am not sure what you want, but you can anytime to var_dump the value of $data['blogvieww'], and if you want make a view section, don't use double tag

Codeigniter best way to handle controller calling views

i just want to know if this is a good way in adding fragmented views.
lets say "header.php" is like this
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Admin Theme v3</title>
some links..
</head>
</html>
then "body.php" is like this
<!DOCTYPE html>
<html>
<body>
lots of stuff..
</body>
</html>
lastly "scripts.php"
<!DOCTYPE html>
<html>
<body>
some scripts..
</body>
</html>
then in my "MyController.php"
$this->load->view('header');
$this->load->view('body');
$this->load->view('scripts');
The best way I find is create a default view.
views > default_view.php
views > includes > header_view.php
views > includes > footer_view.php
views > information > contact_view.php
On that view
<?php
$this->load->view('includes/header_view');
$this->load->view($content_page);
$this->load->view('includes/footer_view');
?>
Then on the controller to load view this way you do not have to load the header and footer views all the time.
Following the Codeigniter StyleGuide
Filename: Example.php
<?php
class Example extends CI_Controller {
public function index() {
// Add any other variables
$data['content_page'] = 'information/contact_view'; // This will be your content page example
$this->load->view('default_view', $data);
}
}
There are too many redundant tag like <html>,<body> try separate it
header.php
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Admin Theme v3</title>
some links..
</head>
<body>
body.php
lots of stuff..
scripts.php or footer.php
some scripts..
</body>
</html>
It is not good way. Except in some cases (using frames for example), document should have just one declaration and one pair of open/closed html tag.
It could be something like:
header.php
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Admin Theme v3</title>
some links..
</head>
<body>
some_page.php
<div class="container">
<div class="row">
//some stuff and forms here
</div>
</div>
footer.php
<div class="footer">
//©2016
</div>
</body>
</html>

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.

PHP: Is it possible to modify varibles of an included php file?

I am new to php.
I have a header.php with variables which I want to be uniquely set depending on which php page includes the header.php. I want each page that loads the header to display unique data in the header - current page info.
Currently I have something like the following:
header.php
<?php $pageInfo = "";?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/header.css">
<title></title>
</head>
<body>
<div id="header-box">
<div id="header-wrapper">
<div id="logo-box">
</div>
<div id="info-box">
<div>
<p><?php echo $pageInfo ?></p>
</div>
</div>
</div>
</body>
</html>
page1.php
<body>
<div><?php $pageInfo = ":D"; require 'shared/header.php'; ?></div>
<div><?php include 'shared/menu.php';?></div>
</body>
Yes that is possible. Include works like as you have have merged multiple pages (Scripts) into one. So if you want to change the value of variables in header.php before including header.php define them.
ex:
<?php
$pageInfo = 'Test page';
require_once 'header.php';
?>
Note: Do not declare $pageInfo = '' in header.php else it will over write it. You can do as follow in header.php:
if(!isset($pageInfo)) {
$pageInfo = '';
}

How assign titles for each page by smarty?

i have alot of pages and i want assign title for each page how can id?
i assign home page but cannot assign pages. the variable will add in header.tpl
pages(contact.php,order.php,products.php)
i'm using {$hometitle}
$smarty->assign('hometitle',$hometitle);
Not sure I understood the question:
Does this help?
In your php:
<?php
$smarty = new Smarty();
$smarty->assign('hometitle',$hometitle);
$smarty->display('index.tpl');
?>
In your templates:
<html>
<head>
<title>{$hometitle}</title>
</head>
<body>
<!--body content here-->
</body>
</html>

Categories