Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

Append letter to end of string

Currently I have a text field that I can input some text into but if the same thing is entered twice I want to append an "a", and then a "b" etc.

So, this is taking a row from the database and then if the submitted value is equal to the database value, it suggests adding the a. This works okay, but without writing 100 switch statements, for if it's a, then use b, if it's b use c, and then when it gets to z it would probably have to become aa, bb and so on.

$prefix = $row['prefix'];
			
			if($prefix == $_POST['prefix']) {
		
		echo "Try this instead!" . $_POST['prefix']."a";
		
		} else {
				
				echo "you can use that prefix";
		}

Open in new window


Is there an elegant way of doing this or a better method? I thought of perhaps a random string but I don't want to make it long, it should really just be an extra letter or two which won't get you much randomness.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree with Ray that an array would be a better choice.
Avatar of Crazy Horse

ASKER

Thank you all. Turns out I don't need to do this anymore (thankfully).