Low End Mac's Online Tech Journal

Displaying External Links with PHP

Website Automation with PHP and MySQL, Part 10

Low End Mac Reader Specials

Memory To Go Special: MacPro 8 Core Memory 4GB kit $192 / 2GB kit $109. MacBook Pro / MacMini / iMac Intel Core2 DUO 2GB $44 1GB $23--Free shipping available.

Download Typestyler, still the Ultimate Styling Tool for Internet, Print and Video Graphics. Works great in Classic with a Native OS X Version on the way. Free Tryout: www.typestyler.com

LA Computer Company: LA Computer Company: Specials on AppleCare, Apple Displays, MacBooks, iMac's, MacBook Pros, Laptop and iPod accessories and more. Apple A/C Adapters for laptops starting at $25.00 Call 1-800-941-7654 or Click Here.

Other World Computing: OWC Mercury On-The-Go FW400/800/USB2/eSATA Solutions: High Performance A/V Rated, **Bus Powered** Hard Disk Storage - **Now 40GB and up to ***250GB*** in the palm of your hand. Macworld Magazine Editor's Choice - from $87.99!

Mac users can finally play Party Poker for Mac. Not only that, they can also learn how to play PokerStars for Mac.

Laptop Hardware Provided by TechRestore - Overnight Mac & iPod Repairs.

Compare products like desktop computers, laptops, and LCD TVs side by side! All the information and reviews to make the best purchasing decision for a new cell phone GPS products or MP3 players. The Ciao network makes searching products easy for you.

Memory For New Intel Core2 DUO MacBooks, MacBook Pro, MacMini & iMacs" 4GB Kit $80, 3GB Kit $60, 2GB Kit $40, 1GB $20. Click to Maximize your Macs...

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? LEM

Recent Online Tech Journal Columns

Recent Content on Low End Mac

Go to the Online Tech Journal index.

About LEM | Support | Usage | Privacy | Contacts


Have a question?
Ask an expert!

Low End Living

Amazon.com

Navigation

Used Mac Dealers
Apple History
Best Used Macs
Video Cards
Email Lists
InfoMac's Low
End Mac Forum

Favorite Sites

MacSurfer
MacMinute
MacInTouch
MyAppleMenu
InfoMac
Macs Only!
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac
   Museum

DealMac
DealsOnTheWeb
Mac2Sell
ramseeker
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End
   Mac FAQ

Abandonware
   Petition

Mac vs. PC Info

Affiliates

The Apple Store
Mac Connection
MacMall
TechRestore
MacResQ
ExperCom
Crucial Memory
batteries.com

Advertise

Open Link