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:
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.
Recent Online Tech Journal Columns
- Optimized Software Builds Bring Out the Best in Your Mac, 2009.06.30. Applications compiled for your Mac's CPU can load more quickly and run faster than ones compiled for universal use.
- Low End Mac's Safe Sleep FAQ, 2009.06.15. What is Safe Sleep mode? Which Macs support it? How can you enable or disable it? And more.
- The Original Macintosh, 2009.01.12. An in-depth look at the original Macintosh and how it shaped future Macs.
- More in the Online Tech Journal index.
Links for the Day
- Mac of the Day: Mac IIfx, introduced 1990.03.19. This 'wicked fast' 40 MHz Mac trumped the 33 MHz DOS world.
- February 14 in LEM history: 98: A perfect compact Mac - 00: Extended computer warranties worth the cost? - Making your PC work with your Mac - 01: Customize Microsoft Word - 02: Quadra revives a passion for computing - 03: Real world performance - DIY Pismo screen replacement - Best Mac for writing - 03: Fastest browser on the Mac - 06: 15" MacBook Pro - Impressions of a newly acquired Lisa - Finding and using free WiFi - Apple should liberate OS 9 - 07: New Mac mini cheaper than upgrading a Power Mac - 08: Falling in love with OS X
- Support Low End Mac
Recent Content on Low End Mac
- PowerBook 165c: 19 Years of Color to Go, Chris Carson, Building Bridges, 2012.02.14. Until 1993, all of Apple's notebook computers had black and white displays. The 165c gave us a color PowerBook for the first time.
- Mac and iOS Browsers: Options Galore, Freeware Forum, 2012.02.10. Safari is adequate on Mac and great on iOS, but the range of good alternatives is stunning. LEM writers share their favorites.
- White MacBook Goes End-of-Life, Logitech Touch Mouse Supports Gestures, Firmware Updates, and More, The 'Book Review, 2012.02.10. Also MacBook Air better than any Ultrabook, docks for MacBook Pro models, Intel offers improved SSDs, and more.
- Fix Home Button Delay, Tablet the Ultimate Mobile PC, iPad Notebook a Possibility, and More, iOS News Review, 2012.02.10. Also using your iPad at work, two photo editors, a new iPad text editor, Macally's magnetic iPad 2 stand, and more.
- Apple's Support Lead Shipping, Smartphones Outsell PCs, OS X Ported to ARM by Intern, and More, Mac News Review, 2012.02.10. Also the power of Tex-Edit Plus, Google and Twitter are already censoring the Web, Snow Leopard Security Update, and more.
- LogMeIn: Remote Screen Sharing for the Rest of Us, Alan Zisman, Zis Mac, 2012.02.09. Configuring the Mac's built-in screen sharing to work over the Internet can be difficult or impossible. LogMeIn makes it easy.
- More links in our archive.
Recent Deals
- Best 17" MacBook Pro Deals
- Best iPod classic Deals
- Best eMac Deals
- Best MacBook Air Deals
- Best iBook G4 Deals
- Best iPad Deals
- Best Classic Mac OS Deals
- Best Apple TV Deals
- More deals in our archive.
About LEM Support Usage Privacy Contact
Follow
Low End Mac on Twitter
Join Low End Mac
on Facebook
Low End Mac Reader Specials
TypeStyler 11 is now in the Mac App Store!! -- Special Introductory Price of $59.95!! -- To Buy From The Mac App Store Click Here Now!! Or buy direct
from Strider Software.
Don't install Parallels to play poker online! Poker Mac will show you how to download and install a native Mac poker and Mac Casino applications in minutes.
Favorite Sites
MacSurfer
Cult of Mac
Shrine of Apple
MacInTouch
MyAppleMenu
InfoMac
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac Museum
DealMac
Deal Brothers
Mac2Sell
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ
Affiliates
Amazon.com
The Apple Store
The iTunes Store
PC Connection Express
GainSaver
Parallels Desktop for Mac
eBay

