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.
Compare products like desktop computers, apple laptops, apple macs, and LCD Monitors side by side! All the information and reviews to make the best purchasing decision for new mobile phones, sat nav systems, or MP3 players. The Ciao online shopping community makes searching products easy for you.
Low End Mac's Online Tech Journal
Using PHP to Display Your Data
Website Automation with PHP and MySQL, Part 4
Dan Knight - 2002.04.26
Last time we filled our database with all the information we needed for our Mac of the Day. Today we'll look at how we extract and display that data.
Again, as the PHP neophyte, I was grateful to Brian <http://brkn.net/> and his expertise at getting this up and running quickly. (Brian's "Buck and a Quarter" game is done using PHP and MySQL.)
Speaking PHP
I started by creating a file called text.html. PHP code has to be separated from regular HTML, so each section of code begins with <?php and ends with >?. Between that is where we do our programming.
The next step is to define where our database is. $host is the name or IP address of your MySQL server, $username is your user ID, and $password is your password. (I've changed them here to protect my databases.) All of these fields are contained between quote signs, and each line ends with a semicolon, as below:
- $host = "sqlserver.lowendmac.com";
- $username = "MyName";
- $password = "MyPassword";
We next define our connection string $connection = @mysql_connect($host,$username,$password); and then tell PHP which MySQL database to use:
- mysql_select_db(lem_mod,$connection);
We haven't even looked at the data yet, only told the server where it's located.
Since we want to change the Mac of the Day entry each day, we next create a field that will change every day. PHP has several ways of doing this, and we chose to use the Unix time stamp (seconds since midnight 1/1/1970 GMT) and divide by 86,400, the number of seconds in a day.
But we didn't want the Mac of the Day to change at midnight in England; we wanted it to switch after midnight on the American west coast, so we added 32,400 to Unix time - that's nine hours. This also coincides with the time of day when traffic to our site is at its lowest. And then we round it off to a whole number.
Here's the resulting line of code:
- $today = round((date(U) + 32400) / 86400);
$today creates a unique number and changes every day at the same time. Now we needed to use that number to choose the Mac of the day. Using modulo (clock) arithmetic, we defined $mod (mac of day) by dividing $today by 89 (the number of Macs in our list) and discarding the remainder:
- $mod = $today % 89;
Now we're finally ready to look at our database with a query command:
- $query = mysql_fetch_array(mysql_query("select * from mod where id = '$mod'"));
This tells the server to look up record number $mod. Next we have to tell it what to do with the data it finds.
- echo "<LI>Mac of the Day: <A HREF=/$query[link]>$query[model]</A> $query[text]</LI>";
The echo (or print) command tells the server to send out the information enclosed between quotes. The first thing we echo is <LI>, which is the HTML code to being an item in a bullet list. Next come the words "Mac of the Day:" followed by a space and the HTML code (<A HREF=/ - the slash signifies the root level of our serer) that creates the link to the proper page.
We follow this with a greater than sign to close the link, followed by the model name of our Mac of the Day. After this comes </A> to close the link text and add a space between the model name and the following text. Finally comes the text about that Mac and the closing </LI> to conclude the item in our bullet list. The ending quote mark and semicolon complete the line.
And when it all runs, it produces the following line of HTML:
- <A HREF="/compact/macintosh-plus.html">Mac Plus</A> (1/86-10/90). First Mac with SCSI, memory expansion. Longest model life - over 4 years.
Which displays thus:
- Mac Plus (1/86-10/90). First Mac with SCSI, memory expansion. Longest model life - over 4 years.
A lot of work? Maybe at first, but if it works, it will save me maybe 5 minutes per day for the life of the site. That's worth an hour or two of creating databases and coding.
-
- <?php
- $host = "sqlserver.lowendmac.com";
- $username = "MyName";
- $password = "MyPassword";
- $connection = @mysql_connect($host,$username,$password);
- mysql_select_db(lem_mod,$connection);
- $today = round((date(U) + 32400) / 86400);
- $mod = $today % 89;
- $query = mysql_fetch_array(mysql_query("select * from mod where id = '$mod'"));
- echo "<LI>Mac of the Day: <A HREF=/$query[link]>$query[model]</A> $query[text]</LI>";
- ?>
-
After all that, it should have worked the first time - and
it did. But then we ran into some problems, things to address in
the next column.
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: Lisa, Jan. 1983 - The ancestor of the Macintosh had a mouse, a graphical interface, and a $10,000 price tag.
- Group of the Day: Unsupported OS X is for those using OS X on unsupported hardware.
- March 21 in LEM history: 00: The compelling Mac - 01: All that for $129? - PowerBook 100 - 02: Improving AppleWorks - 03: The G3 ain't dead yet - Pismo a good value - Western Digital drive issues - 05: iPod halo effect - 06: Rip DVDs so you can watch them on your iPod - 07: Maximum drive size in older Macs - 08: Safari 3.1 fastest browser?
- Support Low End Mac
Recent Content on Low End Mac
- Does iPhone OS Need Multitasking?, iCab Comes to iPhone, Canada's Proposed iPod Levy, and More, iNews Review, 03.19. Also the iPad paradox, Freescale demos $200 tablet, gardening apps, aluminum iPhone stand, steel iPhone case, and more.
- Could iPad Replace the Mac?, Mac Sales Up in 2010, Avoiding Windows 7 'Whenever Possible', and More, Mac News Review, 03.19. Also why your next Mac may be an iPad, science blogger abandons Apple, the benefits of standing while working, and more.
- The Mobile System Stampede, Lithium Battery That Can't Explode, Affordable SSD Options, and More, The 'Book Review, 03.19. Also June 2007 MacBook Pro external display issue, laptop stands, 1 TB ultraportable hard drive, Mini DisplayPort/HDMI adapter, and more.
- How to Zoom Your Browser for a More Readable Web, Steve Watkins, The Practical Mac, 03.18. Instructions for zooming text and pages in Safari, Firefox, Camino, and Opera.
- CardBus WiFi, the Shiira Browser, Ridding the Web of Flash, and Macs vs. PCs, Charles W. Moore, Miscellaneous Ramblings, 03.18. Mac longevity, Shiira speed, ambidextrous Mac and Windows use, and how Flash benefits Apple.
- How Ad Blocking Hurts Your Favorite Websites, Charles W. Moore, Miscellaneous Ramblings, 03.18. Ad income keeps the Web free. Blocking online ads hurts your favorite websites.
- Taking Apart the 12" PowerBook, John Hatchett, Recycled Computing, 03.17. There are a lot of steps involved in disassembling a 12" PowerBook. Proceed with caution.
- More links in our archive.
Recent Deals
- Best Intel iMac Deals, 03.17. Used 17" from $600; 20" from $750; 24" from $825; refurb 21.5" nVidia, $999; new, $1,099; refurb Radeon, $1,299; new, $1,399; refurb 27" 3.06, $1,499; more.
- Best G5 iMac Deals, 03.17. 17" 2.0 GHz, $380; 1.9 GHz iSight, $479 shipped; 20" 1.8 GHz, $509 shipped; 2.1 GHz iSight, $549 shipped.
- Best Time Capsule Deals, 03.17. Close-out 500 GB, $140; new 1 TB, $279; used 2 TB simultaneous dual-band, $400; new, $455. Shipping included.
- Best iPad Deals, 03.16. 16 GB iPad, $499; 32 GB, $599; 64 GB, $699; 16 GB with 3G, $629; 32 GB 3G, $729; 64 GB 3G, $829. Free ground shipping.
- Best iPod classic Deals, 03.12. Used 20 GB, $119; 40 GB, $139; 60 GB, $159; 30 GB video, $129; 60 GB, $159; 80 GB, $169; refurb 120 GB, $189; new, $214; 160 GB, $228 shipped.
- Best G3 iBook and AirPort Card Deals, 03.12. 366 MHz 12" clamshell, $89; 466, $125; 500 white CD, $100; 600, $199; 800 Combo, $239; 14" 900, $225.
- Best Xserve Deals, 03.12. Used 1 GHz dual G4, $499; 2.0 dual G5, $599; 2.3, $749; refurb 2.26 4-core Nehalem, $2,499; new, $2,699; 8-core, $3,449; refurb 2.66, $4,299; new, $4,799; more.
- More deals in our archive.
About LEM | Support | Usage | Privacy | Contacts
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
