From: Olavi Ivask on
Don't you like my solution?

Regards,
Olavi

On Jan 25, 2010, at 9:39 PM, Karl DeSaulniers wrote:

> Hi,
> $req_user_level is a MySql return result and I am trying to utilize
> shortened code to evaluate whither $req_user_level == 0 || 1 || 2 ||
> 3 || 4 || 5 || 6 || 7 || 8 || 9, if it matches a number, return the
> text associated with that number.
> "Guest" || "Regular User" || "Intl. User" || "Contractor" ||
> "Employee" || "Sales" || "Investor" || "Human Resources" ||
> "Administrator";
>
> Karl
>
>
> On Jan 25, 2010, at 1:31 PM, Olavi Ivask wrote:
>
>> Hi,
>>
>> did you mean something like this?
>>
>> $level_names = array("Guest", "Regular User", "Intl. User",
>> "Contractor", "Employee",
>> "Sales", "Investor", "Human Resources", "Administrator");
>>
>> $user_level = $level_names[$req_user_level];
>>
>> Regards,
>> Olavi Ivask
>>
>>
>> On Jan 25, 2010, at 9:13 PM, Karl DeSaulniers wrote:
>>
>>> Hello List,
>>> Trying to learn the right way to code this line.
>>> Can anyone tell me if I am doing this the right way?
>>>
>>> if $req_user_level == 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 ||
>>> 9 ? "Guest" || "Regular User" || "Intl. User" || "Contractor" ||
>>> "Employee" || "Sales" || "Investor" || "Human Resources" ||
>>> "Administrator";
>>>
>>> I am wanting to stray from the if(foo == bar) { routine. ;)
>>>
>>> Thanks,
>>>
>>> Karl DeSaulniers
>>> Design Drumm
>>> http://designdrumm.com
>>>
>>
>
> Karl DeSaulniers
> Design Drumm
> http://designdrumm.com
>

From: Karl DeSaulniers on
Thank you for this as well.
Question? What part is "in_array" playing?
Is it comparing $req_user_level to array()?
Because the text "Guest", etc.. is not in $req_user_level on the
database.
In other words, is it checking the value of $req_user_level to see if
"Guest" is in it?

Karl

On Jan 25, 2010, at 1:32 PM, Peter Beckman wrote:

> On Mon, 25 Jan 2010, Karl DeSaulniers wrote:
>
>> Hello List,
>> Trying to learn the right way to code this line.
>> Can anyone tell me if I am doing this the right way?
>>
>> if $req_user_level == 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 ||
>> 9 ? "Guest" || "Regular User" || "Intl. User" || "Contractor" ||
>> "Employee" || "Sales" || "Investor" || "Human Resources" ||
>> "Administrator";
>>
>> I am wanting to stray from the if(foo == bar) { routine. ;)
>
> if (in_array($req_user_level, array
> (0,1,2,3,4,5,6,7,8,9,"Guest",...))) {
> // do stuff
> }
>
> ----------------------------------------------------------------------
> -----
> Peter Beckman
> Internet Guy
> beckman(a)angryox.com http://
> www.angryox.com/
> ----------------------------------------------------------------------
> -----
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Karl DeSaulniers
Design Drumm
http://designdrumm.com

From: Peter Beckman on
On Mon, 25 Jan 2010, Karl DeSaulniers wrote:

> Thank you for this as well.
> Question? What part is "in_array" playing?
> Is it comparing $req_user_level to array()?
> Because the text "Guest", etc.. is not in $req_user_level on the database.
> In other words, is it checking the value of $req_user_level to see if "Guest"
> is in it?

Sorry, I missed the question mark. in_array isn't appropriate here. The
previous poster has it right, assuming $req_user_level is an integer of
0..9.

$levels = array("Guest", "Regular User", 'Intl. User', ...)
// the array is 0 , 1 , 2 , ...)

Going a little further:

if (!empty($levels[$req_user_level])) { // is both set and doesn't evaluate to false
echo "The user is a {$levels[$req_user_level]}.\n";
} else {
// The $req_user_level was not a valid level.
echo "The returned req_user_level was not valid.\n";
}

Which would output, if $req_user_level was 1 (one):

The user is a Regular User.

Then you know you have a valid user level. Careful though -- sometimes 0
will be returned on a failure, depending on your SQL.

Beckman
---------------------------------------------------------------------------
Peter Beckman Internet Guy
beckman(a)angryox.com http://www.angryox.com/
---------------------------------------------------------------------------
From: Karl DeSaulniers on
Yes, $req_user_level is an int between 0 and 9.

Yes, the other code worked great.
Thank you for your response though.
That is definitely a good way to cross check.
Thanks again for your responses.
Best,

Karl


On Jan 25, 2010, at 2:24 PM, Peter Beckman wrote:

> On Mon, 25 Jan 2010, Karl DeSaulniers wrote:
>
>> Thank you for this as well.
>> Question? What part is "in_array" playing?
>> Is it comparing $req_user_level to array()?
>> Because the text "Guest", etc.. is not in $req_user_level on the
>> database.
>> In other words, is it checking the value of $req_user_level to see
>> if "Guest" is in it?
>
> Sorry, I missed the question mark. in_array isn't appropriate
> here. The
> previous poster has it right, assuming $req_user_level is an
> integer of
> 0..9.
>
> $levels = array("Guest", "Regular User", 'Intl. User', ...)
> // the array is 0 , 1 , 2 , ...)
>
> Going a little further:
>
> if (!empty($levels[$req_user_level])) { // is both set and
> doesn't evaluate to false
> echo "The user is a {$levels[$req_user_level]}.\n";
> } else {
> // The $req_user_level was not a valid level.
> echo "The returned req_user_level was not valid.\n";
> }
>
> Which would output, if $req_user_level was 1 (one):
>
> The user is a Regular User.
>
> Then you know you have a valid user level. Careful though --
> sometimes 0
> will be returned on a failure, depending on your SQL.
>
> Beckman
> ----------------------------------------------------------------------
> -----
> Peter Beckman
> Internet Guy
> beckman(a)angryox.com http://
> www.angryox.com/
> ----------------------------------------------------------------------
> -----
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Karl DeSaulniers
Design Drumm
http://designdrumm.com

First  |  Prev  | 
Pages: 1 2
Prev: Automatic logoff
Next: dl () problem