I have this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<?php foreach ($posts as $post) { ?>
<div class="post">
<h3 class="title"><?php $post["title"]; ?></h3>
<p class="text"><?php $post["text"]; ?></p>
</div>
<?php } ?>
</body>
</html>
And I need to include it in other file and execute all the PHP code in it. How can i do it?
Include with absolute path :
<?php include($_SERVER["DOCUMENT_ROOT"].'/yourdirectory/yourfile.php'); ?>
use the include() function.
include("the file.php")
Related
Hello everyone.
I've got a small problem. Instead of making a $content variable and putting in the whole content there, I would like to set the page up like this (below), though the problem is that the php variables that is placed at the end (title, keywords, description, page), aren't working even though its in the index.php.
Question: How can I make the variables work without making a few head_template.php with the variables filled out already?
index.php:
<!DOCTYPE html>
<html class="html" lang="en">
<?php require_once "head_template.php"; ?>
<body class="<?php echo $page; ?>">
<?php require_once "navigation_menu_template.php"; ?>
<?php require_once "load_template.php"; ?>
<h1>Content</h1>
<?php require_once "cookies_script_template.php"; ?>
<?php require_once "footer_template.php"; ?>
</body>
</html>
<?php
$title = "QCG | Homepage";
$keywords = "QCG, Homepage";
$description = "QCG | Homepage - Description";
$page = "homepage";
?>
head_template.php:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="css/css.css">
</head>
You are setting them after they are used. If you put that block at the top of your page it will work.
<!DOCTYPE html>
<html class="html" lang="en">
<?php
$title = "QCG | Homepage";
$keywords = "QCG, Homepage";
$description = "QCG | Homepage - Description";
$page = "homepage";
?>
<?php require_once "head_template.php"; ?>
<body class="<?php echo $page; ?>">
<?php require_once "navigation_menu_template.php"; ?>
<?php require_once "load_template.php"; ?>
<h1>Content</h1>
<?php require_once "cookies_script_template.php"; ?>
<?php require_once "footer_template.php"; ?>
</body>
</html>
I need help for something please. I'm working on my structure, but I've some problems...
I created a page called "page-container.php" and the content is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $page_title; ?></title>
<link href="css/style.css" rel="stylesheet">
<link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
<?php include "includes/header.php"; ?>
<div class="wrapper">
<div class="page-content">
<?php if(isset($sidebar)): ?>
<div class="main-container">
<div class="main-content">
<?php endif; ?>
<h1 class="page-title"><?= $page_title; ?></h1>
I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES
</div>
</div>
</body>
</html>
You can see the "I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES".
What I mean is that when I write the content in my other pages (for example, my registration page), I want that all the content is placed at this location, like this:
http://prntscr.com/a3oz5k
All my other pages are like this (exemple with index.php):
<?php
$page_title = "Home";
$page_id = "home";
?>
<?php require "includes/page-container.php"; ?>
If I want to write some content in this page, I need to do like this:
<?php
$page_title = "Home";
$page_id = "home";
?>
<?php require "includes/page-container.php"; ?>
<div id="mycontent">
<p>Heellloo</p>
</div>
But look the result: http://prntscr.com/a3p32y
In the code source this is not normal... http://prntscr.com/a3p3kg
My content is below the html and body tag and I want it to be placed just below my h3 title. How can I do please?
Simple example using ob_start(), ob_get_clean().
<?php
ob_start();
?>
<div id="mycontent">
<p>Hello</p>
</div>
<?php
$page = ob_get_clean();
require "includes/page-container.php";
?>
And then in page-container.php just do echo $page;.
Your code will be still formatted as HTML in your editor instead if it would be in a variable.
You can also split your page-container.php into two parts and include the first part before the content and the second one after.
Reference: ob_start(), ob_get_clean()
You can keep another vartiable say $page_content and use as below :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $page_title; ?></title>
<link href="css/style.css" rel="stylesheet">
<link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
<?php include "includes/header.php"; ?>
<div class="wrapper">
<div class="page-content">
<h1 class="page-title"><?= $page_title; ?></h1>
<?= $page_content; ?>
</div>
</div>
</body>
</html>
And for some content in another page
<?php
$page_title = "Home";
$page_id = "home";
$page_content = "content";
?>
<?php require "includes/page-container.php"; ?>
`
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 = '';
}
I created this dynamic site. It works almost fine, but I have one problem.
How can I have a invidiual title for each page (in the html tag).
I hope you understand what I mean.
Take a look at my index.php
<html>
<?php
include_once 'config.php';
include_once 'includes/mysqlconnect.php';
$url_slash=$_SERVER['REQUEST_URI'];
$url= rtrim($url_slash, '/');
//$url = basename($url);
?>
<head>
<meta charset="UTF-8">
<title>KasCraft | *********</title> //Here I need a variable or some other to display the title for each site
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/reset.css">
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/<?php echo $theme;?>.css">
</head>
<body class="body">
<div class="container-all">
<?php include_once 'includes/header.php';?>
<div class="container">
<?php include_once 'includes/navigationbar.php';?>
<?php include_once 'includes/rightsidebar.php';?>
<div class="content"><?php
if ($url==''){
include_once "sites/home.php";
}
elseif (file_exists("sites/$url.php") && is_readable('/var/www/html/sites/'.$url.'.php')){
include_once '/var/www/html/sites/'.$url.'.php';
}
else {
include_once 'sites/404.php';
}
?></div>
<?php include_once 'includes/footer.php';?>
</div>
</div>
</body>
</html>
Can anybody please help me.
I'm very new to PHP, and just customizing my homepage. This is the main layout I have for all of my pages:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title_for_layout ?></title>
<?php echo $scripts_for_layout ?>
</head>
<body>
<div id="header">
</div>
<div id="content">
<?php echo $content_for_layout ?>
</div>
<div id="footer">
</div>
</body>
</html>
I want to have a global CSS file for all pages on my site called "style.css" which will be stored in app/webroot/css/style.css. I have tried doing this:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title_for_layout ?></title>
<link href="<?php $html->css('style', 'stylesheet', array('media'=>'all' ), false); ?>" rel="stylesheet" type="text/css" />
<?php echo $scripts_for_layout ?>
</head>
<body>
<div id="header">
</div>
<div id="content">
<?php echo $content_for_layout ?>
</div>
<div id="footer">
</div>
</body>
</html>
But it's not showing up in the output HTML. Any ideas?
Thanks!
you are definitely new to Cake :) here:
<title><?php echo $title_for_layout ?></title>
<?php
echo $this->Html->css('style');
echo $scripts_for_layout;
?>
That will output the full link element for you.