Explosive PHP Application
Published: February 10th, 2009 by: Kurtis
Sometimes a complex PHP-built website can become tedious when dealing with minor changes to the template and HTML markup. Changing, for instance, a set of links (navigation) or other integral parts of a website, can become quite the lengthy process. In this article, I'll share a technique that I sometimes use to make something as simple as a website navigation a little more automated using an explosive function.
<?php ... $arr = explode(delimiter, string [,limit]); ... ?>
Now you may be thinking: that’s all well in good but what in the world is a delimiter? I thought the same way before I truly understood the function. The delimiter is just the common separator between each element. For instance, if we wanted to explode a sentence into an array with each element as a separate word from the string, the delimiter would be an empty space (” “). The optional “limit” is a limit of the number of elements to allow to be created. If it is left empty or set to “-1,” no limit will be set. Otherwise, once the limit is reached, the last element will be the rest of the unexploded string.
Now this is all good and well unless you have an awesome application of the function, and believe me, there are many, but my favorite is the dynamic navigation idea. I always get sick of having to add in all the extra template elements when altering a website’s navigation, and with this simple idea that can cease.
Here’s a basic run-down of what will happen: first, we’ll define a string holding all the link names and source locations. Then, we’ll explode that string to an array of all link name-source pairs which we will in turn explode individually to produce a great link. Here’s an example:
<?php ... $links = "Home:index.php,About Us:about.php,Contact Us:contact.php,Portfolio:portfolio.php"; $links = explode(",",$links); echo "<ul>"; foreach ($links as $link) { $link = explode(":",$link); echo "<li><a href='".$link[1]."'>".$link[0]."</a></li>"; } echo "</ul>"; ?>
Be aware of the order in which we are exploding the elements. If you explode incorrectly, you could end up with inaccurate pairs. With this application, to update the navigation, instead of sifting through what could become complex HTML, you simply need to edit the “links” variable and let the PHP take care of business for you. Nothing complex, but certainly useful.
Matt (aka teammatt3)
Feb 15th, 2009
8:18 pm
“With this application, to update the navigation, instead of sifting through what could become complex HTML, you simply need to edit the “links” variable”
It appears to me that sifting through that cryptic variable would always be harder than editing HTML (even if the HTML was complex). It is also difficult to debug if you make a mistake in the syntax.
Aren’t there better ways to solve this problem?
Include a page that has the basic HTML of the links wrapped in list item tags. (You could make it a function that prints HTML if you don’t want to access the file system).
Why not use a regular array instead of a cryptic string?
array(“Home” => “index.php”,
“About Us” => “aboutus.php”);
Kurtis
Feb 15th, 2009
9:06 pm
Sure, there are certainly alternatives, but in my opinion, none is more “cryptic” than the other. Your array solution and my variable solution are on the same level with what I was trying to achieve: making an easy-to-update solution. With my method, simply add a “,Text:href” pair to the end of the variable. With yours, simply add a “,’Text’ => ‘href'” pair to the end of the array. The simplicity is maintained across the board.
Andrew
Feb 16th, 2009
10:29 am
I think they both have their pluses and minuses. If I used the “explosive method”, I would probably allow line breaks after the comma. This could be achieved by putting array_map(‘trim’, $links); after $links = explode(“,”,$links); to take out any breaks from the elements, then you can do something like:
It would certainly make it more readable if there were a couple dozen links.