Jump to content

Question


N-Bomb(Nerd)

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';
}

?>

Link to comment
Share on other sites

Ignace, thanks for constructing that demo for me.

 

After playing around with it.. I think I'm starting to pick up on a bit. :D

 

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..  :-[

 

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.