<html><head></head><title></title>
<body onload="document.Form1.n.focus()">

<!-- Solution of Assignment 4; CSIT 207 (Spring 07) -->

<?
echo "<form name='Form1' action=\"{$_SERVER['PHP_SELF']}\" method='post'>\n";
?>
<center>
<b>Enter the number of names to sort (must be between 1 and 20): </b>
<input name="n" type="text" size="3">
<br />
<p>The names will be entered in the format: first middle last, and sorted with respect 
to the format: last first middle.
</p>
<input type="hidden" name="submitted1" value="true">
<input type="submit" value="SUBMIT"> <input type="reset" value="RESET">
</center>
</form>

<?

$number_of_names = 0;

function sort_names($a,&$b) // Note that $b is a reference parameter
{
        foreach ($a as $name)
        {
                // Reverse name to get the last name using split
                $name = strrev($name);
                $z = split(" ", $name, 2);
                // Get name in the format: last first middle
                $name = strrev($z[0]). " ". strrev($z[1]);
		/* You could use in the above line implode instead of concatenation */
                $b[] = $name;
                asort($b);
        }
}

function print_names($a,$b)
{
         // Print the array
	echo "<center>The names were entered in the format: first middle last.<br />\n";
        echo "<b>Here are the sorted (with respect to the format: last first middle) names. 
        </b><br>\n";
        echo "<table width='40%' border='1'>\n<tr><td><b>Name</b></td></tr>\n";
        foreach ($b as $k => $v)
                echo "<tr><td>$a[$k]</td></tr>\n";
        echo "</table></center>\n";
}

if (isset($_POST['submitted1']))
{
	$number_of_names = $_POST['n'];
	if ($number_of_names >= 1 && $number_of_names <= 20)
	{
		echo "<center><form name='Form2' action=\"{$_SERVER['PHP_SELF']}\" method='post'>";
		for ($i = 1; $i <= $number_of_names; $i++)
		{
			echo "<b>Enter a name in the format: first middle last: </b>\n";
			echo "<input name=\"$i\" type='text' size='30' value=\"$_POST[$i]\"><br />\n";
			
			/* Note that you could also use, for example,  name="Name[]"
			If you do that, there will be no need to pass the number of names
			as a hidden field in the second form.
			 */
		}
echo <<< HERE
		<input type="hidden" name="submitted2" value="true">
		<input type="hidden" name="n" value="$number_of_names">
		<input type="submit" value="SUBMIT"> <input type="reset" value="RESET">
		</form></center>
		<hr/>
HERE;
	}
	else
		echo "The number has to be between 1 and 20.<br>\n";
}

if (isset($_POST['submitted2']))
{
	$number_of_names = $_POST['n'];

        // Array a will contain names as entered but with with
	// leading and trailing space, and slashes removed.
        $a = array();

	// Array b will contain names in the format:
	// last first middle
       	$b = array();

	/*
	If you use in the second form name="Name[]" instead of 
	name = "$i", then you need to replace the 
	header of the for loop below and the first line in the loop by
	foreach ($_POST['Name'] as $v)
	{
		$name =  trim(stripslashes($v));
	*/

	// Get the names
	for ($i = 1; $i <= $number_of_names; $i++)
        {
                $name = trim(stripslashes($_POST[$i]));
	        if (!empty($name))
		{
			$name = ucwords(strtolower($name));
        		$a[] = $name;
		}
        }

	sort_names($a,$b);
        print_names($a,$b);
}

?>
</body>
</html>
