Jump to content
  • Checkout
  • Login
  • Get in touch


The e-commerce.

Case problems


Recommended Posts

My customers have an annoying habit of entering their details as either lower case or upper case rather than sentence case so I would like to be able to convert all the name and address fields to sentence case (capitalise first letter of each word) and post code to upper case when they enter the details and therefore ensure it goes into the database in the format I would like for printing labels etc and I also need to be able to do this for all my existing customers which are already set up.


I had a look through PHP.net and seached here and found references to some functions like UPPER, strtoupper and ucwords but I cant get them to work for new entries let alone even knowing where to begin to convert all the existing records. Also Im not sure which other files other than create_account.php I should be adding them to etc


Can anyone help / point me in the right direction??





Link to comment
Share on other sites

Look for the following in create_account.php:

$sql_data_array = array('customers_firstname' => $firstname,

'customers_lastname' => $lastname,

'customers_email_address' => $email_address,

'customers_telephone' => $telephone,

'customers_fax' => $fax,

'customers_newsletter' => $newsletter,

'customers_password' => tep_encrypt_password($password));


$sql_data_array = array('customers_id' => $customer_id,

'entry_firstname' => $firstname,

'entry_lastname' => $lastname,

'entry_street_address' => $street_address,

'entry_postcode' => $postcode,

'entry_city' => $city,

'entry_country_id' => $country);


And any other fields (state is in there somewhere)


Just wrap anything you want converting in ucwords(strtolower())


Do the same in the files where they edit the name/address (address_book_process?)



I don't think there an equivalent of ucwords in SQL, so you'd have to write a code loop to read all the records, explode each column value into an array and apply the technique above on each, then write back to database.

Link to comment
Share on other sites

I would just leave it alone but thinking back I cant count how many databases I have done this on. I think most people know that names should be capitalized, especialy their own name, but for some reason they dont apply that knowledge to the computer.



My Name Is: lazy bum


$firstname = tep_db_prepare_input($HTTP_POST_VARS['firstname']);

$firstname = ucwords(strtolower($firstname));


I dont think there is any mysql function to do this so you would have to write a script to update your db. You could also change your label printing code like above.




Link to comment
Share on other sites

You can use sql to achieve this affect if the column contains only 1 word - firstname, lastname etc - but it won't work on addresses - eg, it will convert

"MYFIRSTNAME" to "Myfirstname", but not "ADDR1 ADD2 ADD3" to "Addr1 Addr2 Addr3"



UPDATE customers SET





change customers_firstname to whatever column you want.

Link to comment
Share on other sites

Try a script instead...


 $sql = "select customers_id, customers_firstname, customers_lastname from " . TABLE_CUSTOMERS;
 $query = tep_db_query($sql);
 while ($customers = tep_db_fetch_array($query)) {
   $firstname = ucwords(strtolower($customers['customers_firstname']));
   $lastname = ucwords(strtolower($customers['customers_lastname']));
   $sql = "update " . TABLE_CUSTOMERS . " set customers_firstname='" . $firstname . "', customers_lastname='" . $lastname . "' where customers_id='" . $customers['customers_id'] . "'";
 $sql = "select address_book_id, entry_firstname, entry_lastname, entry_street_address, entry_suburb, entry_city from " . TABLE_ADDRESS_BOOK;
 $query = tep_db_query($sql);
 while ($address = tep_db_fetch_array($query)) {
   $frstname = ucwords(strtolower($address['entry_firstname']));
   $lastname = ucwords(strtolower($address['entry_lastname']));
   $street = ucwords(strtolower($address['entry_street_address']));
   $suburb = ucwords(strtolower($address['entry_suburb']));
   $city = ucwords(strtolower($address['entry_city']));
   $sql = "update " . TABLE_ADDRESS_BOOK . " set entry_firstname='" . $firstname . "', entry_lastname='" . $lastname . "', entry_street_address='" . $street . "', entry_suburb='" . $suburb . "', entry_city='" . $city . "' where address_book_id='" . $address['address_book_id'] . "'";
   echo $sql;

Link to comment
Share on other sites

Dave's code is preferable...real nice.


However, I prefer to pull the data into an array and loop that instead of looping the SQL query array.


Also, you should free the resource after you're done with the query. This helps keep the amount of server resources to a minimum.


Either way Dave's code will do what you want...be sure to thank him for his time.



Link to comment
Share on other sites

Thanks to everyone who replied - in the end I decided not to modify anything in the database but instead I used ucwords(strtoupper()) in the labelling contribution and also in packingslip.php and invoice.php so irrespective of how the customer enters their address it is printed in upper case.



Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...