Page 1 of 1

MySQL queries & classes & client output limitations

Posted: Mon Feb 02, 2009 11:13 am
by JAB Creations
I've been flirting with a barrier where things such as the title element can't be determined until after they have been processed (currently).

This is leading me to think I may have to determine all or at least the critical MySQL queries I need to make (for my blog and forum in example) before the client output begins and save them to classes. I'm pretty sure that if I pulled it off correctly I'd really smooth out my site's general CMS setup though I could also end up making my site severely spaghetti coded. Since SEO is important (if you're not found what's the point of all the work?) it's one of the situations where using JavaScript to set the title of the page (as well as meta data) would be impossible. I am also curious if there are other approaches though in general I can't/won't change from standards compliant code so I'm open to everyone's thoughts as far as this is concerned. I want to make sure that I don't end up with something even remotely like ...*shivers*...WordPress's code. :| Please, show me the light! :)

Re: MySQL queries & classes & client output limitations

Posted: Mon Feb 02, 2009 11:29 am
by alex.barylski
I'm working on something almost identical in nature. But I have no idea what you are trying to do if that makes sense? :P

The general is to use output buffering and inject the title tag or whatever in at a later time then when it's processed (which is a vague term could mean anything). Alternatively you migt consider creating your layout/template and rendering it, and setting the title, meta, etc at the end of script execution using a DOM or simple str_replace() technique.

I'm not sure I understand your question fully but this is what it osunds like to me.

Cheers,
Alex

Re: MySQL queries & classes & client output limitations

Posted: Mon Feb 02, 2009 11:33 am
by Eran
You can implement the two-step view pattern and use placeholders for titles, meta tags etc.
http://martinfowler.com/eaaCatalog/twoStepView.html

In this pattern, you first render the view without the placeholders and then perform another pass to add the placeholder after the content is already known.

Re: MySQL queries & classes & client output limitations

Posted: Mon Feb 02, 2009 12:26 pm
by VladSun
For SEO purposes I've seen links like this:

http://mydomain.com/page/2-title_of_the_page_here

using mod_rewrite it may become:

http://mydomain.com?action=page&id=2

Re: MySQL queries & classes & client output limitations

Posted: Mon Feb 02, 2009 12:33 pm
by JAB Creations
PCSpectra, one of my critical points was not to rely on the DOM as this would break SEO. Yeah, I can be vague but pytrin has the gift of keen perception thankfully. :)

I think this is what is implied by the two step view pattern and it's also generally what I was thinking of implementing. Please correct me if I'm wrong of course. How close does this example represent the concept? The clientside output works perfectly with the database.

test.php

Code: Select all

<?php 
include("mysql.php");
include("test-class.php");
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><?php echo $cms->get('title');?></title>
<meta name="description" content="<?php echo $cms->get('meta_description');?>" />
<meta name="keywords" content="<?php echo $cms->get('meta_keywords');?>" />
<meta name="language" content="<?php echo $cms->get('meta_language');?>" />
<meta name="robots" content="<?php echo $cms->get('meta_robots');?>" />
<base href="http://localhost/Version 2.9.A.3/" />
<link href="themes/style.css.php" media="screen" rel="stylesheet" title="Classic Theme" type="text/css" />
</head>
 
<body>
 
<div><pre><?php print_r($row1);?></pre></div>
 
</body>
</html>
test-class.php

Code: Select all

<?php
class cms
{
 public function set($name,$value) {$this->$name = $value;}
 public function get($name){return $this->$name;}
}
 
$result1 = mysql_query("SELECT * FROM cms WHERE id='1'");
$row1 = mysql_fetch_assoc($result1);
 
$cms = new cms();
$cms->set('meta_description',$row1['meta_description']);
$cms->set('meta_language',$row1['meta_language']);
$cms->set('meta_keywords',$row1['meta_keywords']);
$cms->set('meta_robots',$row1['meta_robots']);
$cms->set('title',$row1['title']);
//echo $cms->get('title');
?>

Re: MySQL queries & classes & client output limitations

Posted: Mon Feb 02, 2009 12:39 pm
by JAB Creations
VladSun, you're a wee bit ahead of what I'm working on but yeah WordPress does that too and it's on my to do list. :)

Right now I'm just trying to make sure my CMS evolves opposable thumbs, vocal cords, stands up right, and has a nice big prefrontal-cortex. :mrgreen:

Re: MySQL queries & classes & client output limitations

Posted: Mon Feb 02, 2009 12:55 pm
by Eran
You did use placeholders but you rendered the entire view in one step. The point of placeholders in the two-step view is that they can be set at any point during the rendering process, especially after they are declared in the layout.

So suppose this is your layout:

Code: Select all

//layout.phtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><?php echo $this -> headTitle();?></title>
<meta name="description" content="<?php echo $this -> headMeta('description');?>" />
<meta name="keywords" content="<?php echo $this -> headMeta('keywords');?>" />
<meta name="language" content="<?php echo $this -> headMeta('language');?>" />
<meta name="robots" content="<?php echo $this -> headMeta('robots');?>" />
<base href="http://localhost/Version 2.9.A.3/" />
<link href="themes/style.css.php" media="screen" rel="stylesheet" title="Classic Theme" type="text/css" />
</head>
<body>
<?php echo $this -> render('header.phtml'); ?>
 
<div id="container"><?php echo $this -> content; ?></div>
 
<?php echo $this -> render('footer.phtml'); ?>
</body>
</html>
 
Notice that the meta tags and title have not been set yet. Suppose the content being rendered is a content form stored in content.phtml:

Code: Select all

//content.phtml
<?php
$this -> headTitle('Contact Us'); //We declare the title here
$this -> headMeta('keywords','contact form,contact us');
$this -> headMeta('description','official contact form bla bla');
?>
<form>
Email: <input name="email" />
</form>
 
This will be injected as the content member of the previous layout script. The placeholder's values can be set after they have been declared in the layout, and the actual values will be outputted in the second pass over the view. Some values can be set as defaults beforehand, and still be dynamically altered during rendering.

This might be too much for your purposes, and just using placeholders in itself might be sufficient.

Re: MySQL queries & classes & client output limitations

Posted: Mon Feb 02, 2009 1:49 pm
by JAB Creations
Thanks for your replies. I don't see what you did differently other then the includes though?

My latest revision is sort of like this...

Code: Select all

<?php
include("header.php");
include("this_page_functions.php");//strictly functions, no output by include alone, function must be called
include("template_1.php");
/*
page specific arguments here that call page specific functions on line 3.
*/
include("template_2.php");
?>
With this all I would do is have the header file include the CMS class file. Inside of the PHP sub-templates (the main ones are simply include files for organizational purposes) apply the classes as we are now only they are nested a little deeper.

I was trying to make a minimal example, with this now in play in addition to my first example am I on the correct track?