Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Data Translation

Explorer ,
Apr 15, 2008 Apr 15, 2008
In Dreamweaver, I'm using the PHP string

>?php echo (fieldvalue) == 'Y'?'Yes':'No'; ?>

To display the word "Yes" when the value stored in the database is "Y", otherwise, defaults to "No". Can this be done where there are more than just two values? For example, "High", "Medium", "Low". What is the syntax?

I can't find this anywhere in any PHP documentation.

--
TOPICS
Server side applications
371
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 16, 2008 Apr 16, 2008
.oO(DreamerJim)

>In Dreamweaver, I'm using the PHP string
>
> >?php echo (fieldvalue) == 'Y'?'Yes':'No'; ?>
>
> To display the word "Yes" when the value stored in the database is "Y",
>otherwise, defaults to "No". Can this be done where there are more than just
>two values? For example, "High", "Medium", "Low". What is the syntax?

There are various ways. The easiest would be probably to use either a
switch statement or a simple lookup table (array). Of course you could
also nest two or more ternary operators, but this will make the code
harder to read.

Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 16, 2008 Apr 16, 2008
Another way would be a another table. I use this often to link lookup
tables (dropdowns) descriptive data to a major table

tbl2
fld1 Char(1)
fld2 char(xx)

fld1 fld2
y Yes
n No
e Either
i Neither
d Dont Know


select tbl1.name, tbl2.fld2
from tbl1, tbl2
where tbl1.ynfld = tbl2.fld2

Sometimes this can work very well and other times it is does not.

Remember to index

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 16, 2008 Apr 16, 2008
Like this -

<?php echo (fieldvalue) == 'Y'?'Yes':(fieldvalue) == 'M'?'Maybe':'No'; ?>


--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Michael Fesser" <netizen@gmx.de> wrote in message
news:iikb0415u83pavgfpbgibslp8q30vc6eub@4ax.com...
> .oO(DreamerJim)
>
>>In Dreamweaver, I'm using the PHP string
>>
>> >?php echo (fieldvalue) == 'Y'?'Yes':'No'; ?>
>>
>> To display the word "Yes" when the value stored in the database is "Y",
>>otherwise, defaults to "No". Can this be done where there are more than
>>just
>>two values? For example, "High", "Medium", "Low". What is the syntax?
>
> There are various ways. The easiest would be probably to use either a
> switch statement or a simple lookup table (array). Of course you could
> also nest two or more ternary operators, but this will make the code
> harder to read.
>
> Micha

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 17, 2008 Apr 17, 2008
.oO(Murray *ACE*)

>Like this -
>
><?php echo (fieldvalue) == 'Y'?'Yes':(fieldvalue) == 'M'?'Maybe':'No'; ?>

Without parentheses this won't work as expected, because in PHP the
ternary operator is left-associative (in many other languages it is
right-associative, so it would work there). The result will always
be "Maybe" or "No", but never "Yes", because the result of the first
ternary operator is seen as an operand of the second operator, which
will return the result string.

In a more formal way an expression like above without any parentheses:

R = A ? B : C ? D : E

in PHP will be evaluated as

R = (A ? B : C) ? D : E

instead of the intended

R = A ? B : (C ? D : E)

So you should either wrap the second operator in parentheses or negate
the first condition and change the operands of the first operator like
this:

R = !A ? C ? D : E : B

This kind of nesting works. The same in a slightly more readable form:

R = !A
? C ? D : E
: B

or even like this, if the expressions are a bit longer:

R = !A
? C
? D
: E
: B

Applied to the original expression this will work:

$result = $fieldvalue != 'Y'
? $fieldvalue == 'M'
? 'Maybe'
: 'No'
: 'Yes';

Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 17, 2008 Apr 17, 2008
Awesome! Thank you so much, Micha!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
==================


"Michael Fesser" <netizen@gmx.de> wrote in message
news:7s1f04diu6r7j3rko432b27k6cfvpdn127@4ax.com...
> .oO(Murray *ACE*)
>
>>Like this -
>>
>><?php echo (fieldvalue) == 'Y'?'Yes':(fieldvalue) == 'M'?'Maybe':'No'; ?>
>
> Without parentheses this won't work as expected, because in PHP the
> ternary operator is left-associative (in many other languages it is
> right-associative, so it would work there). The result will always
> be "Maybe" or "No", but never "Yes", because the result of the first
> ternary operator is seen as an operand of the second operator, which
> will return the result string.
>
> In a more formal way an expression like above without any parentheses:
>
> R = A ? B : C ? D : E
>
> in PHP will be evaluated as
>
> R = (A ? B : C) ? D : E
>
> instead of the intended
>
> R = A ? B : (C ? D : E)
>
> So you should either wrap the second operator in parentheses or negate
> the first condition and change the operands of the first operator like
> this:
>
> R = !A ? C ? D : E : B
>
> This kind of nesting works. The same in a slightly more readable form:
>
> R = !A
> ? C ? D : E
> : B
>
> or even like this, if the expressions are a bit longer:
>
> R = !A
> ? C
> ? D
> : E
> : B
>
> Applied to the original expression this will work:
>
> $result = $fieldvalue != 'Y'
> ? $fieldvalue == 'M'
> ? 'Maybe'
> : 'No'
> : 'Yes';
>
> Micha

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 17, 2008 Apr 17, 2008
LATEST
.oO(Murray *ACE*)

>Awesome! Thank you so much, Micha!

You're welcome.

Micha
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines