How do I add one field to the end of another field, if one value is true?
I have two fields in one table.
One being named Junior with two outputs of either yes or no. The other being named Result with a string as a sentence saying..Here is the result
If Junior is equal to Yes then I want to add '(Jr.)' to the end of the string..saying 'Here is the result (Jr.)'
How would I accomplish this?
$bio['results'] is my array for my query.
if ($bio['results']['Junior'] == 'Yes' {
what would go here?
});
EDIT: I want concatenate ('Jr') to the end of my string 'Here is the result' resulting in 'H开发者_Python百科ere is the result (Jr.)' Sorry for the typo.
I would appreciate any help! Thanks!
I am quite confident, I can guess what you want. :)
if ($bio['results']['Junior'] == 'Yes') {
$bio['results']['Result'] = $bio['results']['Result'].' (Jr.)';
};
If you want to write the result string back into the database, you will need sql.
For example (because you were asking):
query_function("UPDATE tablename SET Result = '".escaping_function($result)."' WHERE id = ".intval($id).";");
assuming, you wrote the string into the variable $result (it's cleaner).
Since I don't know your database system or table name, I used aliases for the functions.
It's difficult to give you an example since you're question doesn't state the variable name (or path in your array) of the string you want "(Jr.)" added to. With that said, you should look at PHP's String Operators page.
You can use a dot (.) to append multiple string together.
For example:
// Concatenating an array value (string) with another string.
echo $bio['results']['name'] . ' (Jr.)';
// Concatenate 2 string
echo 'Here is the result' . ' (Jr.)';
The same works for setting variables:
// The long way
$bio['results']['name'] = $bio['results']['name'] . ' (Jr.)';
// The short way
bio['results']['name'] .= ' (Jr.)';
Based on your edit
echo 'Here is the result' . ' (Jr.)';
// If "Here is the result" is stored in a variable (for instance $result):
$result .= ' (Jr.)';
if ($bio['results']['Junior'] == 'Yes') {
$bio['results']['Result'] .= "Yes";
}
Something like this?
Alternatively, depending on if you want result to read "This is the result No", or "This is the result Yes", you could omit the conditional and just do
$bio['results']['Result'] .= $bio['results']['Junior'];
This of course assumes the only two possible values of Junior are Yes and No.
精彩评论