[php] Exercise with associative arrays

Margaret Iaquinto iaquinto at ozemail.com.au
Mon Apr 22 15:33:41 EST 2013


This is a simple (dare I say simplistic) example of using associative 
arrays.
The file uses HTML5 and CSS3.

Looking for other examples of associative arrays, the foreach loop and 
searching within a foreach loop. Please send suggestions!

Maggie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PHP Meal Cost.docx
Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Size: 220782 bytes
Desc: not available
Url : http://www.edulists.com.au/pipermail/php/attachments/20130422/095102ee/PHPMealCost-0001.docx 
-------------- next part --------------
<?php

function page_header ($title)

{
	echo "<html> \n";
	echo "<head> \n";
	echo "		<title> $title </title>\n";
	echo "		<link rel=\"stylesheet\" href=\"red.css\" type=\"text/css\">";
	echo " </head>";
	echo "<body> \n";
}
function page_footer ($info)


{
	echo "<footer>";
	echo "<a href=\"meal-cost.html\">Another meal?</a>";
	echo "<br />";
	/*echo "<a href=\"mainmenu.html\">main menu</a>"; */
	echo "<hr />";
	echo "&copy; $info 2013 <a href=\"mailto:$info at gmail.com\">Contact Webmaster</a>";
	echo "<hr />";
	echo "</footer>";
	echo "</body> \n </html>\n";
}
	

	
function Lsearch ($needle, $haystack)
{
	/* Get the number of elements in the array */
$size = count($haystack);
	
	/* Set the "not found" flag - we hope to change this */
	$found = false;

	/* We could use a fancy php array trick, but let's keep 
	 this algorithm as "macro" as possible: Use a position marker */
	$position = 0;
	
	/* Repeat as often as required until the target is found or
	 we run out of elements to compare with: */
	
	while (!$found && ($position < $size))
	{
	    /* Use the trim() function to remove blanks and line endings:*/
		if ($needle == trim($haystack[$position]))
		{
			$found = true;
			$message = "$needle found as element number $position";
		}
		/* Check the next element in the list if $found is still false: */
		$position++;
	}
	
	/* We have gone through all the elements and the Needle was not found,
	 so, change the message to show that sad fact: */
	
	if (!$found)
	{	$message = "$needle was not found in this list";
		
	/* All done, so return the appropriate $message string as the output of this function */
	
	
	}
	echo " $message";
}
	 
?>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/php/attachments/20130422/095102ee/meal-cost-0001.html 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/php/attachments/20130422/095102ee/meal-cost-0001.php 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: red.css
Type: text/css
Size: 7683 bytes
Desc: not available
Url : http://www.edulists.com.au/pipermail/php/attachments/20130422/095102ee/red-0001.css 


More information about the php mailing list