PHP index.php including structure [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 9 years ago.
Improve this question
I need help. On my site I use URL parameters to decide which other PHP files to include on the page. My first question is: What should be in index.php and what should be in the included PHP file?
On the Internet I found instructions, suggesting this structure for index.php:
<html>
<head>
<?php include 'header.php'; ?>
</head>
<body>
<?php include 'menu.php'; ?>
<?php include 'content.php'; /* Including page based on the parameters in the url */ ?>
</body>
</html>
With this structure, how can I change the data in the <head> section based on the content in content.php? For example, for index.php?option=article&id_article=1, I will include article.php and show the article with id 1. How, then, can I change the <title>, <meta>, etc content when <head> was written before including the article?
Thanks!

One option that is kind of ugly but will work is instead of having header.php echo have it simply set variables like $title and $meta[]. Also instead of having article.php from echoing return a variable like $html. Also in article.php you can then overwrite any of the variables set in header.php. Then you can construct your index.php like so:
<?php include 'header.php'; ?>
<?php include 'article.php'; ?>
<html>
<head>
<?php echo $title ?>
</ head>
<body>
<?php include 'menu.php'; ?>
<?php echo $html ?>
</ body>
</ html>
Or you can look into ob_start() and ob_flush() etc...

To be as simple as possible, you could make your header a function, then call the function elsewhere...
Example (untested):
function drawHeader($title, $desc = "", $keywords = "", $extra = "") {
?>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $desc; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
<link rel="stylesheet" type="text/css" href="path/to/my.css">
<?php echo $extra; ?>
</head>
<?php
}
The goal of the above would be so you could easily and quickly do something like...
<!DOCTYPE html>
<html>
<?php drawHeader("Home", "Some description", "Some, keywords, here"); ?>
<body>
<h1>Hello, world</h1>
<?php drawFooter(); // maybe a footer of some type? ?>
</body>
</html>
Or you could set the variables before calling the included file... and in the included file simply echo those values in the proper places.
There's tons of ways to do this, lots of standards and best practices, frameworks, templating systems, placeholders using output buffering, etc.

first the instructions you found have nothing to learn from
as second to get the content of the file article.php
using the url index.php?option=article&id_article=1
you will need to use the $_GET['id_article']
Example :
$page = $_GET {'id_article'};
if (isset($page)) {
$url = $page.".php";
if (file_exists($url)) {
include $url;
}
and you can use database for storing the articles and use the query then using
if ($_REQUEST['id_article'] == $page) {
$querythearticle = mysql_query("select tablename from database_name where id_article='$page'");
}

Related

How to set title after html

For example you have output <head> tag and don't know title at that moment.
You will know title at the end of code.
How to set title from that place?
Simplified template looks like:
<html>
<head>
<title>We don't know title</title>
</head>
<body>
<article>
<!--at this place we include code, that output article and know title-->
</article>
</body>
</html>
Thanks.
First thing to try, the code that is calculating the title I assume occurs after you have outputted the <head> HTML. Are you able to move this code to before the <head> and store any HTML in a variable to be printed after?
Otherwise you would need to set the answer using a script that executes when the page is fully loaded. Something like
//index.php
<head>
<title></title> //blank title
</head>
<body>
<?php
//code that finds title
$title = 'I am set later';
?>
//All other page output
<script>
//This will run at the end of the body after everything else.
document.title = <?php echo $title; ?>
</script>
</body>
</html>
edit: nicely caught

In PHP how can access a property in an included file that was defined in the calling file?

If I have two files
main.php
<?php
$title = "thisTitle";
include $_SERVER['DOCUMENT_ROOT']."/aatemplate.php";
?>
aatemplate.php
<html lang="en">
<head>
<?php include $_SERVER['DOCUMENT_ROOT']."/common/head.php";?>
<title>$title</title>
</head>
</html>
How can I make the '$title' in the template file be replaced with the $title set in the main.php so that it becomes:
<title>thisTitle</title>
If you include a file that's will be access to main file & previous included files variables.
Change
<title>$title</title>
to
<title><?php echo $title; ?></title>
You can refer to PHP variables that are declared by using the echo statement
Replace with <?php echo $title; ?>

file_put_contents in head of another file using php

So, Im creating a library for other uses, but how can I make content from a file specificly go within the <head> or <body> html tag attribute etc...
for example, this is what im trying to make.
<html>
<head>
<?php include('content/starter/library.php'); ?>
<!-- From that included file, theres a script that put content in the head.-->
</head>
<body>
<!-- From that included file, theres a script that put content in the body -->
</body>
</html>
Im just trying to find another way instead of making multiple files for specific sections and do
<html>
<head>
<?php include('content/starter/library_head.php'); ?>
</head>
<body>
<?php include('content/starter/library_body.php'); ?>
</body>
</html>
Which I don't really want to do. Im not very good with javascript so, There no hope of me trying to figure out how to do this with javascript. Thanks for the answers in the future.
If you want to use one file (as your questions suggests) then one method is to create variables or functions in your library.php file and then echo them in your template
// contents of the library.php file...
<?php
$head_content = "put your <head> content here";
$body_content = "put your <body> content here";
?>
// your HTML file...
<?php include('content/starter/library.php'); ?>
<html>
<head>
<?php echo $head_content ?>
</head>
<body>
<?php echo $body_content ?>
</body>
</html>
UPDATE
To answer the question in your comment, here's an example using a function. You can put all of your code in a function and then just echo that anywhere in your document.
<?php
// contents of library.php...
function head() {
$return = '<link href="file.css" rel="stylesheet">';
$return .= '<link href="another_file.css" rel="stylesheet">';
return $return;
}
// your HTML file...
<html>
<head>
<?php echo head(); ?>
</head>
PHP functions explained: http://www.w3schools.com/php/php_functions.asp

Return certain parts of a page with php

How can I return only the body of a PHP file?
An example of how the file would look like:
<html>
<head>
<title>Test Page</title>
</head>
<body>
<div class="content"><p>Hello World</p></div>
</body>
</html>
How can I only return the body of the page with PHP?
<div class="content"><p>Hello World</p></div>
I would use a GET variable to indicate I only require the body, else the entire page is needed.
<?php
// read and interpret your GET-input variable:
$body_only = isset($_GET['body_only']); // you can enhance/change this condition to match your needs
if(!$body_only)
{
// if NOT only body was requested, output intro (head, etc.):
?>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<?php
}
// always output body content:
?>
<div class="content"><p>Hello World</p></div>
<?php
if(!$body_only)
{
// if NOT only body was requested, output outro:
?>
</body>
</html>
<?php
}
?>
Personally, I prefer usual C-style syntax in such cases (i.e. <?php if(...) { ?> ... <?php } ?>), so I've used it in the code above.
PHP also offers alternative syntax, which you may find more readable:
<?php if (...): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>
Read this for more info on escaping PHP code from HTML.
Also, consider using a template engine.
There's the PHP DOM you can use, it is quite simple and doesn't really need any explanation however if you find yourself having trouble don't worry to write a comment!

PHP: How can I reference variables from an included file before it's been included?

How can I reference variables from an included file before it's been included? Or can I somehow include the file (so I can lead its variables later) before its HTML is literally inserted into the body tag? Or can I contain all of home's body content in one big variable that I can echo as well in the index?
Here's what I'm trying to do:
index.php
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php
<?php
$title="home page";
$description="this is the home page";
$keywords="home, awesome, yes";
?>
this is the home page content that gets inserted into the body!
Just move the include statement to the top of the file.
This will expose all values, functions and variables to all subsequent lines.
<?php include 'home.php'; ?>
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
</body>
</html>
Short answer version: You can't. You'll get an 'Undefined variable' notice if you do that.
I find it is usually much more convenient to have a header.php (and a footer.php for that matter) which gets included in the index, home, contact or whatever other file. The advantage is that you don't have redundant code, and if you need to make a modification in the header or footer, you need to only modify one file.
So for example, 'about_us.php' would look like:
<?php
include('path/to/header.php');
#body goes here
include('path/to/footer.php');
?>
And your header would be something like:
<?php
$title = ucfirst(str_replace('_', ' ', substr(basename($_SERVER['PHP_SELF']), 0, -4));
?>
<html>
<head>
<title><?php echo $title; ?> page</title>
<meta name="description" content="this is the home page" />
<meta name="keywords" content="home, awesome, yes" />
</head>
<body>
The $title variable will be the file name, minus the extension, with all underscores replaced by spaces and the first letter of the first word capitalized. So basically about_us.phpwould be converted into "About us". This is not necessarily a general solution, but I gave it as an example keeping in mind that you wanted to use a dynamic title in your original example. For dynamic description and keywords, based on the file name you could also assign different values with the help of a switch() statement.
UPDATE:
Another solution, although kind of the reverse of what you're asking, but at the same time much closer to what you're looking for would be to write the header.php like
<html>
<head>
<title><?php echo $title; ?> page</title>
<meta name="description" content="<?php echo $desc; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>
... the footer like ...
</body>
</html>
... and then include them in your other files:
<?php
$title = 'Your title';
$desc = 'Your description';
$keywords = 'The, big, brown, fox, jumps, over, the, lazy, dog';
include('path/to/header.php');
?>
<!-- body goes here -->
<?php
include('path/to/footer.php');
?>
This way, you are assigning all the variables BEFORE you are including the files in which they are being referenced, you have distinct files for all the links and you don't need fancy switches. Also as a side note, wrapping the body's HTML in PHP is simply bad practice. Try to keep the HTML separated from the PHP as much as possible in general. It will help both you, and whoever is going to do work on the code in the future.
Hope this helps !
I would have a look at using a template system. Separating your code from the content will save you a lot of trouble in the future. it will also allow you to change the html template easily in the future. plus you can see your template without having to run the php code.
have a look at smarty templates
http://www.smarty.net/
you would then build a template file: "template.tpl"
<html>
<head>
<title>{$title}</title>
<meta name="description" content="{$description}" />
<meta name="keywords" content="{$keywords}"/>
</head>
<body>
{$home_content}
</body>
</html>
and some php code to run:
<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->assign('title' , 'Your title');
$smarty->assign('description' , 'Your description');
$smarty->assign('keywords' , 'The, big, brown, fox, jumps, over, the, lazy, dog');
$smarty->assign('home_content' , 'this is the home page content that gets inserted into');
$smarty->display('template.tpl');
?>
And that is just scratching the surface of what a templating system can do. you can repeating or optional bocks, include other templates, etc etc.

Categories