Low End Mac's Online Tech Journal
Managing Local Links with PHP and MySQL
Website Automation with PHP and MySQL, Part 12
Dan Knight - 2002.07.03
We've finally finished automating link updates on the home page, the WebTV home page, the mobile edition home page (a subscriber-only feature), and most editorial content. It's been quite a project.
Creating the "local links" database was pretty simple, although we did have a few different fields compared to the external links database. Because most of our new content is available in regular, WebTV, and mobile versions (the latter especially formatted to work well on Palms), we need to track three different URLs. Here's the naming convention we use:
- General: http://lowendmac.com/column/02/0703.html
- Mobile: http://lowendmac.com/column/02/0703.htm
- WebTV: http://lowendmac.com/column/02/0703w.htm
Every link on the site begins with "http://lowendmac.com", so we don't have to include that part in our database. And rather than create three longer records with the balance of the URL, we use four: one to hold the "/column/02/" part and three to hold the general, mobile, and WebTV final part of the URL.
Entries are dated by their publication date and also time stamped with a Unix time code when entered into the database, just like we did with external links. In fact, except for having to track three types of pages, the local links database is very similar to the external links database.
Displaying the Data
Where things become very different is when we display the data. For external links, sorting by time stamp is fine, but we run into some issues - and some opportunities.
First, I want to include a link to "Today in LEM History." This could come first, but I prefer to focus on the new content and want the history link to show up after today's new articles. That means somehow separating today's links from yesterday's.
And that brings up another issue - days with no updates. I don't usually add content over the weekend or on holidays. So we can't simply search for today's date; we have to search for the most recent date and display that first.
Brian, who has been helping me a lot with this, was on a church sponsored mission trip in Washington, D.C., last week, so Stephen, our 14-year-old PHP wizard and the publisher of PetCube.com (which gets more traffic every day than Low End Mac!), helped me out.
Here's the snippet of code that finds that most recent date:
$latestdate = mysql_fetch_array(mysql_query("SELECT * FROM links ORDER BY timestamp DESC LIMIT 1"));
And here's the code that displays local links for that day:
- $get_links = mysql_query("SELECT * FROM links WHERE pubdate = '$latestdate[pubdate]' ORDER BY timestamp DESC");
- 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 "<a href=\"$array[path]$array[html]\">$array[linktext]</a>, ";
- if ($array[author]<>"")
- {echo"$array[author], ";}
- if ($array[columnname]<>"")
- {echo"$array[columnname], ";}
- echo "$array[pubdate].
- $array[description]</li>";
- }
This creates a bullet list item for local content just like we did last week with external links.
Next we add our history link with the following code:
- $thisdate = date(md);
- $thisrecord = date(nd);
- $today = date("F j");
- $get_links = mysql_fetch_array(mysql_query("SELECT * FROM lemhistory WHERE datefield = '$thisrecord'"));
if ($get_links == "") {die ("no record");}echo "<li> <a href=\"/arc/$thisdate.html\">$today in LEM history:</a>$get_links[stories]</li>";
|
We discovered the above code didn't quite work on July 8, when we had no archive link. Here's how the revised code:
The difference is that instead of telling this subroutine to die if there is no record, we now tell it to display a record if there is one. Problem solved. |
Our archive filenames are of the format MMDD, but our MySQL database strips off the leading zero in our integer field, which is why we need both $thisdate and $thisrecord. $today displays the date using the full name of the month and the one or two digit day. If there is no record for a specific date, which happens maybe twice a month, no LEM archive link is displayed.
After this, we need to display the previous day's links, and here's where we have a unique opportunity to change the sort order from timestamp to something else.
As you may recall, we had to call a second PHP script to create a counter for external links. With local links, I'm less concerned with how many times a link is followed from our home page and more interested in how many times the article itself is read. So instead of calling a second PHP script that increments the counter and then leads to the article in question, I've put a small PHP script on the article's page that increments the counter every time the article is read - even if it's being linked from MacSurfer or another outside source.
Based on the assumption that the article that most people read one day is the one from that date that will generate the most interest the next day, instead of sorting older links by timestamp, we sort them by popularity.
- $previous_date = mysql_fetch_array(mysql_query("SELECT * FROM links WHERE pubdate != '$latestdate[pubdate]' ORDER BY timestamp DESC LIMIT 1"));
- $previous_links = mysql_query("SELECT * FROM links WHERE pubdate = '$previous_date[pubdate]' ORDER BY clicks DESC");
- while ($previous_array = mysql_fetch_array($previous_links))
- {
- <display code omitted>
- }
The first line finds the second most recent date by only checking publication dates not equal to (!=) the latest date used above. And in the next section, which I'm not posting, we do the same thing for one day earlier by checking dates not equal to the $latestdate or $previous_date.
And this code puts in a lot of work. The same script is called up on the main Low End Mac home page; our iMac, Power Mac, and 'Book indexes; and at the bottom of a lot of our editorial content. (Eventually all of it, but it's going to take some time to update and upload all those pages.)
I've slightly modified this code for our mobile edition and
WebTV home pages. Best of all, I don't have to manually update all
of these pages 2-3 times daily - the computer is doing it for me
and making my life simpler.
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: Macintosh Portable, introduced 1989.09.20. The nearly 16 lb. behemoth was innovative but not a smashing success.
- February 13 in LEM history: 01: Layoffs may hurt Mac market - 02: Unix for the Mac - Rage against the Macintosh - 03: Options to move data from PCs to Macs - 04: Low cost RAM for older 'Books - 06: Apple, IBM, and Intel - 07: Picking the right cheap computer, new or used - 08: I needed to find an older Mac
- Support Low End Mac
Recent Content on Low End Mac
- 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.
- 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.
- 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.
- 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.
- 15 Years Ago Motorola Unveiled the PowerPC G3, Low End Mac Round Table, 2012.02.06. The G3 processor was optimized for real world Mac software and made a big leap forward in efficiency.
- Don't Kill Caps Lock, Learning to Love the iOS Keyboard, and an Adaptive iPad Keyboard, Charles W. Moore, Miscellaneous Ramblings, 2012.02.06. The Caps Lock key has a useful function, the iPad's keyboard really is useful, and checking out an adaptive keyboard for the iPad.
- More links in our archive.
Recent Deals
- Best MacBook Air Deals
- Best iBook G4 Deals
- Best iPad Deals
- Best Classic Mac OS Deals
- Best Apple TV Deals
- Best 15" MacBook Pro Deals
- Best Power Mac G4 Deals
- Best Mac OS X 10.6 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

