ucwords() problem

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

Post Reply
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

ucwords() problem

Post by qads »

Sami | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

sorry to put all this code here but i want to show what i am trying to do.

Code: Select all

<?
$t = mysql_query($query);

$name = ucwords($name);

$count = mysql_num_rows($t);

while(list($name, $bmp, $source) = mysql_fetch_row($t))

echo("<table width='490' border='0' height='107' align='center' ' bgcolor='$tone_table_bg'>
  <tr>


    <td height='19' width='436' bgcolor='$tone_name_bg'>
      <p align='center'><b><font color='$tone_name'>$name - Bmp($bmp)</font></b>
    </td>
          </tr>
          <tr>

    <td height='76' width='436' valign='top' align='left' bgcolor='$source_bg'><font color=$source_font>$source</font></td>
          </tr>
        </table>");
}?>
this works fine, but i have some records in the database which are ALL CAP and some are MIxed :roll:

i want to use ucwords() to make all of seem like this: This That Or, so the firt letter of each word is a CAP.

so far this is not working.

any ideas on how or why it is not working?


thanks in adv.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

its because you're applying the ucwords function before it has any content. try placing that line after the while statement and it should work.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

i did that before, but it only shows 1 record.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You probably want to switch things around so you get

Code: Select all

<?php
$t = mysql_query($query); 

$count = mysql_num_rows($t); 

while(list($name, $bmp, $source) = mysql_fetch_row($t))
{
    $name = ucwords(strtolower($name)); 

    echo <<<END
<table width="490" border="0" height="107" align="center" bgcolor="$tone_table_bg"> 
<tr> 
<td height="19" width="436" bgcolor="$tone_name_bg"> 
<p align="center"><b><font color="$tone_name">$name - Bmp($bmp)</font></b> 
</td> 
</tr> 
<tr> 
<td height="76" width="436" valign="top" align="left" bgcolor="$source_bg"><font color=$source_font>$source</font></td> 
</tr> 
</table>
END;
}

?>
The strlower() function will convert the string to lower case so that all-caps and mixed case is not an issue and then ucwords() should work better.

You need an opening brace after your while loop ({).

The echo <<<END ... END; statement means that your HTML formatting will be preserved and you don't have to worry about escaping quotes or other special characters. IIRC single quotes are not valid in HTML.

Hope this helps,
Mac
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

thanks alot, that helped.
Post Reply