Crazy Horse
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.
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.
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";
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree with Ray that an array would be a better choice.
ASKER
Thank you all. Turns out I don't need to do this anymore (thankfully).