开发者

while-list gives strange values

i get the next post vars from a form:

$_POST['segment'][2]
$_POST['segment'][3]
$_POST['segment'][4]
$_POST['segment'][5]

These are checkboxes from a form.

Don't ask me why, but the posted data is transformed to: $IN["segment"]

I check them with:

if($IN['segment'][5])
  $podo_segment = 5;
elseif($IN['segment'][4])
   $podo_segment = 4;
elseif($IN['segment'][3])
   $podo_segment = 3;
elseif($IN['segment'][2])
   $podo_segment = 2;
else
   $podo_segment = 'NULL';

podo means feet:)

Then i insert the (highest) value into the mysql. (a higher value means that lower values are included too, that is why just 1 field is used)

I have to update older values but a mask is used and i really dont understand how this is done so no idea how to transform/update this to new values 2,3,4 or 5.

$segmask = 0;
while (list($a,$b)=@each($IN["segment"]))
   $segmask += pow(2,$a);

I tried php.net but could not make any sense of it. at least nothing more then $segmask becomes a value of 2^$a. (what's $a,$b and $b doesn't seem to do anything???) but the resulting values in mysql for example:

5 gives 32, (looks ok to me: 2开发者_如何学运维^5)

No other choices then 2,3,4 and/or 5, gives values like:

9977932
16273612
18366668
24662092
16448
17846476
18370636
34619404
270024716
18370780
9982092
34623564
9977980
512
16386
1064964
2
37240844
35143692
1064972
9977868
16388
2148548620
269500428
2148548668
268976140
1107836940
11026444
2149072908
1589260

At the end i need to update a new mysql-field with values 2,3,4 or 5 based upon this older field.

Thanks in advance for any help!


I personally don't like the while (list() = each()) structure. It looks messy and confusing and 99.9% of the time foreach will do the same job just as efficiently, if not more so.

Change

while (list($a,$b)=@each($IN["segment"]))
  $segmask += pow(2,$a);

to

foreach ($IN["segment"] as $key => $val)
  $segmask += pow(2, $key);

...and see if you still have the same problem. If you do, then the problem lies somewhere else, and I will probably have some more questions about exactly what you are trying to achieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