Click to See Complete Forum and Search --> : Display 1st Character of Field
Dysan
11-09-2007, 05:31 PM
I have a "Sex" field containing peoples sex/gender. This is stored in the database using the following format:
Male
Female
How do I display just the 1st character of this field, instead of the whole word?
NogDog
11-09-2007, 05:59 PM
You can use substr() as you [almost] did in this thread (http://www.webdeveloper.com/forum/showthread.php?t=165662), or, since you just want one character, you can refer to that character position directly with:
$string = "Abcdefg";
$firstCharacter = $string[0];
bokeh
11-10-2007, 05:46 AM
Do it in your MySQL query, not with PHP. SELECT SUBSTRING (http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring)(`sex`, 1, 1) as gender FROM tablename