Archive

Archive for the ‘100 Weeks of PHP’ Category

So you want PHP Templates, eh?

September 2nd, 2009 No comments

PHP Templates! Everyone wants them, some people try to provide them to others, but deep down inside, you all know smarty isn’t the way to go… Allow me to show you a better way.

Just use PHP! Yes, that’s right! PHP is almost designed from the ground up natively support templates!

Here’s the secret… When you include a php file (using the include(“somefile.php”) method, the php code inside the included file has the same variable scope as the code block that included it… So… Take the following two files….

(index.php)
<?php
  go();
  function go(){
    $a = "Hi There!";
    include('my_view.php');
  }
(my_view.php)
<html>
<body>
<?php echo $a; ?>
</body>
</html>

The output would be:

Hi There!

If you’re a noob, another name for this (if you make extra effort to not put anything but loops, and echos in your template) is MVC (Model View Controller). While it isn’t a complete implementation, it’s a really really good start and makes writing and maintaining a site WAY easier. Cake PHP uses this approach for it’s views (aka templates) as well. Java uses this, but instead of the “view” using the local variables where a page is displayed, the data you want the view (or jsp page in java) to have access to are put in the request object as an “attribute”.

I, personally, put the variables I want to use in a “template” (aka “view”) in a global array ($GLOBALS[‘params’][‘name’] = “bob”;) It isn’t the best practice, because some part of your code far away could write to this and mess something up, but for simple sites, I find it quick and easy.

Of course, if you REALLY want a templating system, you could use SMARTY. (Blech!) But, I really recommend you just get used the to the echo statements and use php to render templates.

So, some sample code from a site of mine is:

<?php
require_once(BASE_FS_PATH . '/classes/BookService.php');
 
switch($_REQUEST['action']){
	case 'Research':
		$book = BookService::importBook($_POST['ean']);
		$_GLOBALS['params']['book'] = $book;
		include(BASE_FS_PATH . '/views/pages/research.php');	
		break;
	case 'mark_not_sellable':
		$book = BookService::importBook($_GET['ean']);
		$book->sellable = 'no';
		$book->save();
		$_GLOBALS['params']['msg'] = 'Book Marked As Not Sellable.';
		show_details($book);
		break;
	case 'mark_not_available':
		$book = BookService::importBook($_GET['ean']);
		$book->available = 'no';
		$book->save();
		$_GLOBALS['params']['msg'] = 'Book Marked As Not Available.';
		show_details($book);
		break;
	case 'details':
		$book = BookService::importBook($_GET['ean']);
		show_details($book);
 
		break;
	default:
		include(BASE_FS_PATH . '/views/pages/research.php');
		break;
}
 
function show_details($book){
	$_GLOBALS['params']['book'] = $book;
	include(BASE_FS_PATH . '/views/pages/details.php');
}

And the “view” for this code (details.php) is:

<body style="width:950px; margin: 0 auto; text-align: left;" class=" yui-skin-sam">
		<?php 
			include(BASE_FS_PATH . '/views/pieces/menu_and_js.php');
		?>
 
		<div style="text-align: center;">
			<?php echo $_GLOBALS['params']['msg']; ?>
		</div>
 
		<table>
		 <tr>
		  <td>
			<h1><?php echo $_GLOBALS['params']['book']->title; ?></h2>
			<ul>
			<li>Ean: <?php echo $_GLOBALS['params']['book']->ean; ?></li>
			<li>Available: <?php echo $_GLOBALS['params']['book']->available; ?></li>
			<li>Imported: <?php echo $_GLOBALS['params']['book']->import_date; ?></li>
			<li>Cost: <?php echo $_GLOBALS['params']['book']->cost; ?></li>
			<li>Change In Inventory: <?php echo $_GLOBALS['params']['book']->change_in_inventory; ?></li>
			</ul>
			<h2>Urls</h2>
                         (More Code That's been removed for this example)

Anyways, there are LOTs of templating engines out there you can use instead, but I would discourage it. This is the fastest, simplest, most powerful way to do templates (IMO).

Categories: 100 Weeks of PHP, hacks, php5 Tags:

Setting up a PHP Development Server on Windows

August 19th, 2009 No comments

I know of four different ways to do this. I’m going to cover them in the order of my personal preference (and ease).

Install WAMP

Wamp is probably the easiest quickest way to get an apache server running PHP on your computer.

Just download the install here: Wamp Download run it, use the tool to make sure you don’t have anything running on port 80, and start the two services (apache, and mysql) by right clicking the icon in the task bar and clicking “Run All Services”

Download and Run Uniform

Uniform is different from WAMP and XAMP in that there is nothing to install, you just download it, and run it. This provides you with the ability to create a seperate “server” for each project you do, with it’s own mysql and apache binaries, configs, etc… I, personaly, perfer WAMP and writing all my apps to be location agnostic, so that they run anywhere and co-exist peacefully, but you may prefer this.

Install XAMPP

XAMPP is like WAMP. You download it, install it, and click go, but the only time I tried it (a few years ago) I ran into some difficulties. At the time they didn’t have a control panel like WAMP. This may in fact be the superior way to go, but I like my WAMP.

Setup Apache, MySQL, and PHP Binaries.

I did this once. It took me a few days (this was like 3 years ago, when my experience with PHP was ‘nill) If your a masochist this is the way to go. You’ll learn all the nitty gritties (or atleast be impeded by them). Hopefully you can beat my time.

Categories: 100 Weeks of PHP, php5 Tags:

Tell us what you want to know!

July 30th, 2009 2 comments

We love learning. There’s nothing we like more then learning, except sharing what we’ve learned. Without teaching, there isn’t much point to learning. So, tell us what you want to know. Tell us what obscure technology, or command, or syntax you want to know more about, and We’ll write an article about it! Just leave a comment below with what you want to know.

Categories: 100 Weeks of PHP Tags:

Use Other People’s wheels when possible…

July 30th, 2009 No comments

Week #1

    ALWAYS look for a wheel before you invent it.

I work at a software company called “Infusionsoft”. We write this kickbutt marketing software. One of our Software Engineer III’s (that’s the highest). Criticizes anyone anytime they invent something that already exists in our code. He is like a walking Google of existing code.

This is a rule that applies across all programming languages and other aspects of life. If someone else has done it, see if you can either use what they did, or learn from it. Don’t feel bad if you re-invent a few wheels along the way. Sometimes it is easier to just write your own rather then spend hours pouring through searches for someone elses.