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
OWC: Get the Right Memory for Your Mac Top Quality, Competitive Price, Lifetime Backed Free Expert Support + Installation Videos too! MacBook & mini 8GB, iMac 16GB, Mac Pro up to 32GB. Click here
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.
Laptop Hardware Provided by TechRestore - Overnight Mac & iPod Repairs.
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: 17" iMac G4/800 MHz, July 2002 - The iMac 'grows up' with a 17" 1440 x 900 display.
- Group of the Day: LisaList supports Lisa users.
- November 8 in LEM history: 99: OS 9: I think I like it - 01: The simplified Mac life - Soured on Windows - Flea market Mac - 02: Little room for improvement in new 'Books - Combo drive upgrade for iceBooks - 04: Re-Porter - 05: Fix the old iMac or buy a Mac mini? - Apple's Copland project - 06: MacBook Core 2 - MacBook value equation - Cheap is as cheap does - 07: Problems with Classic mode in Tiger - The G4 Power Mac that won't run Leopard
- Support Low End Mac
Recent Content on Low End Mac
- Quad-Core CPU Makes Sense in MacBook Pro, OS X 10.6 Causing Overheating, Overseas Power, and More, The 'Book Review, 11.06. Also Late 2009 MacBook reviewed, how to add RAM to new MacBook, 18.4in Acer notebook used Intel i7, and SanDisk SSD chosen for Sony VAIO X.
- Dumping Macs for Google Apps, SSD in iMac, Late 2009 iMac Performance Problems, and More, Mac News Review, 11.06. /newsrev/09mnr/1106.html
- WiFi Paranoia, iMac-O-Lantern, Magic Mouse Does Click, Free Clipboard Managers, and More, Charles W. Moore, Miscellaneous Ramblings, 11.05. Also strange time stamps, problem with ColorIt on Intel Mac, and the story behind OS X 10.5.4 install discs.
- IDE Is Dead; Long Live SATA!, Dan Knight, Mac Musings, 11.04. SATA has displaced parallel ATA. While IDE hard drives haven't disappeared, the best deals are in SATA hard drives.
- QuickTime X in Snow Leopard Imports, Trims, and Publishes Video Quickly and Easily, Alan Zisman, Zis Mac, 11.04. The long, slow process of importing video into iMovie to edit it, then render it to another format, is history as QuickTime X does that much more quickly.
- More links in our archive.
Recent Deals
- Best Mac Pro Deals, 11.03. Used 2.66 GHz 4-core, $1,300; 3.0 8-core. $2,299; refurb 2.66 4-core Nehalem, $2,149; 2.93, $2,549; 2.26 8-core, $2,799; 2.93, $4,999.
- Best iPhone Deals, 11.03. New 8 GB iPhone 3G, $$99; refurb 16 GB 3GS, $149; new, $199; 32 GB, $299.
- Best 12" PowerBook G4 Deals, 11.03. Used 867 MHz SperDrive, $348; 1 GHz, $499; 1.33 Combo, $298; SD, $559; 1.5 Combo, $448; SuperDrive, $589.
- Best Power Mac G3 and PCI Video Card Deals, 11.02. Used beige 300 MHz, $25; G4/366, $49; blue & white 350, $80; 400, $90; 450, $105; PCI video cards from $15; shipping additional.
- Best Power Mac G4 and AGP Video Card Deals, 11.02. Used 400 MHz, $50; 733 MHz, $69; 933 MHz, $209; 1.25 GHz dual, $299.
- Best 15" MacBook Pro Deals, 11.02. Used 2.0 GHz, $800; 2.2, $900; 2.4, $1,000; refurb 2.53, $1,449; 2.66, $1,699; 2.8, $1,949; 3.06, $2,169; new 2.53, $1,579; 2.66, $1,799; more.
- Best Mac mini Deals, 10.30. Used 1.33 GHz G4 mini, $379; 1.42, $389; 1.5, $419; 1.83 GHz Core Duo, $350; Core 2, $439; new 2.26 GHz nVidia, $580; 2.53 GHz, $770; Server, $990.
- Best G4 iBook Deals, 10.30. Used 12" 1.07 GHz Combo, $225; 1.33 GHz, $298; 14" 1 GHz, $349; 1.33 GHz, $398; 1.42 GHz SuperDrive, $498.
- Best Classic Mac OS Deals, 10.30. System 6.0.8 floppies, $10; 7.1, $12; 7.5, $20; 7.5 CD, $4; 7.6 $13; 8.1, $11; 8.5, $20; 8.6, $90; 9.0, $20; 9.2.2, $30.
- 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
Advertise
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
