BLOODHOUNDBLOG.COM

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: , , ,

Related posts:
  • Speaking in tongues: Presentable PHP in WordPress
  • Project Bloodhound: How to make Google your weblog’s best friend
  • How to make fast, flexible web pages . . .

  • 16 comments

    16 Comments so far

    1. Chicago Real Estate December 29th, 2007 3:15 am

      This is a great way to make content modular, meaning you only have one place to go to for edits and changes, and it is a very standard practice amongst all programmers.

      For all you TypePad users out there, this is very similar to TypeLists, which are available whether you have a simple template blog or have converted your design to “Advanced Templates” for ultimate control of your look and feel.

      BTW, I think the relative path would work just fine, but I’d imagine delivering your feed through something like Feed Burner might cause issues. Not sure though.

    2. CJ, Broker in L A, CA December 29th, 2007 4:43 am

      Chicago Real Estate: I am an “advanced template” TypePad user, and have found that while you can place Typelists in any TP page template or sidebars, you cannot place TypeLists in the ~body~ of a blog post.

      In fact, I asked TypePad Support about this very same question back in April, here’s the answer I got:

      “On Apr 14, 2007 12:03:19 PM, TypePad Customer Support said: Hi Cheryl,

      Thanks for the note. There is not a way to include TypeList or other TypePad code to a weblog post. We are always looking for ways to enhance TypePad for our users, so we will keep this in mind as a possible future feature for the system.”

      But that said, since the type of list Greg is describing is a simple HTML file with an unordered list, TypePad users could easily enough use the HTML iframe tag to insert their “list file” at the end of any post. There, I just did it on Nelalive, with one of Brian Brady’s Mortgage Reports http://www.nelalive.net/2007/12/los-angeles-m-2.html

      I do think some readers may feel overwhelmed by the coding and miss the fact that it appears Greg is actually doing a fair amount of hands-on-human-work to accomplish this. He’s reading each contributer’s post, deciding how to categorize it, manually adding a link to each post in his “list file”, and manually adding the script code to the end of each contributor’s post. Am I right, Greg? Wow!

    3. Chicago Real Estate December 29th, 2007 9:18 am

      Yes, I see.

      Did you try an include, too, like Greg’s?

    4. Chicago Real Estate December 29th, 2007 12:36 pm

      Just looked it up in TypePad. You can certainly do a #include there also.

    5. CJ, Broker in L A, CA December 29th, 2007 4:30 pm

      Yo Chicago: I have tried several variations of <!–#include file=”mycleverfile.html” –>
      but TypePad keeps stripping them out of the post. I give in. Uncle. My iframe idea is working fine, so since it ain’t broke, I ain’t gonna fix it. :-)

    6. John Wake December 31st, 2007 12:58 am

      Greg, I just used my first PHP include last night and it opens up a whole new world.

      I can use Perl to generate dynamic content locally and then uploaded it to the server. The sales table is an include (and ultimately much of the rest of the page will be made up of includes as well).
      http://www.arizonarealestatenotebook.com/85258-scottsdale-homes-for-sale-trends/

      I plan to update it every week which should keep the pages minty fresh for the search engines.

      I’ve used includes with HTML pages but never put it together that the same technique could be used for WordPress blogs.

      New Subject. This technical evolution of blogs is one reasons why I think new RE bloggers will confront a significant (knowledge) barrier to entry. Some of the old bloggers have tricked out their blogs over years of experimentation which will be tough for new real estate bloggers to compete with. Of course, people with great writing skills will always be able to break in and succeed. Many Realtors are great talkers, god love ‘em, but few are great writers.

    7. Greg Swann December 31st, 2007 7:32 am

      Ah, a fresh victim! I’ve been debating with myself whether to keep writing these posts, few people seem to be interested. I’ll do at least one more, talking about how to programmatically build site- and appearance-independent web pages.

      John added this in another post:

      BTW, I don’t like removing the original code so I just comment it out. PHP comments are // to comment out to the end of the line, or /* and */ to comment out multiple lines.

      //A comment on a single line

      /*
      A comment
      on multiple
      lines
      */

      Yes. To comment out HTML, you do it like this:

      <!--
      <p>I'm just a memory
      of the text that used to be.</p>
      -->
    8. John Wake December 31st, 2007 4:07 pm

      Whoops!

      You’re right. You need HTML comments not PHP comments in header.php.

      Thanks for the heads up!

    9. CJ, Broker in NELA, CA January 2nd, 2008 1:43 pm

      Greg, I hope you decide to keep writing … I did a WP.org install on one of my hosts over the New Year holiday, just so I could play along.

    10. [...] Speaking in tongues: Dynamically updated lists of links in PHP [...]

    11. [...] Speaking in tongues: Dynamically updated lists of links in PHP [...]

    12. [...] Speaking in tongues: Dynamically updated lists of links in PHP [...]

    13. [...] Speaking in tongues: Dynamically updated lists of links in PHP [...]

    14. [...] Speaking in tongues: Dynamically updated lists of links in PHP [...]

    15. [...] Speaking in tongues: Dynamically updated lists of links in PHP [...]

    16. [...] Speaking in tongues: Dynamically updated lists of links in PHP [...]