Jump to content

[SOLVED] Logical question


abdul_zu

Recommended Posts

Hi all,

I need to find a way for the following code.

i have one variable his value is 2047

$val = 2047;

and i have following numbers, and each numbers depend his own description.

 

1 : Active

4 : GW

8 : PC2Phone

16: Devices

128: All clinets

256: Booth

32: CallBack

64: IVR

2: routes

512: Disable rates

1024: rates to DNIS

 

The sum of all numbers will come 2047 always.

 

Now how i can know in $val which number is assigned for which description, and if i need to make some modification like i need to remove the IVR (64) and enable Active (1), which logic so i use.

 

I am sorry if this question is not more clear, please let me know i can do more explination.

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/179086-solved-logical-question/
Share on other sites

It's very hard to understand what you're trying to do, but I'll take a shot in the dark. You should create an array like:

$val = array(1 => 'Active', 4 => 'GW');

and so on.

 

You can then use all the PHP functions for arrays to manipulate the data. I.E. echo $val['1']; // returns Active

 

It looks like your number is supposed to relate to more than one value. If you are trying to do what I think you are, the number 2047 should represent all 11 of those descriptions. In that case, the number is a summation of all the relevant description numbers. So, to find them all, you go backwards and subtract the largest possible description numbers from the $val number until you get down to 0.

I am sorry for this explanation.

Here is once again i am trying if it will make some help for me.

 

I have one filed in MySQL table called permissions.

 

and in HTML page i have 11 check box which  have the separate value for the 11 titles.

<label>
<input type="checkbox" name="checkbox" value="1" />
Active</label>
<label>
<input type="checkbox" name="checkbox" value="4" />
GW</label>

Till 1024: rates to DNIS

 

When this page will load i need to select the checkbox if its value already exits in permissions filed.

The query for mysql is created and working well.

I need to get proper value to select only appropriate checkbox.

 

Thank You

 

 

Imagine that number as a binary string:

2047 = 11111111111

 

Notice eleven 1's. These are representing each attribute. Now, if CallBack is disabled, for example, the binary number would change to 11111011111, making the decimal number 2015 which is 32 less than 2047.

 

Knowing this, there are a few ways to do this. You could just convert the number to binary and traverse the string, or you could shift the string using the shift operator (<<) to traverse it by powers of two.

 

Or you could put them all in arrays like milesap suggested.

Hopefully this will be of use to you. It will output all the options that match (binary wise) to the number $val

$a = array(
    1 => 'Active',
    2 => 'routes',
    4 => 'GW',
    8 => 'PC2Phone',
    16 => 'Devices',
    32 => 'Callback',
    64 => 'IVR',
    128 => 'All clinets',
    256 => 'Booth',
    512 => 'Disable rates',
    1024 => 'rates to DNIS'
);

$val = 7;

foreach($a as $k => $v) {
    if($val & $k) {
        echo $v.'<br />';
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.