Create Member Directory with Alphabetical Listings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

devnethead
Forum Newbie
Posts: 1
Joined: Sat Mar 10, 2007 11:20 pm

Post by devnethead »

nice solution feyd. i found the foreach structure a little difficult to work with for what i wanted to do. so i used a letter array instead. here's my implementation. $row_WADAtblMembers is the MySql result set.

key points: the first part of feyd's code is before the loop, the second part in the loop and the third part after the (result set) loop --- we set up another loop to cycle through the arrays and spit out the output.

Code: Select all

$letter_headings = array(	
					0=>"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 
					   "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");


	// Create a new array with a key for every letter and the elements are arrays.
	// Query for all members ordering by last name.
	// Analyze the last name of each member, tossing them into the proper letter bucket.
					                       
	// Create Array of letter buckets:
	$buckets = array();
	$A = ord('A');
	for($i = 0; $i < 26; $i++) {
  		$buckets[chr($A + $i)] = array();
	}

	// Extract member detail and sort into appropriate bucket
	do {  // result set loop
		$bucket = strtoupper(substr($row_WADAtblMembers['strLastName'],0,1));
		$buckets[$bucket][] = $row_WADAtblMembers;
	} while ($row_WADAtblMembers = mysql_fetch_assoc($WADAtblMembers)); 
	
	// Now, start a loop through the letter buckets.
	// Output the letter header.
		
	// Bucket output loop
	for($j = 0; $j < 26; $j++) {
		echo "<tr><td>\n";
		echo "<p align=\"left\"><strong>[ $letter_headings[$j] ]</strong></p>";
	
		for($ac=0; $ac<count($buckets[$letter_headings[$j]]); $ac++) {
			echo "<tr><td><p><strong>" . $buckets[$letter_headings[$j]][$ac]['strFirstName'] . " " . $buckets[$letter_headings[$j]][$ac]['strLastName'] . "</strong><br />\n";
			echo $buckets[$letter_headings[$j]][$ac]['strAgencyName'] . "<br />\n";
			echo $buckets[$letter_headings[$j]][$ac]['strAddress']."<br />\n";
			echo $buckets[$letter_headings[$j]][$ac]['strCity'].", ".$buckets[$letter_headings[$j]][$ac]['strState']." ".$buckets[$letter_headings[$j]][$ac]['strZipCode']."<br />\n";
			echo "Phone: ".$buckets[$letter_headings[$j]][$ac]['strPhone']." Fax: ".$buckets[$letter_headings[$j]][$ac]['strFax']."<br />\n";
			if($buckets[$letter_headings[$j]][$ac]['strEmail'])
				echo "Email: <a href=\"mailto:".$buckets[$letter_headings[$j]][$ac]['strEmail']."\">".$buckets[$letter_headings[$j]][$ac]['strEmail']."</a><br />\n";
			if($buckets[$letter_headings[$j]][$ac]['strWebSite']) 
				echo "Website: <a href=\"http://www".$buckets[$letter_headings[$j]][$ac]['strWebSite']."\">".$buckets[$letter_headings[$j]][$ac]['strWebSite']."</a></p>\n";
			echo "<p><u>Category(ies)</u><br />\n";
		}
		echo "</td></tr>\n";
	}
Post Reply