visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Bit masking with PHP functions


By: TheFalklands U.S.A.  Date: 21/04/2003 00:00:00  English  Points: 75 Status: Answered
Quality : Excellent
I'm newer to php, so bear with me. I've got a user list, and have about 57 properties for each user, all assigned by checkboxes. I'm not worried about adding, as each user will be put in myself, and all checkboxes defaulted to 0. Updating is what I'm focused on. I think bit masking would work well. I'm unsure of how to proceed with the functions though, and how many I'll need. I'll need one to take the selected boxes and then AND'ing them to pop out the value stored in the db, and then when the page is loaded, one to take out that value from the db, and then OR'ing it to re-populate the checked boxes. Are these the only required functions? Also, if someone could educate me on the math I'd need to use to create these functions, I'd be most appreciative. (PHP + MySQL if that makes a difference)

Thanks much for your consideration,

Methonis
By: VGR Date: 21/04/2003 16:47:00 English  Type : Answer
ok ok ok

so you don't want 57 different fields in your 'users' table ? That's understandable 8-)

if you can group your checkboxes logically, it won't hurt.

The rules to establish a bitfield value for, say, N checkboxes, is :
<?
$bitfield=0;
for ($i=0;$i<$N;$i++) $bitfield=$bitfield OR (2<<$i);
// now write $bitfield to the DB
?>


NB if you want, when reconstructing the checkboxes after reading from the DB, you can look at this comment in the online PHP documentation.
It transforms a decimal value (bitfield read from DB table) into an array of booleans, suitable to provide CHECKED attributes to the corresponding set of checkboxes in your generated HTML code :

npm at pla dot net dot py
04-Apr-2003 02:17

This is a function to convert flags saved as integers, to a boolean array for easy comparison.
Might be helpful to someone.
You input the the base10 number and it will return an array with the boolean values.

function flags2array($dec){
$bits= Array();
$bin = decbin($dec);
for ($i==0; $i< (strlen($bin));$i++){
$bits[$i] = $bin{$i};
}
return $bits;
}

Nico


By: VGR Date: 21/04/2003 16:53:00 English  Type : Comment
the "maths" are very simple
bit::={0,1}; // corresponding to FALSE/TRUE for type Boolean

decimal value $bitfield=43 (base 10, so 43 is 4*10+3*1)
=> binary value (decbin()-ed, up to 32 bits) = 101011 (base 2, so 43d(ecimal) is binary 1*1+1*2+0*4+1*8+0*16+1*32)

this means that bit0 = 1, bit1=1, bit2=0, bit3=1, bit4=0, bit5=1

thus checkbox1=CHECKED (HTML attribute), checkbox2=CHECKED, checkbox3=unchecked(no HTML attribute), etc

is this ok ?
By: TheFalklands Date: 22/04/2003 05:19:00 English  Type : Comment
I can't thank you enough for the explaination. I had a very small grasp on the concept, and you filled in a lot of gaps. :)

Thanks again,


By: VGR Date: 22/04/2003 05:22:00 English  Type : Comment
cool because it wouldn't be considered "complete" by my former teachers :D

Do register to be able to answer

EContact
browser fav
page generated in 339.403870 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page