Low End Mac's Online Tech Journal

Fixing a Small Scripting Problem

Website Automation with PHP and MySQL, Part 6

Low End Mac Reader Specials

Memory To Go Special: MacPro 8 Core Memory 4GB kit $192 / 2GB kit $109. 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: LA Computer Company: Specials on AppleCare, Apple Displays, MacBooks, iMac's, MacBook Pros, Laptop and iPod accessories and more. Apple A/C Adapters for laptops starting at $25.00 Call 1-800-941-7654 or Click Here.

Other World Computing: Performance SATA Drives up to 1.0TB from $54.99 Replace, Upgrade, Add SATA to Mac Pro, PowerMac G5, iMac G5 & even PowerMac G4(with PCI SATA Card) Specials: 400GB from $99; 500GB 7200RPM from $99; 750GB & 1.0TB In Stock now!

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.

Memory For New Intel Core2 DUO MacBooks, MacBook Pro, MacMini & iMacs" 4GB Kit $80, 3GB Kit $60, 2GB Kit $40, 1GB $20. Click to Maximize your Macs...

Dan Knight - 2002.05.10

This morning I discovered a problem with the PHP scripts we use to display the Mac of the Day and Email List of the Day.

My #2 son Brian <http://brkn.net/> and I came up with the following code to display the Mac of the Day:

<?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 "<B><BR>Mac of the Day</B><BR>&nbsp;<A HREF=/$query[link]>$query[model]</A>";
$lod = $today % 38;
$query = mysql_fetch_array(mysql_query("select * from lists where id = '$lod'"));
echo "<B><BR>List of the Day</B><BR>&nbsp;<A HREF=\"$query[link]\">$query[listname]</A><BR>";
?>

Notice the line near the middle - $mod = $today % 89;

That line tells PHP to use modulo arithmetic to divide today's date (converted to a number) by 89 and discard everything but the remainder. This would give us 89 unique values - just what we need with 89 entries in the Mac of the Day database.

The problem is, modulo arithmetic returns a remainder from 0 (zero) to one less than your maximum number, so our function was using numbers from 0 to 88. And our database uses numbers from 1 to 89.

We use a similar function to handle the 38 email lists, and that's where I noticed the problem this morning. Today the email list script was returning zero, so no list was displayed.

All we have to do is add one to the number returned to have values in the range we need. I've modified two lines, uploaded the new script, and now everything works:

$mod = ($today % 89) + 1;

$lod = ($today % 38) + 1;

The Next Project

I'd planned on automating site links next, but have instead decided that the next project will be automating the Deal of the Day. This will involve real calendar dates, not raw numbers based on seconds since 1970.01.01.

I hope to cover this project next week.  LEM

Recent Online Tech Journal Columns

Recent Content on Low End Mac

Go to the Online Tech Journal index.

About LEM | Support | Usage | Privacy | Contacts


Have a question?
Ask an expert!

Low End Living

Amazon.com

Navigation

Used Mac Dealers
Apple History
Best Used Macs
Video Cards
Email Lists
InfoMac's Low
End Mac Forum

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
MacMall
TechRestore
MacResQ
ExperCom
Crucial Memory
batteries.com

Advertise

Open Link