Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, 13 May 2008

Helpers are the way forward

Read and weep at the sheer awesomeness that is helpers in the Zend framework. Helpers are going to be how
  • individual stylesheets are applied to a page
  • Client-side script can be added
  • Metadata such a description and keywords

Saturday, 3 May 2008

Zend and XSLT as a template engine

If you have XML data that you need displayed, turn off automatic view rendering within you bootstrap file with


$controller->setParam('noViewRenderer', true);

This gives you more control over how Zend renders views. Then, in your controller action , create a new view and assign some variable from you application logic. You need to tell the view where the script is to execute. You do this using $view->setScriptPath('...');. Once this points to the right directory, you can add a script in there you want the view to run. You do this by calling the render method of the view instance you create, passing it a single string parameter as the script you want it to run. As the contents of the script is returned, the whole thing needs to either be echoed or printed from the controller action.

Within the view script, print anything you want to be rendered on the page. So here you can pair up the xml data passed from the controller action using $this and an XSLT stylesheets you want to transform.

Brilliant hey? I sort of get this stuff.

Thursday, 1 May 2008

Differences between echo and print

It's all in the single, or double quoting. echo "$variable" will get echo to replace the variable name with it's value. If echo is used with single quotes it won't work.

Also, echo is slightly quicker than print apparently.


Saturday, 22 March 2008

Printing Arrays in PHP

always use print_r(); to debug an array. I guess echo and print just produce array.

When displaying stuff in web pages use a foreach loop.

Thursday, 20 March 2008

MySQL wrapper class

I started writing a wrapper, then I found this PHP MySQL wrapper class prebuilt. It seems very good and the site includes lots of cool example of how to use it. Why bother trying to re-invent the wheel? Well done guys.

SQLite

SQLite seems to be an alternative to MySQL. Sure it has many benefits, by i'm sticking to MySQL for the time being.

Apparently it's fully OOP though so you can do cooler things with exception classes, and it comes with PHP 5.

Smarty: A PHP template Engine

Ok Smarty. All you really need is everything in the libs/ directory. When copying in unix, remember to add option -r to go into the internals/ and plugins/ directories within libs/. With those files saved somewhere, your PHP has to find one file in particular-Smarty.class.php (they also seem to be quite strict about the capital "S" in "Smarty"). That file needs to be referenced with an absolute path. This can be done a number of ways, look them up at Smarty's documentation. One cool way I've found so far is this:

// Use the absolute path for Smarty.class.php
$base_path= basename(dirname(__FILE__));
require($base_path.'/Smarty/Smarty.class.php');

Of course this assumes that you're writing this in a file which is in the document root of your webserver, and that the Smarty files are located further down in the folder Smarty/. The require_once is similar to PHP's include statement apart from it will result in an error if it can't find the file (include will just try and not care if it can't) and the _once part ensures there's no errors if you accidentally try to require it again.

Within that Smarty directory, make four folders: templates, templates_c, cache and configs. Link these files up with your code with

$smarty = new Smarty();
$smarty->template_dir = $base_path.'Smarty/templates';
$smarty->compile_dir = $base_path.'Smarty/templates_c';
$smarty->cache_dir = $base_path.'Smarty/cache';
$smarty->config_dir = $base_path.'Smarty/configs';

Put this immediately after the required_once statement. Then start making templates and go.

Wednesday, 19 March 2008

PHP: the ? operator

The ? in PHP is shorthand for an if..else statement. It works like this
{expression} ? return_when_expression_true : return_when_expression_false;
Notice the ? and the : to separate everything apart.

Casting in PHP

Casting seems relatively straight-forward:
$test = 1234;
$test_string = (string)$test;

Simple.

Tuesday, 18 March 2008

.inc files and libraries

Use require_once() to include files to use as a library. These files get interpreted by the interpreter but aren't called directly. They are conventionally given a .inc file extension, and they allow you to call functions and stuff hidden inside them. Quite useful.