PHP include() function [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In my web application there are 4 pages (About.html, Register.html,Quiz.html,Topic.html ) in each page navigation, header and footer sections are same. Now I want to put all these sections into single php include() functions(not multiple php files )

Then you may create a file perhaps named template.php which contains:
function printHeader($arg=null) {
echo "<div class='header'>Here is your header contents!</div>";
}
function printFooter($arg=null) {
echo "<div class='footer'>Here is your footer contents!</div>";
}
and just call them appropriately in each page having the same template, for example in the "about" page:
include_once "template.php";
// ...
printHeader();
// ...
printFooter();
// ...

1) About.html, Register.html,Quiz.html,Topic.html rename them to .php.
2) add 'sections.php'
function addHeader(){
echo "THIS IS HEADER!!!";
}
function addFooter(){
echo "THIS IS FOOTER!!!";
}
function addSparta(){
echo "THIS IS SPARTA!!!";
}
3) in Topic.php(and other files) add
include_once 'sections.php';
4) use those functions to print out what you need.
But still its not very good way to compose your pages.

let's consider an index.php file
in that file:
<html>
<head>
<title><?php echo !empty( $_GET['page'] ) ? $_GET['page'] : 'Home'; ?></title>
<!-- head info -->
</head>
<body>
<ul id="menu">
<?php foreach( array( 'About', 'Register', 'Quiz', 'Topic' ) as $page ) : ?>
<li><?php echo $page; ?></li>
<?php endforeach; ?>
</ul>
<div id="content">
<?php if ( !empty( $_GET['page'] ) ) : ?>
<h2><?php echo $_GET['page']; ?></h2>
<?php
$content = file_get_contents( dirname( __FILE__ ) . '/' . $_GET['page'] . '.html' );
echo reset( explode( '</body>', end( explode( '<body>', $content ) ) ) );
else : ?>
*** home page (default) content goes here (or include it from a separate file too) ***
<?php endif; ?>
</div>
<div id="footer">
<hr />
This is the footer
</div>
</body>
</html>
you must understand that given the broad scope of your question, and the fact you are using HTML files, this solution is incredibly basic. but it answers your question and/or plants the seed for learning from here.
for example, using is_file() to check that the page actually exists.

Related

PHP Read Variables from a second page (NOT get / post)

I am sure this is answered on this forum but I can't find the answer so here goes:
I have a webpage template.php. It has lot of code but in between there is:
<!-- Hero Content -->
<div class="home-content">
<div class="home-text">
<h1 class="hs-line-8 no-transp font-alt mb-50 mb-xs-30"> DISCOVER </h1>
<h2 class="hs-line-12 font-alt mb-50 mb-xs-30"> <?php echo $saved_data['title']; ?> </h2> <?php echo "<img src='$saved_data['builderimagepath']'>"; ?>
<div class="local-scroll"> Register <span class="hidden-xs"> </span> Learn More </div>
</div>
</div>
<!-- End Hero Content -->
Key lines are:
<?php echo $saved_data['title']; ?>
<?php echo "<img src='$saved_data['builderimagepath']'>"; ?>
Now the second web page read.php wants to read the variable names being used. Hence I want to know how to get "title" and "builderimagepath" in an array in read.php.
( I can rename the variables as a key or as a multi-dimensional array in template.php )
In read.php, this is what I have so far:
<?php
$url = 'index.php';
$content = file_get_contents($url);
$first_step = explode( '<?php echo $saved_data' , $content );
$second_step = explode("']; ?>" , $first_step[1] );
echo $second_step[0]; // Will Add to array to process
?>
We are NOT passing variables through GET / POST from template.php to read.php , but read.php wants to get it in an array. What I want to know:
1) Is there a better approach?
2) What is the best way to name variables in template.php , so its easier to access and process in read.php?
I don't think its relevant but $saved_data is an array coming from a file:
<!DOCTYPE html>
<?php
// Read from file
if (empty($_GET['file'])) {
// Use default file:
$filename = 'mydata.txt';
$saved_raw_data = file_get_contents($filename);
$saved_data = unserialize($saved_raw_data);
} else {
$saved_raw_data = file_get_contents($_GET['file']);
$saved_data = unserialize($saved_raw_data);
}
?>
In the file that you want to store the variables, return the values as an array and you can catch them in another file when including into a variable.
template.php
return [
'var1' => 'value',
'var2' => 'value2
];
read.php
$variable = include('template.php');
print_r($variable);

Wordpress: How Can You Get The URL Of The Next Child Page In A Sequence Of Child Pages

