Solved! Title+Content to 1.php from 2.php in two instances?

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
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Solved! Title+Content to 1.php from 2.php in two instances?

Post by JAB Creations »

I am interested in creating a dynamic file handler to serve multiple pages on my site. However the issue is that I wish to pull a title and the content separately from 2.php. So the client sees 1.php?page=thisorthat. I'm not sure how to best approach the scenario. I can work with PHP classes if that helps any. Here is what I'd like to do...

1.php - Needs the HTML title initially but should not include other HTML content from 2.php until later in the body.
2.php - Has the title and the content though needs to serve both to 1.php separately.
Last edited by JAB Creations on Mon Mar 10, 2008 2:21 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Title+Content to 1.php from 2.php but in two instances?

Post by Chris Corbyn »

You need a "view" object. You set properties into the view (i.e. set the title among other things), and the render it as late as possible. Probably time to start reading up on MVC.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Title+Content to 1.php from 2.php but in two instances?

Post by JAB Creations »

Hey Chris thanks though I need a little help: what should I read up more specifically on php.net? I have the feeling in my mind this is what I want though I try to use as minimal code as possible. Thanks!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Title+Content to 1.php from 2.php but in two instances?

Post by Chris Corbyn »

Nothing specifically on php.net, this is more of a general (Web MVC) pattern.

Effectively you want a place to hold some values, and a way to tell that thing to display a template at the end.

Here's a very basic view (the best bare bones of most view containers):

Code: Select all

<?php
 
class SimpleView {
  private $_vars = array();
  
  public function setVar($name, $value) {
    $this->_vars[$name] = $value;
  }
 
  public function render($_template) {
    export($this->_vars);
    include($_template);
  }
}
So somewhere at the start of you code you create a "new SimpleView()".

Code: Select all

$view = new SimpleView();
Then you could set some variables into it if you like:

Code: Select all

$view->setVar('author', 'Me!');
And you pass it to anything else which needs to modify variables in it.

Then when you want to output your page:

Code: Select all

$view->render('some-template.tpl.php');
In this particular case there would be a $author variable exposed inside the template file.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Title+Content to 1.php from 2.php but in two instances?

Post by JAB Creations »

Check it out, I was able to get it to work! :mrgreen:
file1.php

Code: Select all

<?php
include("templates-1.php");
$request = array_keys($_GET);
foreach ($request as $game2) {echo $game2;}
$game1 = 'pc_';
$game3 = '.php';
$game = $game1.$game2.$game3;
include($game);
 
echo '<title>'.$title.' Cheat Codes</title>
<meta name="Description" content="'.$title.' cheat codes for PC." />
<meta name="keywords" content="'.$title.', cheat, codes, pc" />
<meta name="language" content="en" />' . "\n";
include("templates-2.php");
 
//get command from list
echo $page;
include("templates-3.php");
file2.php

Code: Select all

<?php
$title = 'A game name with ImpORTanT casing';
$page = <<<EOD
<div>Lots of HTML stuff that could be a hundred lines long goes here, yay!</div>
EOD;
?>
Post Reply