Topics
Dates
200902 · 05 · 06
2008
01 · 02 · 03 · 04 · 07 · 08 · 09 · 11
2007
06 · 07 · 08 · 09 · 10 · 11 · 12
Also check out FishoftheBay.com for more thoughts about design and life.
Using PHP When Possible
PHP January 14, 2008
In recent months, I've gotten in the habit of writing things in PHP arrays, using a function with a loop to actually generate HTML. This works great for anything where you have repeating elements (such as a table, lists, navigation, images, etc.). The benefit is that it's much clearer when you need to change the text - rather than looking through HTML code and brackets, you can just change the text:
$arr = array(
"Home" => "home.php",
"About" => "about.php",
"Contact" => "contact.php"
);
echo '<ul>';
foreach($arr as $k=>$v) {
echo '<li><a href="'.$v.'">'.$k.'</a></li>';
}
echo '</ul>';
Instead of hand-writing each navigation button in their HTML form, here we separate them into two parts: the text-only array and then the HTML code which doesn't ever have to be touched. Thus, it's much easier and faster to edit (especially for those who don't know HTML and are afraid to touch code!).
The same thing can be said for tables - perhaps each item in the array is a row which maps to its own array of table cells with data. Whatever the case, it's much easier to separate the text from the HTML and it saves time later.
I've had to do numerous websites for clients who then go on to edit the code themselves for updates (this is, of course, if they opt not to include a content-management system). So using this approach makes it easier for them to edit their pages without worrying they'll mess up the code. User Experience behind the scenes!
Just a useful note in case you didn't know :^P
Other PHP Posts
Always Coding Generically
I've been programming websites for a while now and though I often do things fast for prototyping purposes, I find myself wishing I coded in a more generic manner. Sure, it's easy to go ahead and write the code as you ...
Processing Forms: Javascript or PHP?
I've dealt with a lot of online forms. Most, on submit, will refresh the page and process the data before the page loads. Then it will tell you of any errors you have. Sometimes, a page reload can take a while. If yo...
PHP Arrays
PHP hashmap-style arrays make a great solution to wanting to pass multiple variables but not wanting to specify them all in the arguments to a function. As far as object-oriented stuff goes, you can specify an array ...