I'm finishing up my new portfolio and am trying to figure out a way to get a 'next' and 'prev' URLs for the child pages of the parent, which is called 'Work'.
The reason I want URLs is because the actual clickable link is an SVG at larger viewport sizes and I want to maintain all of my css access to the SVGs.
I'm including an image of how the navigation looks (looks like an aside, but it's actually a outside of the ).
I know that pages in worpress 'aren't meant to be paginated' but this is the same thing as creating a new post type and using the pagination there, except im doing some other things where I want access to templates. I tried using this plugin:
http://binarym.com/2009/next-page-not-next-post/
Which works, but I can't get my SVGs in there as opposed to text. If anyone has a way to replace the text in those strings with my SVG paths, that's an acceptable fix too. Here's a code snippet of what that looks like using that plugin:
<nav role="navigation" class="project-pagination">
<a href="/work">
<?php include (TEMPLATEPATH . '/images/_svgs/nav_gallery.svg'); ?>
</a>
<?php
$nextPage = next_page_not_post('Next Page', 'true', 'sort_column=post_date&sort_order=desc');
$prevPage = previous_page_not_post('Previous Page', 'true', 'sort_column=post_date&sort_order=desc');
if (!empty($nextPage) || !empty($prevPage)) {
if (!empty($nextPage)) echo $nextPage;
if (!empty($prevPage)) echo $prevPage;
}
?>
</nav>
Thanks all!
I found the answer here: Magic Town
<nav role="navigation" class="project-pagination">
<a href="/work" aria-label="View All Projects" alt="View All Projects">
<?php include (TEMPLATEPATH . '/images/_svgs/nav_gallery.svg'); ?><span>View All Projects</span>
</a>
<?php
$pagelist = get_pages("child_of=".$post->post_parent."&parent=".$post->post_parent."&sort_column=menu_order&sort_order=asc");
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<?php if (!empty($nextID)) { ?>
<?php include (TEMPLATEPATH . '/images/_svgs/nav_next.svg'); ?><span>Next Project</span>
<?php }
if (!empty($prevID)) { ?>
<?php include (TEMPLATEPATH . '/images/_svgs/nav_prev.svg'); ?><span>Previous Project</span>
<?php } ?>

A footer in different languages

I have a web in different languages. I make an include for the footer. The easiest way could be to have a different footer for each language. But is it possible to have just one footer and change the few sentences that are different in each language?
In all pages put the same include:
<?php include('footer.php'); ?>
Then, in the includes just change what is different. Something like:
<footer>
<?php echo $text; ?> <br><br>
</footer>
</body>
</html>
<?php
if ('<html lang="en">')
$text = 'Some text in English';
elseif ('<html lang="fr">')
$text = 'Français';
?>
(In each page I have the html lang= )
What is the better way to have a footer in different languages?
(I am just learning php, so please, just help me with the basics, where to begin)
Okay so first you need to create translation files for all languages you wish to support. Store them in "/lang/en.php" and "/lang/fr.php".
"lang/en.php"
<?php
return [
"title" => "My site",
"welcome" => "Welcome",
"goodbye" => "Goodbye"
]
?>
"lang/fr.php"
<?php
return [
"title" => "Mon site",
"welcome" => "Bienvenue",
"goodbye" => "Au revoir"
]
?>
Next, you include the appropriate language file in your php page:
"index.php"
<?php
$locale = $_SESSION['locale']; // this is "en" or "fr", depending on a choice the user made earlier
$lang = require("/lang/$locale.php"); // load "/lang/en.php" or "/lang/fr.php"
$user = $_SESSION['username']; // e.g. "Bart"
?>
<html>
<head>
<title><?php echo $lang['title']; ?></title>
</head>
<body>
<?php include('header.php'); ?>
<main>content</main>
<?php include('footer.php'); ?>
</body>
</html>
And in your header/footer you can just use $lang as well:
"header.php"
<header>
<p><?php echo $lang['welcome'] . ', ' . $user; ?></p>
</header>
It's important to know that you should include your language file only in pages that a user will view directly (i.e. don't include it in partial views like header.php)
You can create a poor man's translation function:
function translate($sentence, array $vars = null, $lang = 'en') {
static $table = array();
if ( ! isset($table[$lang])) {
$table[$lang] = require(ROOT."/lang/{$lang}.php");
}
$trans = isset($table[$lang][$sentence]) ? $table[$lang][$sentence] : $sentence;
if ( ! empty($vars)) {
$trans = strtr($trans, $vars);
}
return $trans;
}
You can then create some language files, such as:
<?php
// ROOT/lang/de.php
return [
'Welcome :name' => 'Willkommen :name',
'Thank you' => 'Danke',
];
And then in your scripts you can translate stuff:
<header>
<?php echo translate('Welcome :name', [':name' => 'Bob'], 'de') ?>
</header>
Instead of using a function you could also just include the language file and then use that.
<?php
// some-page.php
$lang = require(ROOT."/lang/{$_SESSION['user.lang']}.php");
$name = $_SESSION['user.name'];
?>
<header>
<?php echo str_replace(':name', $name, $lang['Welcome :name']) ?>
</header>
It will require you to do some more work, but if you find it to be more to your liking then okay.
You could set a session that contains the language such as:
<?php
session_start();
$_SESSION['language'] = "EN";
?>
Then in the footer:
<?php
switch($_SESSION['language']) {
case 'EN':
$sentences['site_slogan'] = "This is your site slogan";
$sentences['site_messag'] = "This is your site message";
break;
case 'FA':
$sentences['site_slogan'] = "This is your site slogan in FA";
$sentences['site_messag'] = "This is your site message in FA";
break;
}
echo $sentences['site_slogan'];
echo $sentences['site_messag'];

