Question

I am looking to capitalize every letter of the first word that is presented by MySQL and PHP and keep the rest of the words lowercase. How could that be accomplished?

It is stored in the database with only the first letter capitalized, so the real trick is to keep the rest of the words lowercase.

Here is the code pulling the words from the database as of today:

<?php

$query0  = "SELECT LCASE(ord) FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);

while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
$keyword = $row0['LCASE(ord)'];

echo "$keyword, ";

}
?>
Was it helpful?

Solution 7

The solution ended up being like this:

$query0  = "SELECT LCASE(ord) FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);

while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
$keywords[] = $row0['LCASE(ord)'];
}
echo ucfirst(implode(', ', $keywords));
echo ".";
?>

This presents a line of words that looks like this:

Deltagende, sosial, pragmatisk, kunnskapsrik, løsningsorientert, samarbeidende, engasjert.

Instead of:

deltagende, sosial, pragmatisk, kunnskapsrik, løsningsorientert, samarbeidende, engasjert,

This solution was the result of a modification of the solution to a different question regarding how to remove the last comma that I posted here Remove last comma or prevent it from being printed at all MySQL/PHP

OTHER TIPS

Unless I'm misunderstanding something...

<?
$count = 0;
while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
    if ($count == 0) {
        $keyword = ucfirst($row0['LCASE(ord)']); // this will capitalize just the first letter
        $keyword = strtoupper($row0['LCASE(ord)']); // this will make the first word in all CAPS (choose which one you want)
    }
    else
        $keyword = strtolower($row0['LCASE(ord)']);

    echo "$keyword, ";
    $count++;

}
?>

If I understand you correctly, use the php method ucfirst($string)

I'm confused as to why you need this, but it's better accomplished with PHP:

$query0  = "SELECT ord FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);

while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
$keyword = ucfirst(strtolower($row0['ord']));

echo "$keyword, ";

}

ucfirst: upper-case first letter
strtolower: lower-case entire string

$query0  = "SELECT LCASE(ord) FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);

$i=0;
while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
   if($i == 0){
      $keyword = ucfirst(strtolower($row0['LCASE(ord)']));
      $i++;
   }
   else
      $keyword = strtolower($row0['LCASE(ord)']);

   echo "$keyword, ";

}

If the data is stored in the database with the first letter capitalized (how you want it?), why are you intentionally lowercasing it in your query?

If I'm totally confused, the function you want is ucfirst:

echo ucfirst($keyword);

Edit

$index = strpos($word, ' ');
echo ucfirst(substr($word,0,$index)) . substr($word, $index);

Try aliasing the LCASE(ord) as follows:

<?php

$query0  = "SELECT LCASE(ord) LCaseValue FROM `keywords` ORDER BY RAND()";
$result0 = mysql_query($query0);

while($row0 = mysql_fetch_array($result0, MYSQL_ASSOC))
{
    $keyword = $row0['LCaseValue'];
    echo "$keyword, ";
}
?>

you can use

ucwords(-yourdata-);

this will captialize every word of data .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top