Dynamic PHP headache!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mlynch1985
Forum Newbie
Posts: 3
Joined: Fri Jul 10, 2009 4:36 pm

Dynamic PHP headache!

Post by mlynch1985 »

Hi all,

I have a dynamic php query that I simply cannot find the answer to.

I am running a site using dynamic php (i didnt design the core)

It runs from a default.php page which is the template

from this the index.php loads all the content in the layout default.php directs.

and from index.php I'm using .htaccess to rewrite pages which 90% are index.php=RANDOMPAGE.php

I am trying to set up something which can change the title tags for each page, however as they are feed from the master index.php page I'm having no joy.

The best suggestion I have found is to use header.php and call for it in the index.php, however it doesnt work becuase the "randompage.php" is loaded further down the page, thus the header is called before the content and no title is loaded.

I cant load the content before the header.php so I'm stuck.

my site is http://tinyurl.com/lhod33 if this will visually explain how i'm set up.

Do you have any suggestions as to how I can get around this?

Cheers,

Matt
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Dynamic PHP headache!

Post by Eric! »

Your link didn't work for me.

Where are the title tags located now that you want to change? Are they static right now or are they echoed out from variables?

How do you want to control the page title? Do you want index.php to determine the titles or do you want each page to have their own configuration (easier to manage)?

Many sites use a small php file with all the HTML header info in it, including variables with the $title, $keywords, $etc meta tag crap. Then the php file being requested sets the $title, $keywords, whatever else and then an include at the beginning that includes "header.php" and header.php echos out all the header info with the $titles, $keywords, $etc. echoed as values set by the calling script (in this case header.php). This way you can have header.php do a lot of repetative work and all you have to do is set a few variables for each page and include header.php.
mlynch1985
Forum Newbie
Posts: 3
Joined: Fri Jul 10, 2009 4:36 pm

Re: Dynamic PHP headache!

Post by mlynch1985 »

Eric! wrote:Your link didn't work for me.

Where are the title tags located now that you want to change? Are they static right now or are they echoed out from variables?

How do you want to control the page title? Do you want index.php to determine the titles or do you want each page to have their own configuration (easier to manage)?

Many sites use a small php file with all the HTML header info in it, including variables with the $title, $keywords, $etc meta tag crap. Then the php file being requested sets the $title, $keywords, whatever else and then an include at the beginning that includes "header.php" and header.php echos out all the header info with the $titles, $keywords, $etc. echoed as values set by the calling script (in this case header.php). This way you can have header.php do a lot of repetative work and all you have to do is set a few variables for each page and include header.php.

Hi Mate,

The Title is held within the default.php/html it was html but it can be a php if necessary

The issue is, this file is the template/layout which calls the index.php to the stage like this:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>[*title*]</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Football Betting Advisers/Sports Analysts with free bets, football betting tips, La Liga, Serie A, Bundesliga, Premiership, Championship, Ligue 1, UEFA Cup and Champions League" />
<meta name="keywords" content="football betting, online football betting, european football betting, uk football betting, betting advice, football betting advice, football betting advice, betting tips, betting tip, football betting tip, football betting tip, free betting tip" />
<meta name="author" content="Moneyline Media" />
<meta name="revisit-after" content="1 days">
<meta name="robots" content="index, follow" />  
<script type="text/javascript" language="javascript" src="/include/java.js"></script>
 
<style type="text/css">
@import url(/templates/default.css);
@import url(/templates/myaccount.css);
@import url(/templates/leftmenu.css);
</style>
</head>
 
<body>
 
<script language=JavaScript>
<!--
 
var message="";
///////////////////////////////////
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
 
document.oncontextmenu=new Function("return false")
// --> 
</script><div id="container" style="width:816px;">
        <div id="logo"><a href="/index.php"><img src="/images/top1.jpg" alt="Football Advisers - The UK's Preferred Football Betting Analysts" width="418" height="113" border="0" /></a></div>
