MultiDev
MultiDev

Reputation: 10649

How can I use variables defined later in the page? PHP

This is how I have my files setup:

INDEX.PHP:

    include($sys->root_path.'sections/start.php'); // Includes everything from <html> to where we are now

    if (isset($_GET['page'])) {
      $page = $_GET['page'].'.php';
    } else {
      $page = 'index.php';
    }

    if (isset($_GET['cat'])) {
      $cat = $_GET['cat'];
    } else {
      $cat = '';
    }

    include($sys->root_path.'/content/'.$cat.'/'.$page);

    include($sys->root_path.'sections/end.php'); // Includes everything from here to </html>

To view this page, I visit: example.com/index.php?cat=red&page=car This will show me a page with the content of the file at:

/content/red/car.php

The problem I am having is that I want to specify a title, meta description, etc. for each page, and this is outputted to the page in start.php- before any specific data for this particular page is called.

What I was hoping to do is something like this:

/CONTENT/RED/CAR.PHP:

<?php $page_title = 'Title of the page'; ?>
<p>Everything below is just the page's content...</p>

How can I use this page specific data in the <head> of the site, when all that data is grabbed before the contents of this specific page?

Upvotes: 1

Views: 135

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98961

The correct way to do what you're trying to do is with a database and apache url_rewrite. My answer is just a fix for your problem.


Step 1

Include start.php below the if statment, this way when you include start.php you already know which page you need, like this:

if (isset($_GET['page'])) {
  $page = $_GET['page'].'.php';
} else {
  $page = 'index.php';
}

if (isset($_GET['cat'])) {
  $cat = $_GET['cat'];
} else {
  $cat = '';
}

include($sys->root_path.'sections/start.php');

Step 2

Now, inside the start.php use a switch:

<?php
switch ($cat) {
    case "blue":
        $title = "The car is $page";
        break;
    case "green":
        $title = "The car is $page";
        break;
    case "red":
        $title = "The car is $page";
        break;
    default:
        $title = "Welcome to ...";
} 
?>

    <!DOCTYPE html>
    <head>
    <title><?php echo $title ?></title>
    </head>
etc...

Upvotes: 1

Braulio J. Solano
Braulio J. Solano

Reputation: 161

You could do something like:

switch($_GET['page']) {
    case 'car':
        // Here you could have another conditional for category.
        $page_title = 'Title of the page';
        break;
    // Other cases.
}
include($sys->root_path.'sections/start.php');

And in start.php you could have something like:

<title><?php echo $page_title; ?></title>

I must advise against that way of including content. It is insecure. Someone could browse your server files or include something you don't want included. One should never include files that way (through get variables) unless one always filter that variable through a regular expression or something else.

Upvotes: 1

kojow7
kojow7

Reputation: 11404

My recommendation would be to use an MVC approach with either a function to pass the parameters or OOP with a setter function.

Upvotes: 0

Related Questions