0

What i'm i doing wrong? here is my code

FUNCTIONS.PHP

<?php
function get_header(){
    include 'header.php';
}
function get_footer(){
    include 'footer.php';
}

HEADER.PHP

<?php function set_title($title){
    echo $title;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php set_title();?></title>
    <meta charset="UTF-8">
    <meta name="description" content="Photography site" />
    <meta name="keywords" content=" Angelic Photography" />
<!--    <link rel="stylesheet" href="assets/css/main.css"/>-->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
     <link rel="stylesheet" href="assets/css/grayscale.css"/>
</head>

HOMEPAGE.PHP

<?php 
require 'functions.php';
$title = ("homepage");
get_header(); 

php get_footer(); ?>
5
Contributors
10
Replies
35
Views
2 Years
Discussion Span
Last Post by rhodoscoder
0

SOrry rhod. But I fail to see the point of this.

Perhaps you'd be better off building a pagemanager class. From your code you look to be supplying the data from the page itself, which makes me think that you may as well be providing static markup.

0

I don't see the need for functions.php here.

I'd do homepage.php like this:

<?php 
$title = "homepage";
include 'header.php';

echo "<div>page body here</div>";

include 'footer.php';
?>
0

Yes, thaat's the point. Unless you're running everything through a front "index.php" page or a page builder, or a template engine, there's little for php to actually do here.

0

If I am guessing correctly, you are just trying to get some basics to work before you implement the actual functionality. There's nothing wrong with that!

The problem basically is just that you need to declare the $title variable as global in each of the functions that references it. I would do away with the set_title function completely and change get_header:

<?php
function get_header(){
    global $title;
    include 'header.php';
}
function get_footer(){
    include 'footer.php';
}

Then in header.php:

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title; ?></title>
    <meta charset="UTF-8">
    <meta name="description" content="Photography site" />
    <meta name="keywords" content=" Angelic Photography" />
<!--    <link rel="stylesheet" href="assets/css/main.css"/>-->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
     <link rel="stylesheet" href="assets/css/grayscale.css"/>
</head>

The key thing to remember is that when you include() or require() a file, the code in it is processed exactly as if you had pasted in that code in the same position as the include() call in the parent file. In this case, that means that in order to access the $title variable you either have to use a global or pass it in as an argument.

A better approach
You may want to consider not relying on globals at all, however, in which case you would want to pass the page data to the get_header() and get_footer() functions like this:

<?php
/**
 * Render the header markup for a basic page.
 * @param $page array of data for the page.
 * @return nothing
 */
function get_header($page) {
    include 'header.php';
}

/**
 * Render the footer markup for a basic page.
 * @param $page array of data for the page.
 * @return nothing
 */
function get_footer($page) {
    include 'footer.php';
}

I added some phpdoc comments, always a good idea with any function (but never required for the code to work, it's more for you later to remember how it works).

Some might argue that you don't need the get_header() and get_footer() functions, and in fact that's correct, but I like making things flexible. If in the future you want to modify how this works (for instance, maybe you want to store the header in mecached to save on database calls or something), you can do so more easily by putting it in a function and there is no harm in doing that.

Anyway, with the above changes header.php would then use an array index into the $page array:

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $page['title']; ?></title>
    <meta charset="UTF-8">
    <meta name="description" content="Photography site" />
    <meta name="keywords" content=" Angelic Photography" />
<!--    <link rel="stylesheet" href="assets/css/main.css"/>-->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
     <link rel="stylesheet" href="assets/css/grayscale.css"/>
</head>

Then in homepage.php you would do something like this to call the new functions:

<?php 
require_once "functions.php";

// Setup page data
$title = array(
    "title" => "homepage",
    "body" => "Body text",
);

// Render the page header
get_header($page); 

// Other render calls as needed for the particular page

// Render the page footer
get_footer($page);

Edited by Isaac_4

0

That's a lot of work ^^ for an assumption, heh heh. Perhaps we should wait for the OP to reply.

0

Actually, the problem exist in the coding itself. As you see

<?php function set_title($title){
    echo $title;
}
?>

The function expect a parameter and the code calling it is <title><?php set_title();?></title> did not parse in the parameter to the function for it to echo.

0

I didn't go that far. It was a mess.

0

I appreciate both Isaac 4 and ips for understanding this Simple code and its use. Isaac 4 thank you for all the time you spent on it and it worked flawlessly, i didn't think of creating a global variable, I kept tying to pass it in the get header function function get_header($title){ include "header.php";}. THANKS!!

0

However diafol was right about a pagemanager class, still not good with PHP OOP but I'll dive into it , see what i can come up with and come back here if I get stuck.

0

includer.php

<?php
Class Includer {


function get_header($title){

    include 'header.php';
}
function get_footer(){
    include 'footer.php';
}
}

Header.php

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $title; ?></title>
    <meta charset="UTF-8">
    <meta name="description" content="Photography site" />
    <meta name="keywords" content=" Angelic Photography" />
<!--    <link rel="stylesheet" href="assets/css/main.css"/>-->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css"/>
     <link rel="stylesheet" href="assets/css/grayscale.css"/>
</head>

Homepage.php

<?php 
require_once "includer.php";
$includer = new Includer;

$includer->get_header("homepage"); 

$includer->get_footer(); ?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.