Low End Mac's Online Tech Journal

Displaying External Links with PHP

Website Automation with PHP and MySQL, Part 10

Dan Knight - 2002.06.26

In the last article, we looked at the ten fields necessary to make an "external links" database work the way we want it to. This article looks at the programming necessary to display that data properly.

The first thing is to punctuate our display with commas, spaces, and periods. There's a space after the flag; a comma and space after the link, the author, the column title, and the site name; and a period and space after the date.

In our earlier projects, we didn't worry about empty fields in a record, but this time we have to. Here's why: Our standard link entry looks like this:

  • can Rights: Canada bans Mac use on Internet, Joe Who, Tech Time, Ottawa Ragsheet, 06.25. After voting to move the nation's capitol to Redmond, WA, Parliament has banned use of Macs on the Internet inside Canada.

While every article has a category, a URL, the text that links that URL, a date, and a description, some don't display a flag, have a known author, have a column heading, or sometimes even really have a website. If we hadn't taken that into consideration, the above link could look like this with a few empty fields:

  •  Rights: Canada bans Mac use on Internet, , , Ottawa Ragsheet, 06.25. After voting to move the nation's capitol to Redmond, WA, Parliament has banned use of Macs on the Internet inside Canada.

That just doesn't look good. So for the first time, I had to wade into using the if command in PHP. Here's the code I created so the above would display properly:

<?php

$db_server = "server.com";
$db_username = "username";
$db_password = "password";
$db_name = "db_name";
$connection = @mysql_connect($db_server,$db_username,$db_password);
mysql_select_db($db_name,$connection);

  echo "<ul>";

$get_links = mysql_query("SELECT * FROM xlinks ORDER BY timestamp DESC LIMIT 24");
while ($array = mysql_fetch_array($get_links))
{
  echo "<li>";
  if ($array[flag]<>"")
    {echo"<img src=/art/$array[flag].gif width=17 height=13 align=middle> ";}
  echo "$array[category]: 
  <a href=\"$array[URL]\">$array[linktext]</a>, ";
  if ($array[author]<>"")
    {echo"$array[author], ";}
  if ($array[columntitle]<>"")
    {echo"$array[columntitle], ";}
  echo "$array[website], 
  $array[pubdate]. 
  $array[description]</li>";
  
}

  echo "</ul>";

?>

The first and last echo statements define the start and end of an unnumbered (bullet) list. That's simple HTML.

The line starting with $get_links tells the server to access the xlinks database and select the 24 records with the most recent timestamps. The next line sets up a loop that repeats up to 24 times to display our external links.

echo "<li>"; starts our bullet list entry - and we immediately run into a field that may be empty. Since most of our linked stories are not of interest to just one nation, we don't often use the flag, but when we do, it comes first.

This is where I learned by trial and error just how the if statement works. It's not hard, but as with all things PHP, you have to get it just right to make it work.

First we test whether the flag field is empty with if ($array[flag]<>""). If it is empty, PHP skips the rest of this command. If not, it displays the flag GIF from our /art directory followed by a space.

Next it links the text to the URL, displays it, and follows it with a comma and space. Then we have two more if statements, one for the author and one for the column title.

After this comes the name of the website, a comma, a space, and the date. Then comes the description of the article or a relevant quote from it.

We've decided that in the case of personal sites (such as mac.com), we'll skip the author field and put the author in the website field. That keeps the code from getting even messier.

And after much trial and error, it looks just beautiful - exactly like the links we used to craft by hand, but now done automatically by a computer. And isn't that why computers were created in the first place?

Join us on Facebook, follow us on Twitter or Google+, or subscribe to our RSS news feed

Today's Links

Recent Content

About LEM Support Usage Privacy Contact

Custom Search

Follow Low End Mac on Twitter
Join Low End Mac on Facebook

Favorite Sites

MacSurfer
Cult of Mac
Shrine of Apple
MacInTouch
MyAppleMenu
InfoMac
The Mac Observer
Accelerate Your Mac
RetroMacCast
The Vintage Mac Museum
Deal Brothers
DealMac
Mac2Sell
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ

Affiliates

Amazon.com
The iTunes Store
PC Connection Express
Macgo Blu-ray Player
Parallels Desktop for Mac
eBay

Low End Mac's Amazon.com store

Advertise

Open Link