How to move <title> inside the <head>? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my website I have the same header for all pages. So I use the include function for the header. This includes all the <head></head>.
Because the <title> is variable depending on the page, I have put it just after the <body>.
I know this is wrong. How can I fix this and move the variable <title> inside the standard <head> ?
Thank you
In the code that you include for the header, use a placeholder for the title:
<head>
<title><?php echo $pageTitle; ?></title>
...
</head>
Then, in your page-specific code, just before the include statement, populate the $pageTitle variable:
$pageTitle = "Your Title";
include...
To make your included code more robust, consider setting a default title if none is provided:
<title><?php echo isset($pageTitle)? $pageTitle: 'Default Title'; ?></title>
Set a title variable before including header:
$title = 'Whatever you want';
include('header.php');
//rest of your page
Inside header.php:
<head>
<title><?php echo $title ?></title>
<!-- the rest of your stuff -->
</head>
Put a variable inside of your header file for your title, then define that value in your page before you include the header:
Inside your include:
<html>
<head>
<title><?= $page_title; ?></title>
Inside your main file:
<?php
$page_title = 'My page title';
include('header.php');
Make your included file only contain the contents of the <head> tag, and not the tag itself.
This way you can include the file between <head></head> tags and also have the title differ for each page.
Possibly set the page title in a variable ahead of your include, and have this in the head:
<title><?php echo htmlspecialchars($pageTitle, ENT_QUOTES); ?></title>
You can add a PHP variable above the header include such as
$title = 'test page';
Then in the header you use
<title>Main site title | <?php isset($title) ? $title : 'No title set' ?>
Depending on how you have things set up, you may have to set the title before you call your include.
PHP:
<?php
$title = "My Page Title";
include('path-to-your-header.php);
Your header would look something like this then:
PHP:
<html>
<head>
<title="<?php echo($title); ?>">
...
</head>
Or, if you are using a framework (codeigniter for example) you can use a tempalte - then in your controller method(s) you can set the page title.
PHP:
<?php
// Controller
public function index()
{
$data['page_title'] = 'Your Page Title';
$data['main_content'] = 'home'; // page content
$this->load->view( 'templates/public', $data ); // page template
}
// Tempalte
<html>
<head>
<title><?php echo ($page_title !== '' ) ? $page_title . ' | ' . SITE_NAME : '' . SITE_NAME; ?></title>
...
</head>
<body>
<?php echo $this->load->view( 'header' ); ?>
<?php echo $this->load->view( $main_content ); ?>
<?php echo $this->load->view( 'footer' ); ?>
</body>
in page.php
<?php
$title = 'This title';
include('header.php');
?>
in header.php
<html>
<head>
<title><?php echo isset($title) ? $title : '' ?></title>
</head>
<!-- rest of code goes below here -->

echo name of file 'including' not 'included'

Ok so I am making a website on each page where I want to include a file sidebar.php .
In sidebar.php I would like to echo the name of file that included sidebar.php .
Below are contents of sidebar.php, they return 'sidebar'.
<?php
$file = basename(__FILE__, '.php');
echo $file;
;?>
I found a similar question but the whole point is that I don't have to make no variables in on each page.
Excuse me for vague use of word 'include', I am using the it as the statement in php.
You could pass it to the sidebar page through a session variable:
<?php
session_start();
$_SESSION['filename'] = "thisFilesName.php";
include('../sidebar.php');
?>
sidebar.php:
<?php
session_start();
echo $_SESSION['filename'];
?>

Categories