Maintaining a Hidden Content Archive Within a PHP Page

Do you have content that cycles regularly, such as a news feed, events calendar, etc. If so, there's a good chance that the current entries would serve as useful templates for future updates. "But where do I store these content templates?" Hey, thanks for asking. If you normally delete old information and are unsure where/how to archive it, let's take a closer look at one of the methods described in last week's post (Three Simple Methods for Hiding Website Content Until It’s Ready)—hiding content with PHP comments.

The Example

Let's say our website has a section listing upcoming events which might interest visitors. A subset of those events is posted on an annual basis. Updating the section typically consists of removing old entries and adding new ones as they are discovered. So how do we add a content archive?

Maintaining a Content Archive

Instead of deleting entries, as we may do for one-time events, the content could be hidden within the page using PHP comments. To demonstrate, here is the code for the upcoming events section:

...
<h1>Upcoming Events</h1>
<ul>
      <li><a href="http://uxweek.com/">UX Week</a> (San Francisco, CA; Aug. 23-26, 2011)</li>
      <li><a href="http://www.zendcon.com/">Zend PHP Conference</a> (Santa Clara, CA; Oct. 17-20, 2011)</li>
      <li><a href="http://aneventapart.com/2011/dc/">An Event Apart</a> (Washington, DC; Oct. 24-26, 2011)</li>
      <li><a href="http://futureofwebdesign.com/new-york-2011/">Future of Web Design</a> (New York, NY; Nov. 7-9, 2011)</li>
</ul>
<?php
/* EVENT ARCHIVE GOES HERE
...
*/
?>
...

Note the event archive between the open and close PHP tags (<?php ?>). Looking through the events listed above, UX Week needs to be moved to the archives:

...
<h1>Upcoming Events</h1>
<ul>
      <li><a href="http://www.zendcon.com/">Zend PHP Conference</a> (Santa Clara, CA; Oct. 17-20, 2011)</li>
      <li><a href="http://aneventapart.com/2011/dc/">An Event Apart</a> (Washington, DC; Oct. 24-26, 2011)</li>
      <li><a href="http://futureofwebdesign.com/new-york-2011/">Future of Web Design</a> (New York, NY; Nov. 7-9, 2011)</li>
</ul>
<?php
/* EVENT ARCHIVE GOES HERE
      <li><a href="http://uxweek.com/">UX Week</a> (San Francisco, CA; Aug. 23-26, 2011)</li>
*/
?>
...

In case it wasn't obvious, the UX Week entry was moved between the PHP comments (/* */). Once the page is uploaded to the server, the event information will no longer be sent to the browser. As an added bonus, we still have access to the old event information which can assist in keeping the calendar up to date.

Leveraging the Archive

With the archive in place, the content can be useful when looking for events. The archive contains the:

  • Title of the events we normally post
  • Website address where we'll likely find future event information
  • Date information for the previous event which provides a clue to when the next one will be held; annual events tend to be held around the same time frame every year

As upcoming event information is found, we just need to pull the corresponding entry out of the archive, customize as necessary, and post it back to the visible list.

Caveats

Although the above technique may sound simple, there are things to watch out for. First, the web page needing the PHP comment archive must be able to run PHP. For example, the code may not function in a .html file. PHP files typically have a .php extension. You'll also want to make sure that PHP is installed on your server.

When moving old event information to the archives, you'll also need to be careful of entries that contain PHP comments. For example, the following would give some unexpected results:

...
<h1>Upcoming Events</h1>
<ul>
      <li><a href="http://aneventapart.com/2011/dc/">An Event Apart</a> (Washington, DC; Oct. 24-26, 2011)</li>
      <li><a href="http://futureofwebdesign.com/new-york-2011/">Future of Web Design</a> (New York, NY; Nov. 7-9, 2011)</li>
</ul>
<?php
/* EVENT ARCHIVE GOES HERE
      <li><a href="http://uxweek.com/">UX Week</a> (San Francisco, CA; Aug. 23-26, 2011)</li> /* PHP comments that will break the archive */
      <li><a href="http://www.zendcon.com/">Zend PHP Conference</a> (Santa Clara, CA; Oct. 17-20, 2011)</li>
*/
?>
...

If PHP comments don't work for whatever reason, you could look into other commenting options such as HTML comments.

0 Comments

There are currently no comments.

Leave a Comment


Warning: Undefined variable $user_ID in /home/cyberscorp/webdev.cyberscorpion.com/wp-content/themes/scorpbytes/comments.php on line 72