<div id="top">
            
            <div id="myaccount">
            <div  class="top"><img src="/images/myaccount.jpg" alt="Login to your Account" width="130" height="20" border="0" /></div>
            <div class="left"><img src="/images/myaccount_left.jpg" width="3" height="42" alt="" /></div>
            <div class="content">
                [*myaccount*]
             </div>
            </div>
        </div>
        
        <div id="menu">[*menu*]</div>
        <div id="homepage">[*homepage*]</div>
        <div id="leftmenu">[*leftmenu*]</div>
        <div id="content">[*main*]</div>
        
</div>
 
the index.php is the main page which is

Code: Select all

<?php
ob_start();
   
   session_start();
   
   header("Cache-control: private");
   
   ob_flush();
session_start();
header("Cache-control: private"); 
 
/*
if($_SESSION['username']) {
    $url = explode(":",$_SERVER['SCRIPT_URI']);
    if($url[0] == 'http') {
        $redirect = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        header("Location: $redirect");
        exit;
    }
}*/
 
/* Never allow request via get and post contain snippets, javascript or php */
$sptags = array('@<script[^>]*?>.*?</script>@si',
                  '@&#(\d+);@e',
                  '@\[\[(.*?)\]\]@si',
                  '@\[!(.*?)!\]@si',
                  '@\[\~(.*?)\~\]@si',
                  '@\[\((.*?)\)\]@si',
                  '@{{(.*?)}}@si',
                  '@\[\*(.*?)\*\]@si');
foreach($_POST as $key => $value) {
  $_POST[$key] = preg_replace($sptags,"", $value);
}
foreach($_GET as $key => $value) {
  $_GET[$key] = preg_replace($sptags,"", $value);
}
 
 
 
/* include time generator (for faster performance testing) */
include('include/generate.time.php'); 
$generate->start();
 
 
/* include config file */
require_once('include/config.php');
require_once('include/functions.php');
 
$TimeZoneSettings=mysql_fetch_array(mysql_query("SELECT * FROM timezone"));
 
if($TimeZoneSettings['title']=="GMT")
{
    $time = date("g:ia \G\M\T- dS F, Y",time() + (1 * 60 * 60) + ($TimeZoneSettings['diff'] * 60 * 60));
    $time_timestamp = time() + (1 * 60 * 60) + ($TimeZoneSettings['diff'] * 60 * 60);
    define(est_time_date ,$time);
    define(est_time_date_stamp, $time_timestamp);
}
else
{
    $time = date("g:ia E\T- dS F, Y",time() + (1 * 60 * 60) + ($TimeZoneSettings['diff'] * 60 * 60));
    $time_timestamp = time() + (1 * 60 * 60) + ($TimeZoneSettings['diff'] * 60 * 60);
    define(est_time_date ,$time);
    define(est_time_date_stamp, $time_timestamp);
}
 
/*$time = date("g:ia C\S\T- dS F, Y",time() + (1 * 60 * 60));
$time_timestamp = time() + (2 * 60 * 60);
define(est_time_date ,$time);
define(est_time_date_stamp, $time_timestamp);*/
 
 
/* if trying to logout */
if(isset($_GET['logout'])) {
    
    unset ($_SESSION['username']);
    unset ($_SESSION['password']);
    unset ($_SESSION['userid']);
    
    header("Location: index.php");
    exit;   
}
 
/* if trying to login */
if(isset($_POST['usr'])) {
    $luser = $_POST['usr'];
    $lpass = md5($_POST['passw']);  
    
    // include class and start it
    include('classes/auth.class.php');
    
    $auth->login($luser,$lpass);
    print $auth->login_result;
    exit;
}
 
/* check if user is trying to login, set page etc. */
include('include/content.php');
 
 
/* start template engine and set the default 
template file located in "templates" folder */
$path_footer = $path_template . 'default_footer.php';
$path_menu = $path_template . 'menu.php';
$path_myaccount = $path_template . 'myaccount.php';
$path_leftmenu = $path_template . 'leftmenu.php';
$path_homepage = $path_template . 'homepage.php';
$path_template = $path_template . 'default.html';
 
