[Phpsig] First PHP script

Paul VandenBosch phpsig@kalamazoolinux.org
Wed, 10 Apr 2002 23:07:08 -0400


I'm wondering if anyone would take a look at this script and give me some feedback on whether it is done right (syntax, style, logic, etc.).

It is supposed to present four images of book covers, which are selected at random, but the selection is weighted by the dollar value (actually cent value) of book sales last quarter from amazon.

I know that this would work better with a database than an array but I haven't got that far into the book yet, this is just my first try at it.

Unfortunately it is not working right, there are two intermittent problems:
1. Sometimes only three or two images show up, and the document source shows that the url's and image paths of the missing images were parsed with no information for some reason.
2. I am still getting double images sometimes, even though I thought I wrote a few lines to detect duplicates.

You can try it out at http://cruisenews.net/booklist.php


Here's the script:
__________________

<?php

#
#	Two dimensional array: amount of sales in cents, name of the book with author, url to amazon, path to image
#

$booklist = array(array(4892, "The Voyager's Handbook: The Essential Guide to Blue Water Cruising, Beth Leonard", "http://www.amazon.com/exec/obidos/ASIN/0070381437/ref=nosim/guidetosailingan", "/books/VoyagersHandbook.gif"),
	array(10488, "World Cruising Routes, Jimmy Cornell", "http://www.amazon.com/exec/obidos/ASIN/0070134065/ref=nosim/guidetosailingan", "/books/WorldCR.gif"),
	array(3496, "Nigel Calder's Cruising Handbook, A Compendium for Coastal and Offshore Sailors, Nigel Calder", "http://www.amazon.com/exec/obidos/ASIN/0071350993/ref=nosim/guidetosailingan", "/books/NCCH.jpg"),
	array(3212, "The Cruising Life: A Commonsense Guide for the Would-Be Voyager, Jim Trefethen", "http://www.amazon.com/exec/obidos/ASIN/0070653607/ref=nosim/guidetosailingan", "/books/cruisinglife.gif"),
	array(6585, "All About Cruising: Prepare Yourself - Equip Your Boat - Plan Your Escape - Live Your Dream, Walt Gleckler", "http://www.amazon.com/exec/obidos/ASIN/0966141636/ref=nosim/guidetosailingan", "/books/allabtc.jpg"),
	array(3496, "Boatowner's Mechanical and Electrical Manual: How to Maintain, Repair, and Improve your Boat's Essential Systems, Nigel Calder", "http://www.amazon.com/exec/obidos/ASIN/007009618X/ref=nosim/guidetosailingan", "/books/bmanual.gif"),
	array(10000, "Lin and Larry Pardey, Storm Tactics Handbook, Modern Methods of Heaving-To for Survival in Extreme Conditions", "http://www.amazon.com/exec/obidos/ASIN/0964603667/guidetosailingan/", "/books/stormtactics.gif"),
	array(10000, "Diana Jessie, The Cruising Womans Advisor for the Voyaging Life", "http://www.amazon.com/exec/obidos/ASIN/0070319812/guidetosailingan/", "/books/CruisingWoman.gif"),
	array(10000, "Beth Leonard, Following Seas, Sailing the Globe, Sounding a Life", "http://www.amazon.com/exec/obidos/ASIN/1559493704/guidetosailingan/", "/books/follseas.jpg"),
	array(10000, "Lin and Larry Pardey, Cruising in Seraffyn", "http://www.amazon.com/exec/obidos/ASIN/0924486368/guidetosailingan/", "/books/CruisinginSeraffyn.gif"),
	array(10000, "Capable Cruiser, Lin and Larry Pardey", "http://www.amazon.com/exec/obidos/ASIN/0964603624/guidetosailingan/", "/books/CapableCruiser.gif"),
	array(10000, "There Be No Dragons, Reese Palley", "http://www.amazon.com/exec/obidos/ASIN/1574090100/guidetosailingan/", "/books/TherebenoDragons.gif"),
	array(10000, "Lin and Larry Pardey, Self-Sufficient Sailor", "http://www.amazon.com/exec/obidos/ASIN/0964603675/guidetosailingan/", "/books/SelfSufficientSailor.gif"),
	array(10000, "Lin and Larry Pardey, Cost Conscious Cruiser", "http://www.amazon.com/exec/obidos/ASIN/0964603659/guidetosailingan/", "/books/Costconscious.gif"),
	array(10000, "Anne Hammick, Ocean Cruising on a Budget", "http://www.amazon.com/exec/obidos/ASIN/0071580123/guidetosailingan/", "/books/Oceanconbudget.gif"),
	array(10000, "George Buehler, The Troller Yacht Book, A Powerboaters Guide to Crossing Oceans", "http://www.amazon.com/exec/obidos/ASIN/0393047091/guidetosailingan/", "/books/troller.gif"),
	array(10000, "The Atlantic Crossing Guide, Anne Hammick", "http://www.amazon.com/exec/obidos/ASIN/007026032X/o/guidetosailingan/", "/books/AtlanticC.gif"),
	array(10000, "Lin Pardey, Care and Feeding of a Sailing Crew", "http://www.amazon.com/exec/obidos/ASIN/0964603608/guidetosailingan/", "/books/carefeed.jpg"),
	array(10000, "Chapman Piloting, Elbert Maloney, Charles Chapman", "http://www.amazon.com/exec/obidos/ASIN/0688168906/guidetosailingan/", "/books/Chapman.gif"),
	array(10000, "Don Casey, This Old Boat", "http://www.amazon.com/exec/obidos/ASIN/0071579931/guidetosailingan/", "/books/ThisOldBoat.gif"),
	);


