There’s always something to howl about.

Speaking in tongues: Dynamically updated lists of links in PHP

Let’s get dynamic, shall we? There are a lot of things you can say about PHP — and some of them are even safe for work. But, at bottom, PHP is a working stiff’s programming language for producing dynamic web pages.

What’s a dynamic web page? It’s a page that reflects a user’s actions or editing without requiring manual editing of each little line of HTML. In the world of AJAX coding, web pages are becoming so dynamic that they seem to mimic the behavior of stand-alone complied applications. Calm down. We’re not going to do anything that sexy. And while we will actually be introducing real bona fide PHP programming in this post, you can dial down the palpitations: We will be doing precisely one line of PHP programming. Everything else will be familiar old HTML.

Okay then: Go take a look at this article on HR 3915. Brian Brady wrote that post, and it was one of our most popular entries this year. BloodhoundBlog is the home of big stories on the RE.net, and, when we cover a big story, I do everything I can to make sure that readers — especially readers arriving from search engines or by hard clicks from non-RE.net sites — don’t miss the full extent of our coverage.

So, if you scroll down in that post, you’ll see a bullet list of links to other posts with the heading “More on HR 3915.”

That list of links is done with PHP.

The actual list is simply a file of plain vanilla HTML — no PHP programming. The file for this list of links is called HR3915.php. It looks like this:

&nbsp;<br>&nbsp;<br><B><I>More on HR
3915:</I></B><UL>
<li><a
href="http://www.mortgageratesreport.com/2007/11/
15/daily-kos-on-hr-3915-mortgage-industry-set-to"
target="_blank">Daily Kos on HR 3915: Mortgage
Industry Set To</a>
<li><a
href="http://www.bloodhoundrealty.com/
BloodhoundBlog/?p=2217" target="_blank">HR 3915:
Why Federally-Chartered Banks Get The Pass</a>
<li><a
href="http://www.bloodhoundrealty.com/
BloodhoundBlog/?p=2200" target="_blank">HR 3915:
Open Letter to Senator Dodd from a Veteran
Mortgage Originator</a>
<li><a
href="http://www.mortgageratesreport.com/2007/11/
08/hr-3915-legislating-to-armageddon"
target="_blank">HR 3915- Legislating to
Armageddon</a>
<li><a
href="http://blownmortgage.com/2007/11/07/hr-3915-
mortgage-reform-bill-passes-committee-with-
important-changes/" target="_blank">H.R. 3915
Mortgage Reform Bill Passes Committee with
Important Changes</a>
<li><a
href="http://www.bloodhoundrealty.com/
BloodhoundBlog/?p=2176" target="_blank">HR 3915:
Anti-Consumer Bank Protection Act of 2007</a>
<li><a
href="http://www.bloodhoundrealty.com/
BloodhoundBlog/?p=2153" target="_blank">HR 3915:
Exploring the Minds of the Enablers</a>
<li><a
href="http://www.bloodhoundrealty.com/
BloodhoundBlog/?p=2112" target="_blank">HR 3915 Is
Dangerous</a>
<li><a
href="http://www.bloodhoundrealty.com/
BloodhoundBlog/?p=2103" target="_blank">HR 3915:
Mortgage Reform and Anti-Predatory Lending Act of
2007</a>
</UL>

(It’s cleaner than that in real life. I had to introduce line breaks to keep it from screwing up this page.)

Okay, so that much is easy. You write lists like this all the time. But what’s the point of putting the list out in its own file on the server?

By doing it this way, I can make a dynamically updating list of related links. Every time I want to add another link, I just edit the file, adding another entry to the list, and FTP it up to the server. Each of the relevant posts on BloodhoundBlog “includes” that list (we’ll talk about how in a moment). So every time I edit the file, each one of the affected posts reflects those edits without my having to do anything to them. The Project Blogger and the Divorced Commissions lists of links are huge, but I can dynamically update every affected post just by editing the respective link files.

Okayfine. How does it work?

First, you’ll need to download and install the RunPHP WordPress plug-in. There are other PHP plug-ins out there, and none are perfect, but RunPHP is the best for the kind of work we’re doing. Note that you only need a PHP plug-in to run PHP within “The Loop,” the section of PHP code in your WordPress installation that interrogates the MySQL database for each successive message to be displayed. In the header, the footer and the sidebar, you can run PHP without a plug-in. You can even run it within the loop from your WordPress PHP files (BloodhoundBlog does a ton of this). What you cannot do, without a PHP plug-in, is put PHP programming code within the body of a post (or the content of a WordPress “Page”). Without the plug-in, WordPress will display what it can of your PHP code as text and throw away the rest.

Once you have the RunPHP plug-in installed, hit the “run PHP code” check-box in your post. Then all you need to do to add a list of links to your weblog entry is add this one line of code to the body of the post:

< ?PHP
include("http://www.bloodhoundrealty.com/BloodhoundBlog/HR3915.php");
?>

In that particular case, you would be adding the list of HR 3195 links — and this will work if you’ve done everything right.

But, presumably, you would want to make your own link of lists — or your own HTML code of any sort. Your PHP code might look like this:

< ?PHP include("http://www.MyServer.com/MyLinks.php"); ?>

The angle brackets are a given in HTML. “?PHP” and “?” denote the start and end of PHP code to the parser. The “include” command simply tells PHP to include that file at run time. This really is programming, so spelling and punctuation definitely count. Remember this: Real programmers copy and paste, typing only the changes.

When it encounters that “include” command, PHP will open the file and blow the contents into the text stream just as if you had pasted the HTML into each post one at a time. This is the fundamental purpose of PHP: To programmatically produce or echo HTML on the fly. I’m using an absolute path to the file — in other words, the full server address — for maximum compatibility with RSS and other kinds of syndication. A local path (no server name) would probably work, but I’m a belt and suspenders kind of guy. My attitude is, “Why take a chance?”

Anyway, that’s it. One plug-in, one line of PHP programming and a dynamically updatable file of content you want to spread across any number of posts. We do this once a month, at least, sometimes a lot more often.

This is simple stuff, harder to explain than to execute. So do execute it. Play with it. See what you can make happen. In coming days we’ll talk about other cool ways of using “include” to build easy-to-use, fault-free web pages.

 
Getting in touch with your inner geek:

Want more? Real Estate Weblogging 101 will speak to your inner geek. And if you want even more than that, be sure to join us for BloodhoundBlog Unchained.

Technorati Tags: , , ,