require_once("include/engine.php");
$page = new loadTemplate($path_template);
 
 
/* set tages and replace them with content inside
the template, additional tags can be set by using: [*TAG*] 
will also need to set the tags below so they are replaced... */
$tags->title = require_once("header.php");
$tags->main = $page->parse($content);
 
$tags->menu = $page->parse($path_menu);
$tags->leftmenu = $page->parse($path_leftmenu);
$tags->myaccount = $page->parse($path_myaccount);
$tags->homepage = $page->parse($path_homepage);
 
/* load the above tags into the template engine */
$page->replace_tags($tags);
 
 
/* after setting the template and the tags to be replaced,
we are now going to generate the page... */
$page->output();
 
 
/* stop the time generator and show it at bottom of page */
$generate->stop();
include_once($path_footer);
 
 
 
?>  
please note the $tags->title = require_once("header.php"); was originally set as $tags->title = "some random title" which meant it ws delivered to every page.

I am using the code on each included page:

Code: Select all

<?php 
 
$title = "Football Advisers - Football Betting Tips"; 
$keywords = "betting tips, free football betting, software betting, uk betting tips, football betting system, football predictions, forum betting, premier league predictions, championship predictions, la liga predictions, bundesliga predictions, ligue 1 predictions, serie a predictions, champions league predictions, europa league predictions"; 
$description = "betting tips, free football betting, software betting, uk betting tips, football betting system, football predictions, forum betting"; 
require_once("header.php"); 
 
?>
and the header is this:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php if(isset($title)) { print $title; } else { print "Football Advisers Betting Advice"; } ?></title> 
<meta name="keywords" content="<?php if(isset($keywords)) { print $keywords; } else { print "Football, Betting, Tips, Picks, Advice"; } ?>" /> 
<meta name="description" content="<?php if(isset($description)) { print $description; } else { print "Football Betting Advisers/Sports Analysts with free bets, football betting tips, La Liga, Serie A, Bundesliga, Premiership, Championship, Ligue 1, UEFA Cup and Champions League"; } ?>" />
I've tried the header with and without the doctype and <head> variable.

The best I've got so far is to delete the [*title*] from the default.php and then the titles load into the browser fine, but in the source the tags are way down the page in the body where the included page is loaded (where the [*main*] is loaded in the default.php

My word, you can see why I have a headache!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Dynamic PHP headache!

Post by jackpf »

Why don't you have each individual page set out like a function, so instead of echoing anything, do something like this:

Code: Select all

$page = 'hello, this is a page';
//add whatever...
$page .= 'asfasfsf';
 
return array('title' => 'thispage', 'content' => $page);
And on your index page

Code: Select all

$page = include('page.php');
echo '<title>'.$page['title'].'</title></head><body>'.$page['content'];
Or whatever.

That's the only way I can think of doing what you want to do without having a switch statement to check for titles (which is what I do personally, because if the page exists, the title is always in the url, so it's really easy for me...but it may not be the case for you).
mlynch1985
Forum Newbie
Posts: 3
Joined: Fri Jul 10, 2009 4:36 pm

Re: Dynamic PHP headache!

Post by mlynch1985 »

cheers for the reply mate.

I have to say, I'm a little lost with your suggestion, I know enough about php to customize whats there and so on, its just creating from scratch I have problems with.

I find it wierd that every suggestion I have looked at on the net requires php to load the content (to grab the title data), then load the header after. this means it works but the header is below the content and not in the header of the page!!!

is this just a limit of php?

I've floated the idea of calling the title from an SQL table, however I'm wondering if I'll have the same issue re calling the content before the title still?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Dynamic PHP headache!

Post by jackpf »

Well, you only limit yourself to what you can echo out where if you echo it out all at the same time. If you control what is output like I showed above, then you're not limited by previous outputs.

You only limit yourself by your own programming. Wow...sounds cool.

Anyway, that's how I'd do it in your shoes. If you can't write the code then you can either look up a tutorial and learn how to....or just leave it as it is.
Post Reply