Hey guys

i jst wanna ask about how to put categories in php in such a that i write a function that will take a specific surname as a parameter and check in which category of letters it falls to and return the name of the person

e.g muller
category: M-o

Recommended Answers

All 2 Replies

function get_category($name) {
	// get the first letter of the name passed in.
	$first_letter = substr($name,0,1);
	// normalize it, in this case lets make them all lowercase.
	$lowercase = strtolower($first_letter);
	// I was going to make an array of categories, but this will do the job...
	$category = '';
	if ($lowercase == 'a' || $lowercase == 'b' || lowercase == 'c') {
		$category = "A-C";
	} else if ($lowercase == 'd' || $lowercase == 'e' || lowercase == 'f') { 
		$category = "D-F";
	} else if ($lowercase == 'g' || $lowercase == 'h' || lowercase == 'i') { 
		$category = "G-I";
	// continue on with your else if statements until all of your categories are defined.
	} else {
		$category = "Undefined";
	}
	
	return $category;
}
// oops...
all of the 'lowercase' should be $lowercase
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.