Archive

Archive for September, 2009

How do I create a website icon?

September 10th, 2009 No comments

It’s a piece of cake…

Just create a file called favicon.ico (an icon file) and put it in the root of your domain. Optionaly, you can include a meta tag to do it.

More information is here: http://en.wikipedia.org/wiki/Favicon

Also, here is an online generator: http://www.favicon.cc/

There is one gotcha… On your browser, the icon is almost NEVER updated, unless you load just the icon, i.e. http://yoursite.com/favicon.ico then it will refresh the cache, and the new icon will always appear.

Categories: web Tags:

JavaScript Primer

September 10th, 2009 No comments

So you want to learn JavaScript huh? Way to go! SOA powered by JavaScript is the future of the web. (Trust me).

What is JavaScript? It’s a scripting language that all modern web browsers run that allows you to manipulate web pages and do sweet stuff.

To get started, first, you need an easy way to run JavaScript. While you could write javascript in an html page and run it, it’s generally easier to be able to run scirpts without making an html page. To do this, install Firebug, and use it’s console. (Just install firebug, click on the little cock-roach icon in the lower left of Firefox, and click the console tab, now reload your window.).

Once you have the firebug console working, type the following into the console, and you should get a popup.

alert('Hello World!');

Here’s a video that shows how to install and use Firebug:

If you want to learn more, head over to w3schools’ JavaScript tutorial.

Categories: html, javascript Tags:

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: