database select result upper case first letter of name

150 views Asked by At
if ($stmt - > execute()) {
    if ($stmt - > rowCount() > 0) {
        while ($selected_row = $stmt - > fetch(PDO::FETCH_ASSOC)) {
            echo ucwords($selected_row[$name]).
            "   ";
            $basicinfo1[] = $selected_row[$name];
        }
        $input = array_map("unserialize", array_unique(array_map("serialize", $basicinfo1)));
        echo json_encode($input, JSON_UNESCAPED_UNICODE);
        //echo $_GET['callback'] . '('.json_encode($basicinfo1).')';
    }
}

Echo results in

JEHOMAR MARIA JAMAICA MARIO MARIEGRACE MARIO MARY ANNE MARILYN MARIA CHRISTINA MARIE GRACE MARBIE MARIA JASMIN MARY JANE MARCIANA LEO MARCCO MARGOT MARIA CRISTINA MARIA TERESA MARK ANTHONY MARK ANA MARIE RALPH MARLON MARIANNE JOEMAR MARITES SHIELA MARIE MARIBETH MARIVIC MARIAN MARCELINO ALMEN MARTIN MARK SUNNY MARLOU MARY JANE MARIA RIA ELMAR MARILYN MARGARITA MARIELA MARJORIE ANNE MARTIN MARIONITO JHOMAR MARIA LUISA MARIE KRISTINE MARY ANN MARCELO MARGIE MARIO CHARL-MARI GEMARIE MARIO JOEMAR MARIA LENIE MARILOU MARILOU MARJORIE MARIA PAZ JOSE MARIE MARYCRIS MARVIN MARY FRANCESS JOURIEMAR MARIE Rose MARY JANE ROSEMARIE MARILYN MARIE-MONIQUE MARIANETH MARIBEL MARICEL MARILYN MARK ANTHONY MARVIN LOEWEN MARK MARVEL MARIANO MARTE JOVEMAR MARIBETH MARIVER MARTE MARILOU EDDIMAR MARIO MARISSA MARITES MARLYN MARWIN MARIETTA MARY ANN MARIVI MARYJANE ANNA MARIA EMMAR MARIO MARITES MARK VINCENT MARILYN MARIBEL MARICEL MARISSA MARK ANTHONY MARK KNEP MARK REGGIE

In this code i tried to change the first letter of the names by using ucwords then followed by the selected row ($selected_row[$name]) but the echo show no change in the names. Still in capital letters. Are there other ways to change words in first letter upper case

2

There are 2 answers

2
Sougata Bose On BEST ANSWER

Try with -

ucwords(strtolower($selected_row[$name]))

ucwords will transform only the first letter but wont take care of the rest. So first strtolower to transform it lowercase then ucwords to transform the first letter uppercase.

0
Mat On

If you don't work in ascii (and you should), it's useful to use mb_ functions to handle UTF-8 charset. The case will arise when/if you will have foreign names in your db.

Try with

mb_convert_case($selected_row[$name], MB_CASE_TITLE, "UTF-8");

MB_CASE_TITLE put all words in lower case but the first letter in upper case. That's a single call compared to ucwords(strtolower(...)) and it handles UTF-8 characters.


An example to understand differences of the two methods:

$str = "Hello guys! I'm émile ÉÀ!";
echo ucwords(strtolower($str));
echo '<br />';
echo mb_convert_case($str, MB_CASE_TITLE, "UTF-8");

outputs

Hello Guys! I'm émile ÉÀ!

Hello Guys! I'm Émile Éà!

In the first case, strtolower does not transform uppercase accentued letters to lowercase (the last letter is still À instead of à). And uwords does not put in uppercase the é of émile. This is handled correctly in the second case with mb_convert_case.