Home Profiles Articles Groups Deals News Software Mac Help News Feed
Low End Mac Reader Specials
TypeStyler For Mac OS X is Now Shipping! Download The Free Fully Functional 60 Day Tryout at www.typestyler.com
Don't install Parallels to play poker online! Poker Mac will show you how
to download and install a native Mac poker application such as Full
Tilt Poker Mac.
Low End Mac's Online Tech Journal
Displaying Stock Quotes and Timestamps with PHP
Website Automation with PHP and MySQL, Part 13
Dan Knight - 2002.07.16
We'll start by noting a programming error in the previous column.
Here's the code we first came up with to see if a record existed:
- if ($get_links == "") {die ("no record");}
- echo "<li> <a href=\"/arc/$thisdate.html\">$today in LEM history:</a> $get_links[stories]</li>";
It seemed to work just fine, until the day we didn't have a record. Then it did what the die command is supposed to do - stopped execution. Oops.
Thinking things through, I revised the code. Instead of testing if there was no data, I tested if there was data as follows:
- if ($get_links <> "")
- {echo "<li> <a href=\"/arc/$thisdate.html\">$today in LEM history:</a> $get_links[stories]</li>";}
Voilà, problem solved. And we've had the previous column fixed for some time to reflect the change.
We discovered one more problem: The 80 character field for URLs just wasn't long enough for some of the sites we've linked to. We ended up boosting that field length to 128 characters to play it safe.
Automating the Date Stamp
This week we've made to more automation changes to the site. The first was automating the time stamp that appears on the home page and in a few other spots. I've been manually updating it for years - and sometimes making mistakes.
Also, since we're adding links throughout the day, the manual timestamp was incorrect in no time at all. Time to figure out how to display the time of the most recent link addition.
On our Apple Quicklinks headline news site, this was easy. We have one database of links, so we simply check for the most recently added one, like this:
- $last_added = mysql_fetch_array(mysql_query("SELECT * FROM xlinks ORDER BY timestamp DESC LIMIT 1"));
- $timestamp = $last_added[timestamp];
- echo "<h5>Most recent link added ";
- echo date("h:i a T l, F j, Y", $timestamp);
- echo "</h5>";
The first line selects the most recently added record, and the next line reads the timestamp. Then we display it with the format hour, minute, am/pm, time zone, weekday, month, date, and year.
But on Low End Mac, we have two kinds of links - those on our site and those on other sites. Each is kept in a separate database, so we couldn't do things quite as simply as on the Quicklinks site.
Here's what we came up with for LEM:
- $latestlem = mysql_fetch_array(mysql_query("SELECT * FROM links ORDER BY timestamp DESC LIMIT 1"));
- $latest = mysql_fetch_array(mysql_query("SELECT * FROM xlinks ORDER BY timestamp DESC LIMIT 1"));
- if ($latestlem > $latest)
- {$latest = $latestlem;}
- $latest = $latest[timestamp];
- echo "<h4>Most recent link added ";
- echo date("l, F j", $latest);
- echo " at ";
- echo date("h:i a T", $latest);
- echo "</h4>";
The first line reads the timestamp from the latest local article, and the next line reads the timestamp from the most recently added external link. We compare them to decide which is more recent, then display them as a header 4 like this:
Most recent link added Monday, July 15 at 04:43 pm EDT
And now I don't have to manually post timestamps when I'm adding new content or worry about a manually entered timestamp not reflecting that fact that we may have added content since then.
Apple Stock Tracker
I've been looking for an automated stock tracker for years. Long before I even thought about updating links automatically, I wanted a real time (well, the usual 20 minute delay) stock tracker. I never did find a simple one I could add to the site.
So Monday I spent some time searching the Web, fiddling with some PHP samples, referencing php.net, testing and debugging, tweaking, and finally came up with a nice simple stock tracker that does just one thing: Report the latest trade in AAPL stock.
I found far more than I was looking for on the EvilWalrus Group website - a script by Mukul Sabharwal that does far more than I wanted, right down to displaying graphs.
All I wanted was the current price and how much it had changed during the day, so I had a lot of code stripping to do. Here's what I ended up with:
- $fd = fopen ("http://quote.yahoo.com/d/quotes.csv?s=AAPL&f=sl1d1t1c1ohgv&e=.csv", "r");
- $contents = fread ($fd, 200);
- fclose ($fd);
- $contents = str_replace ("\"", "", $contents);
- $contents = explode (",", $contents);
- echo "<ul><li>Apple stock tracker. Most recent trade: <b>\$$contents[1]</b>. Change: $contents[4]. (Prices delayed up to 20 minutes.)</li>";
The first line opens the AAPL record on the quotes.yahoo.com website and reads it. $contents = str_replace ("\"", "", $contents); goes through the data and strips out quote marks. The next line "explodes" commas, which means it turns every comma into a field delimiter.
The only fields I'm interested in are 1 (the last traded price) and 4 (how much Apple stock has moved up or down today). And the LEM home page now displays that information - generated each time someone visits the home page.
One less thing to update manually, and one more thing to put on
the Apple Quicklinks site when I have a chance.
Recent Online Tech Journal Columns
- Optimized Software Builds Bring Out the Best in Your Mac, 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, 06.15. What is Safe Sleep mode? Which Macs support it? How can you enable or disable it? And more.
- The Original Macintosh, 01.12. An in-depth look at the original Macintosh and how it shaped future Macs.
- The Innovative Lisa, 01.08. Apple's Lisa and how it paved the way for the Macintosh.
- More in the Online Tech Journal index.
Links for the Day
- Mac of the Day: PowerBook 2300, Aug. 1995 - The only Duo equipped with a PowerPC processsor run at 100 MHz.
- Group of the Day: MichiMac is our list for Mac users in Michigan.
- September 9 in LEM history: 98: The LCDs are coming - 99: PowerBook G3 WallStreet 13.3" display issues - 02: Building a fast, reliable network economically - Using 2 mice at once - 03: Mac virus writers return from extended cruise
- Support Low End Mac
Recent Content on Low End Mac
- Education Trends: Netbooks and iPads, John Hatchett, Recycled Computing, 09.08. "Two relatively new devices are going to make an impact on computers in education."
- Trapped on the Dark Side, Kodachrome and PowerPC Obsolescence, Graphic Software Overview, and More, Mac News Review, 09.03. Also ErgoMotion mouse tilts, rocks, and rolls, Back-In-Time manages your Time Machine backups, PhotoLine image editor, limited-time discount on Logos 4 Mac, and more.
- 5 Strategies That Keep Apple Ahead of the Competition, Frank Fox, Stop the Noiz, 09.03. Apple covers the MP3 price range, innovates while sticking with what sells, uses new features to differentiate products, knows its users, and thus remains the leader.
- Apple Overhauls iPods and Apple TV, $159 iPad Challenger, Flash on a Smartphone, and More, iNews Review, 09.03. iPod touch gets FaceTime and Retina Display. iPod nano goes touch. Tiny Apple TV now an iOS device. Also an app for reporting bad drives, new iPad cases, and more.
- Mac Gamers Prefer a MacBook Pro, Mid 2010 MacBook Pro EFI Firmware Update Released, and More, The 'Book Review, 09.03. Also NewerTech cuts prices on battery charger/conditioner, new Logitech speakers for laptops, The Reg's back-to-school netbook guide, and more.
- MailForge Not Truly Useful, Eudora Problems with Leopard, Firefox 4 Beta for PPC, and More, Charles W. Moore, Charles Moore's Mailbag, 09.02. Also a Pismo PowerBook that refuses to come back from the dead.
- More links in our archive.
Recent Deals
- Best iPod touch Deals, 09.03. Used 1G/32, $199; 3G/64, $300; refurb 3G/8, $149; 3G/32, $230; 3G/64, $339; close-out 3G/8, $174; 32, $249; 64, $339; 5G/8, $223; 32, $288; 64, $383.
- Best iPod nano Deals, 09.03. Used from $79; refurb 5G/8 GB, $99; close-out, $129.54; refurb 16 GB, $149; close-out, $158 to $160; 6G/8 GB, $144; 16 GB, $172. Shipping included.
- Best iPod shuffle Deals, 09.03. Close-out 2 GB, $49; 4 GB, $65; new 5G/2 GB, $49. Shipping included.
- Best Power Mac G5 Deals, 09.02. 1.6 GHz single, $200; 1.8 dual, $300; 2.0, $350; 2.3, $400; 2.5, $500; Quad, $730.
- Best MacBook Air Deals, 09.02. Used from $969; refurb 1.86 GHz/120 HD, $1,249; 128 SSD, $1,349; 2.13, $1,549; new 1.86 GHz/120 HD, $1,428; 2.13 GHz/128 SSD, $1,707.
- Best Mac mini Deals, 09.01. Used 1.25 GHz G4, $250; 1.5 Core Solo, $325; 2.0 Core 2, $439; close-out 2.66, $699; 2.53 Server, $819; new 2.4, $669; 2.66, $849; Server, $949.
- Best iBook G4 Deals, 09.01. 12" 1 GHz Combo, $200; 1.33 GHz, $250; 14" 1 GHz, $285; 1.33 GHz, $320; 1.42 GHz, $448; SuperDrive, $500.
- More deals in our archive.
About LEM Support Usage Privacy Contact
Follow LEM on Twitter
LEM on Facebook
Navigation
Used Mac Dealers
Apple History
Video Cards
Email Lists
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
B&H
MacMall
TechRestore
ExperCom
Crucial
Memory
batteries.com
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
Mac Connection
B&H
MacMall
TechRestore
ExperCom
Crucial Memory
batteries.com
