Displaying Stock Quotes and Timestamps with PHP
Website Automation with PHP and MySQL, Part 13
Low End Mac Reader Specials
Memory To Go Special: MacPro 8 Core Memory 4GB kit $154 / 2GB kit $94, New 2008 iMac 2GB $46. 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: 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: NewerTech NuPower Batteries for iBook and PowerBooks Designed+Built in USA to run longer, LAST LONGER TOO! Free Battery Recycling Return Label; Quality High-Capacity from $99.95
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.
MacPro Memory 667Mhz With Apple Spec Heat Sink 2GB $ 82 / 4GB $128 / 8GB $256 - Click to Maximize your Macs...
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
- 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: PowerBook 170, Oct. 1991 - At 25 MHz, the PB 170 was at the top of the original PowerBook line.
- List of the Day: The iPhone List Low End Mac's forum for discussing and supporting Apple's iPhone.
- August 29 in LEM history: 00: My lowest low-end Mac - 01: Uncluttered organization - Microsoft wins over Mac user - 02: Salute to SatireWire - 03: Wireless Internet popping up everywhere - 05: World domination, online or off - A 3-dimensional Dock replacement - 06: Productive at the low end - PowerPC vs. Intel - Secure wireless
Recent Content on Low End Mac
- Looking for a Content Management System That's as Easy as Mac, Dan Knight, Mac Musings, 08.29. Low End Mac needs to move to a content management system, but the few we've tried just don't cut it for people used to the simple elegance of the Mac.
- First 3 Million Mac Quarter, Skinny on Mac mini Pricing, Mac-like gOS, and More, Mac News Review, 08.29. More plan to buy Apple products than ever before, complete reset can fix MobileMac synch problems, Apple boosting computer and smartphone share, and more.
- New 'Books Likely in September, 17" PowerBook Display Fault Site, SSD Security, and More, The 'Book Review, 08.29. Also 6 ways to speed up your MacBook, next generation MacBook Air CPU, MacBook Air Update, LapStrap carrying solution, rise and fall of ultraportables, bargains from $220 to $2,699, and more.
- iPhone 3G Reception 'Completely Normal', AT&T International Data Plans for iPhone, and More, iNews Review, 08.29. Also longer life for iPod earbuds, an alternative to MobileMe, new cases and apps for iPhone, AppStoreGems website launched, and more.
- Best Power Mac G4 Deals, Low End Mac Deals, 08.29. Used 450 MHz AGP, $75; 500, $99; 800 QS, $199; 1.25 GHz MDD, $375; 450 MHz dual, $179; 867 dual, $300; 1 GHz dual, $395; 1.42 dual, $575.
- Best iBook G3 Deals, Low End Mac Deals, 08.29. Used 300 MHz clamshell, $150; 500 CD, $150; 800, $200; 600 CD-RW, $240; 900 Combo, $300; 14" 600, $360; 900, $400.
- Best iPod nano Deals, Low End Mac Deals, 08.29. Used 2 GB iPod nano, $89; refurb 3G 4 GB, $99; new, $140; refurb 8 GB, $149; new, $179.
- 10 Mac Browsers Compared, Simon Royal, Mac Spectrum, 08.28. A look at Internet Explorer, Radon, Opera, Safari, Shiira, iCab, Firefox, Netscape Navigator, Flock, and Camino running in Leopard.
- Clone and Boot: Another Advantage of the Mac OS, Kev Kitchens, Kitchens Sync, 08.28. Unlike Windows, Apple makes it possible to clone a bootable drive (Classic Mac OS or OS X) and use it with another supported Mac.
- Best MacBook Deals, Low End Mac Deals, 08.28. Used 1.83 GHz, $799; 2.0 black, $875; refurb 2.1 GHz, $899; 2.4, $1,099; black, $1,299; new 2.1, $1,019 after rebate; 22, $1,094; 2.4, $1,219 a/r; black, $1,394 a/r.
- Best iMac G5 Deals, Low End Mac Deals, 08.28. Used 17" 1.6 GHz Combo, $499; 1.8 SuperDrive, $530; 2.0, $600; 1.9 iSight, $625; 20" 1.8 GHz, $580; 2.0, $650; 2.1 iSight, $700.
- Best classic Mac OS Deals, Low End Mac Deals, 08.28. System 6, $10; 7.1, $12; 7.5.1, $4; Mac OS 7.6, $13; 8.0, $13; 8.1, $48; 8.5, $25; 8.6, $20; 9.0, $20; 9.2.2, $20; more.
- CrossOver Strikes Out, Frank Fox, Stop the Noiz, 08.27. Running Windows apps on a Mac without paying for Windows is great in theory, but actually getting Windows software working is another story.
- MacDrought: 4 Months with No New Macs, Dan Knight, Mac Musings, 08.27. The most recent Mac update was over four months ago, and the Mac mini has been unchanged for over a year.
- Resurrecting a Dead Pismo, Spotlight Search Tip, and EasyFind a Good File Finder, Charles W. Moore, Miscellaneous Ramblings, 08.27. Lots of tips on bringing a comatose Pismo back to life, a Spotlight file name search tip, and EasyFind as an alternative to Spotlight.
- Best Intel iMac Deals, Low End Mac Deals, 08.27. Used 17" 1.83 GHz, $625; 20", $599; 2.16, $749; 24", $950; refurb 20" 2.4, $999; 2.66, $1,299; 24" 2.4, $1,299; 2.8, $1,549; new 3.06, $2,094 after rebate; more.
- Best 15" PowerBook G4 Deals, Low End Mac Deals, 08.27. Used 1.25 GHz Combo, $600; SuperDrive, $650; 1.33 Combo, $640; 1.5, $680; SD, $725; 1.67, $730; hi-res, $800.
- Best Time Capsule and AirPort Deals, Low End Mac Deals, 08.27. 500 GB Time Capsule, $294; 1 TB, $468; AirPort Extreme Card, $39; 802.11n Base Station, $166; 802.11g AirPort Express, $60; 802.11n, $98.
- More links in our archive.
Go to the Online Tech Journal index.
About LEM | Support | Usage | Privacy | Contacts