#
#	Calculate total sales amount in cents with a loop which increments until $booklist[x] is null.
#	Adds up $booklist[0-x][0] to get $totalsales in cents.
#

$totalsales = 0;
$counter = 0;

while (isset($booklist[$counter]))
{
	$totalsales = $booklist[$counter][0] + $totalsales;
	$counter++;
};

echo "<html>";
#echo $totalsales;
#echo"<p>";

#
#	The main loop starts here, four blips means four images
#

for($blip=0; $blip<4; $blip++)
{

#
#	Random generation of a number between 0 and the value of $totalsales.
#	Then a fractional value is set between 0 and 1 as a ratio of the random number to $totalsales.
#

	$seed = (float) microtime( ) * 100000000;
	srand($seed);
	$chance = rand(0,$totalsales);
	$fraction = ($chance/$totalsales);

	#echo "<p>";
	#echo $chance;
	#echo "<br />";
	#echo $fraction;
	#echo "<p>";

#
#	The next loop goes through each line in the array, adds the value to $salesincrement, and tests to see
#	if the fraction $salesincrement/$totalsales is larger than the fraction calculated from the random number.
#	As it iterates, in increases the index, $salesindex.
#	When the script escapes this loop, it will have selected the index number of the image to print.
#

	$salesratio = 0;
	$salesindex = 0;
	$salesincrement = 0;

	while ($salesratio < $fraction)
	{
		$salesincrement = $salesincrement + $booklist[$salesindex][0];
		$salesratio = ($salesincrement/$totalsales);
		$salesindex++;
	}

	#	echo $salesratio;
	#	echo "<br />";

#
#	This script is supposed to test if the image has already been printed earlier in the loop.
#	Unfortunately, it does not seem to work.
#

	if ($printest[0]==$salesindex)
		continue;
	if ($printest[1]==$salesindex)
		continue;
	if ($printest[2]==$salesindex)
		continue;
	if ($printest[3]==$salesindex)
		continue;

#
#	The image is printed out as html, using the salesindex to call the info on each image from the array.
#

	echo '<a href="';
	echo $booklist[$salesindex][2];
	echo '"><img src="';
	echo $booklist[$salesindex][3];
	echo '" border=0 alt="';
	echo $booklist[$salesindex][1];
	echo '"></a>';

	$printtest[$blip]=$salesindex;

}
?>