PHP index.php包括结构[关闭]

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'");

}