Displaying External Links with PHP
Website Automation with PHP and MySQL, Part 10
Low End Mac Reader Specials
Memory To Go Special: New 2008 iMac 2GB $42 / iMac Intel Core2 DUO & MacBook Pro 2GB $36 - 1GB $20. MacPro 8 Core Memory 4GB kit $154 / 2GB kit $94 -- 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: Specials on AppleCare, iMac's, Apple Batteries and Apple A/C Adapters. Also Great prices on Used Apple Computers. Call 1-800-941-7654 Click Here.
OWC: Top Quality Memory for Faster Mac Performance 1GB/2GB/4GB Kits from $23.99/$47.99/$94.99 Expert Support, Free Installation Videos & Guides, Lifetime Advance Replacement Warranty - www.MacSales.com
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.
MacBook/MacBook Pro / MacMini / iMac Intel Core2 DUO DDR2 667Mhz 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:
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?
Recent Online Tech Journal Columns
- Apple's AAUI ethernet connector, 09.04. From 1991 through 1995, Apple used a proprietary ethernet connection. Why they created AAUI and where to find adapters.
- PowerPC G5: Apple's last fling with PowerPC architecture, 05.24. Teaming up with IBM, Apple adopted the PowerPC G5 in 2003 - and phased out the last G5 Power Mac three years later.
- The PowerPC G4: From 350 MHz to 2.0 GHz, 05.24. AltiVec and dual processor support made the G4 a big improvement over the earlier G3 processor.
- The PowerPC G3 story: From 233 MHz to 1.1 GHz, 05.24. The history of the PowerPC 750 family and its use in Apple computers from 1997 through 2004.
- More in the Online Tech Journal index.
Links for the Day
- Mac of the Day: iMac Core2, Sep. 2006 - Apple introduced the biggest screen ever in an iMac with a 24" Core2 Duo model at 2.16 GHz.
- List of the Day: MacBook List for those using a MacBook or MacBook Pro.
- September 6 in LEM history: 99: G4 vs. Pentium III - 00: Setting up a server - 02: Norton Utilities warning - 10 greatest computer annoyances - 06: iMac Core2 Duo - Mac mini Core Duo - The iMac Core2 value equation - 07: Apple seduction - Why I really want an iPod touch - iPod history, 2005 to present - Upgrading a Power Mac G - Apple intros iPod touch, classic, and video nano
Recent Content on Low End Mac
- Anticipation: New iPods Now, New Macs Later, Kev Kitchens, Kitchens Sync, 09.05. The season of new iPods is at hand, but new Macs may wait until 2009.
- Listen to Just the Music with the V-Moda Vibe Earbuds, Tommy Thomas, Welcome to Macintosh, 09.05. Well built, the noise canceling earbuds will let you hear all the nuances of your music without letting through background noise.
- Buy a MacBook Now or Wait?, MacBook touch Patents, Samsung X360 Takes on MBA, and More, The 'Book Review, 09.05. Also 20 years of portable Macs, data backup and preservation, universal U-Charge battery charger for Mac 'Books, bargain 'Books from $150 to $2,699, and more.
- Source of iPhone 3G Problems, Army Uses iPods as Field Translators, Gains with Business, and More, iNews Review, 09.05. Also UK bans iPhone ad as 'misleading', iPhone password easy to bypass, GM to offer radios with USB in 2009 models, weather tracking software, and more.
- Macs Gain Ground in August, Consumers Most Likely to Buy Macs, LaCie USB Speakers, and More, Mac News Review, 09.05. Also migrating Time Machine to a new drive and two new keyboards from Logitech.
- Best 15" MacBook Pro Deals, Low End Mac Deals, 09.05. Used 1.83 GHz Core Duo, $999; 2.16, $1,125; new, 2.2, $1,450 after rebate; refurb 2.4, $1,699; 2.5, $1,999; 2.6 Santa Rosa, $1,849; rebates on new.
- Best iPod touch Deals, Low End Mac Deals, 09.05. Refurb 8 GB, $199; new, $284; refurb 16 GB, $299; new, $370; refurb 32 GB, $399; new, $453.
- Best iMac G4 Deals, Low End Mac Deals, 09.05. Used 15" 700 MHz CD-RW, $279; 800 Combo, $300; 1 GHz $390; 17" 800 MHz SD, $439; 1.25 GHz, $449; 20", $569.
- Overclocking a Mac mini Got Me Hooked on Souping Up Macs, Adam Geller, My First Mac, 09.04. Stories of hot rodding iBooks, G3 iMacs, and PCI Power Macs on the cheap.
- Apple Will Not Abandon Optical Drives, the Mac Drought, Purposeful Mac Acquisition, and More, Dan Knight, Low End Mac Mailbag, 09.04. Also Mac OS X 10.5 on a G4-upgraded Blue & White G3 and problems using a flat panel display with a Quadra 700.
- Only Leopard Runs Routine Maintenance Tasks after Startup or Waking from Sleep, Charles W. Moore, Miscellaneous Ramblings, 09.04. Mac OS X 10.5 runs routine system maintenance scripts as soon as possible after starting up or waking up your Mac. Earlier versions of OS X do not do this.
- Tomorrow's Solid State Drives and Notebooks, Dan Knight, Mac Musings, 09.04. Flash drives are great but have some shortcomings. Some thoughts on building better SSDs and notebooks to use them.
- Best 12" PowerBook G4 Deals, Low End Mac Deals, 09.04. Used 867 MHz Combo, no APX, $490; 1 GHz, $550; SuperDrive, $625; 1.5 GHz w/o APX, $660; w/APX, $675.
- Best Mac mini Deals, Low End Mac Deals, 09.04. Used 1.25 GHz G4 SD, $549; 1.42 Combo, $409; new 1.83 Core2 Combo, $569 after rebate; 2.0 SD, $769 after rebate.
- Best 17" PowerBook G4 Deals, Low End Mac Deals, 09.04. Used 1 GHz, $779; 1.33 GHz, $799; 1.5 GHz, $859; 1.67 GHz, $910.
- 11 Mac Browsers Compared, Simon Royal, Mac Spectrum, 09.03. The latest versions of Internet Explorer, Opera, Safari, Shiira, iCab, Radon, Firefox, Netscape Navigator, SeaMonkey, Flock, and Camino tested in Leopard.
- Save Internet Radio, USB and Hard Drives, Hardware Manufacturers vs. Linux, and More, Dan Knight, Low End Mac Mailbag, 09.03. Also Mac won't book after cleaning, newer versions of OS X improve wake from sleep, downgrading to OS 8.6, unreadable pages on Low End Mac, and more.
- Another Free POP3 Provider, Recharging a Dead PRAM Battery, Current Kanga Value, and More, Charles W. Moore, Miscellaneous Ramblings, 09.03. GMX email now available in US, Panasonic UJ-841S drive won't burn discs, restoring a dead PRAM battery in a Pismo, and thoughts on Kanga value today.
- Best eMac Deals, Low End Mac Deals, 09.03. Used 700 MHz Combo, $120; 1.25 GHz SuperDrive, $150; 1.42 GHz, $349.
- Best Mac OS X 10.5 'Leopard' Deals, Low End Mac Deals, 09.03. Mac OS X 10.5, single user, $99; 5 users, $140; 10.5 Server, 10 users, $395; unlimited, $850.
- Best MacBook Air Deals, Low End Mac Deals, 09.03. Refurb 1.6 HD, $1,499; new, $1,690 after rebate; refurb 1.8, $1,699; new, $1,919 a/r; refurb 1.6 SSD, $2,099; new, $2,294 a/r; refurb 1.8, $2,299; new, $2,400 a/r.
- Psystar Strikes Back, Countersues Apple, Frank Fox, Stop the Noiz, 09.03. Psystar is trying to paint Apple as a monopoly and force it to license the Mac OS.
- More links in our archive.
Go to the Online Tech Journal index.
About LEM | Support | Usage | Privacy | Contacts

