N-Bomb(Nerd) Posted January 14, 2010 Share Posted January 14, 2010 Hi, I'm making a website and users are able to create a public profile that other people can view. I would like them to have the opinion to keep certian fields private so they won't be displayed. I still want to keep any value that may already be in the field, how would I go about keeping it from displaying? I was thinking for every field in my database I'll have another suffixed with "_private" and if that's 1 then it won't display that field. For example, let's say my table looks like this: email email_private Then in my script, as I'm dynamically creating the profile, I'll check the value of "email_private" before I actually display "email". Does that seem like a good way to go about this? There's going to be quite a bit of information that I want to have this option, but I couldn't think of any better way to handle this than what I stated above. Thoughts/opinions are welcome. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/ Share on other sites More sharing options...
N-Bomb(Nerd) Posted January 14, 2010 Author Share Posted January 14, 2010 Help? Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995056 Share on other sites More sharing options...
harkly Posted January 14, 2010 Share Posted January 14, 2010 That's how I did it. There may be simpler way but I didn't find one. Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995065 Share on other sites More sharing options...
laffin Posted January 14, 2010 Share Posted January 14, 2010 You can also use bit flags. bools are simple to use, but if yer dealing with a lot of flags, a bit flag system is more compact. but bit flags do take a lil more coding in sql & php. Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995066 Share on other sites More sharing options...
ignace Posted January 14, 2010 Share Posted January 14, 2010 Some more explanation of what laffin is saying: (2^0 = 1) = 0001 (2^1 = 2) = 0010 (2^2 = 4) = 0100 (2^3 = = 1000 You notice a pattern in the above code 0001, 0010, 0100, 1000 the one moves from right-to-left. Now each place represents wether or not he wants to show a certain field for example email, birthday, friendlist, .. $birthday = 1; // 1 = 0001 $email = 2; // 2 = 0010 $friendlist = 4; // 4 = 0100 list($bitflags) = mysql_fetch_array($result, MYSQL_NUM); $bitflags = intval($bitflags); // assume: $bitflags = 3 = 0011 (user wants to show email and birthday but nothing else) if ($bitflags & $email) { // 0011 & 0001 = 0001 (true) .. } if ($bitflags & $birthday) { // 0011 & 0010 = 0010 (true) .. } if ($bitflags & $friendlist) { // 0011 & 0100 = 0000 (false) .. } You need to understand binary math to be able to understand why this works: or: 0 | 0 = 0 0 | 1 = 1 1 | 0 = 1 1 | 1 = 1 and: 0 & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1 = 1 xor: 0 ^ 0 = 0 0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0 Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995090 Share on other sites More sharing options...
N-Bomb(Nerd) Posted January 14, 2010 Author Share Posted January 14, 2010 Bit flags sounds like the way I want to go. I understand a bit of what you posted ignace, however it's still pretty confusing. Does anyone happen to know of a decent tutorial for bit flags? I tried Googling without much success.. Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995143 Share on other sites More sharing options...
ignace Posted January 14, 2010 Share Posted January 14, 2010 Try this piece of code to get a hang on it: <form action="" method="post"> <label for="real_name">Show real name:</label> <input type="checkbox" name="real_name" id="real_name"> <label for="email_address">Show email address:</label> <input type="checkbox" name="email_address" id="email_address"> <label for="street_address">Show street address:</label> <input type="checkbox" name="street_address" id="street_address"> <label for="country">Show country</label> <input type="checkbox" name="country" id="country"> <label for="phone_number">Show phone number</label> <input type="checkbox" name="phone_number" id="phone_number"> <label for="cellphone_number">Show cellphone number</label> <input type="checkbox" name="cellphone_number" id="cellphone_number"> <label for="gender">Show gender</label> <input type="checkbox" name="gender" id="gender"> <label for="political_preferences">Show political preferences</label> <input type="checkbox" name="political_preferences" id="political_preferences"> <input type="submit"> </form> <?php if ('POST' === $_SERVER['REQUEST_METHOD']) { $show_real_name=1; $show_email_address=2; $show_street_address=4; $show_country=8; $show_phone_number=16; $show_cellphone_number=32; $show_gender=64; $show_political_preferences=128; $flags = 0; if (isset($_POST['real_name'])) $flags |= $show_real_name; if (isset($_POST['email_address'])) $flags |= $show_email_address; if (isset($_POST['street_address'])) $flags |= $show_street_address; if (isset($_POST['country'])) $flags |= $show_country; if (isset($_POST['phone_number'])) $flags |= $show_phone_number; if (isset($_POST['cellphone_number'])) $flags |= $show_cellphone_number; if (isset($_POST['gender'])) $flags |= $show_gender; if (isset($_POST['political_preferences'])) $flags |= $show_political_preferences; echo 'decimal: ', $flags, ' binary: ', decbin($flags), '<br><br>Public viewable data:<br>'; if ($flags & $show_real_name) echo 'real name<br>'; if ($flags & $show_email_address) echo 'email address<br>'; if ($flags & $show_street_address) echo 'street address<br>'; if ($flags & $show_country) echo 'country<br>'; if ($flags & $show_phone_number) echo 'phone number<br>'; if ($flags & $show_cellphone_number) echo 'cellphone number<br>'; if ($flags & $show_gender) echo 'gender<br>'; if ($flags & $show_political_preferences) echo 'political preferences<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995174 Share on other sites More sharing options...
N-Bomb(Nerd) Posted January 14, 2010 Author Share Posted January 14, 2010 Ignace, thanks for constructing that demo for me. After playing around with it.. I think I'm starting to pick up on a bit. When I'm creating my fields such as: $show_real_name $show_email_address $show_street_address $show_country $show_phone_number $show_cellphone_number $show_gender $show_political_preferences I assign values to starting with 1 then keep raising the power by two for the next number in the order. I don't understand what happens to the "0" value for $flags though. I guess I don't know exactly what's happening when I see these two lines: $flags |= $show_real_name; and if ($flags & $show_real_name) For the first one I'm assuming that it's just adding the bit value of $show_real_name to the right of the "0" in flags, but the "0" doesn't show. Thought the |= assigns that to the right leaving the "0" in tact? As for the second one.. I believe somehow it's comparing if $show_real_name's value is within the $flags variable, but how? I just like to know these things so when I'm programming I can work through the logic myself instead of trying to find examples or trial and error. I'm still a little confused about this, I'm sure I could just get what I need from the example and modify it, but I'm interested in learning. You've been such a big help to me, thank you. Sorry to be such a pain.. Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995205 Share on other sites More sharing options...
laffin Posted January 14, 2010 Share Posted January 14, 2010 well u got the pefect task to do so, right now. I will try to make this easy, and not complex to understand its not a tutorial, but some explanations and already made code. Intergers - Most computers these days, you'll hear are 32 bits. This just means that whole numbers are represented by 32 bis within the computer. with that in mind, u can say, you can have a bit flag system with 32 flags per integer in php Okay, so with that in mind. we want to go ahead and assign each bit position with a specific flag. so we want to make a list, and number them accordingly email, birthday, gender, real name to assign them a positiong, we want to use a bitwise operator known as left bit shift. like this: $private_email = 0; $private_birthday = 1; $private_gender = 2; $private_realname = 3; since we are working with 32 bit integers the position will be in the range of 0-31 (yes, zero counts). now let me introduce some functions for setting and reading specific bit. function setbit($flags,$position) { return $flags|(1<<$position); } function resetbit($flags,$position) { return $flags&(~(1<<$position)); } function togglebit($flags,$position) { return $flags^(1<<$position); } function readbit($flags,$position) { return ($flags|(1<<$position))?1:0; } even tho they are small functions, we now have enough tools to write our code you turn on a bit flag in our integer with setbit. turn it off with resetbit toggle it (if on, turn it off, if off turn it on) with togglebit and of course check a flag with readbit. so lets write a little script using the above function, with our bit flags all together header('Content-type: text/plain'); // we just want a quick display, so we can use plain text to display our sample // Function to aid in our quick display, just adds a newline to a string function nle($str) { echo "{$str}\n"; } $flags=0; // this is our initial setting of our flags $flags=setbit($flags,$private_emal); // turn on private email flag $flags=setbit($flags,$private_gender); // turn on private gender flag nle($flags); $flags=resetbit($flags,$private_gender); // turn off the private gender flag nle($flags); $flags=togglebit($flags,$private_birthday); // since its off, this will turn it on nle($flags); nle("Private Email: ".readbit($flags,$private_email)); nle("Private Birthday: ".readbit($flags,$private_birthday)); nle("Private Gender: ".readbit($flags,$private_gender)); a simple but pretty self explanatory script Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995224 Share on other sites More sharing options...
ignace Posted January 15, 2010 Share Posted January 15, 2010 I actually wanted to hide this kind of complexity from him Now he has to deal with <<, >>, ~ and ^ instead of only | and & function setbit($flags,$position) { return $flags|(1<<$position); } function resetbit($flags,$position) { return $flags&(~(1<<$position)); } function togglebit($flags,$position) { return $flags^(1<<$position); } function readbit($flags,$position) { return ($flags|(1<<$position))?1:0; } N-Bomb check this manual page out: http://php.net/manual/en/language.operators.bitwise.php Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995512 Share on other sites More sharing options...
laffin Posted January 15, 2010 Share Posted January 15, 2010 he doesnt need to understand how it works, just that it works as described, the bit positions are easier to manage as well. especially if used with arrays. As I said that wasnt a tutorial, more of a quick start up using bit flags. Using a quick start up is a lot easier than trying to write your own code, or maintaining the values of each bit position. and learning bit/octals/decimal/hexadecimal numbering systems in order to get a project going is a bit overkill. Most of the time, one understands better whats going on by using premade tools, than trying to read and discover on their own. Just like computers, you dont need to understand how they work in order to use them, but it sure helps if your building your own. Quote Link to comment https://forums.phpfreaks.com/topic/188476-question/#findComment-995629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.