Hey everyone!! I want to create a weak entity from two tables, in my first table which is “UserInfo” i have the user id, name, a drop down for category and email. I want to show that when a user logs-in he/she can see a page where it checks from the other table “Entity” that this person have a right to what and displays that you have a right to this this…How can i create that? Plzzzz Help me…
i have also created an image for better understanding this is the URL of the image…
http://tinypic.com/r/2evq82g/6
The way you do that is by creating another table with the two columns you want, and have your code query that table to get all the entities that person is allowed to. Although, what I would do is create another column on the user info table called permissions and have a string of all the entities they are allowed to access. For example, following your img user 1 has permssion to: 1,2,4.
[php]$query = “Select permissions from UserInfo where ID = ‘$id’”;
$results = get_mysql array(…);
$permissions = explode(’,’,$results);
for($i = 0; $i < sizeof($permissions); $i++)
{
if(($i+1) == sizeof($permissions))
{
$access .= " entity.ID = $permissions[$i]";
}
else
{
$access .= " entity.ID = ‘$permissions[$i]’ AND";
}
}
$query = “Select user_access from Entity where ‘$access’”;[/php]
hope that helps,
Albert
Hey Albert! thanks for replying. Can i give you my code? i am having a bit confusion with this.
hey albert!!
im friend of aneeb and working with him in this same project.can u tell me how can we have multiple records entered in the same coloumn of our field in the database. i m using an input field of type checkbox and through that chkeckboxes(4 checkboxes) user can chose multiple option.so how can multiple options be accomodated in single coloumn.i read somewhere that this can create problems so a seperate table should b there for enteries.u plz explain me thorugh coding. since we both r new in php.
thanks.
Hi Hasan, you can save all 4 values in one field, but it is really hard to make sure that the values will be saved correctly in one field, unless you save it as a string. You have several options,
first: is saving it as multiple fields in the db, but that is not what you want
second saving it as a string separated with commas and using the implode()/explode() functions to separate the values when you need them.
the third would be saving all values as a integer, the basics of this option is you set checkbox(cb) 1 to have a value of 3, cb 2 has a value of 5 and cb3 a value of 7, cb a value of 11. Add those values up and you get 3+5+7+11 = 26, if you get an integer of 3,5,7,11 you know that only one cb is selected. If value is 8 you know it is cb 1and 2 checked, value of 10 cb 1and 3 are checked, value 14 cb 1and 4 checked, value of 12 cb 2 and 3, value 16 cb 2 and 4, value 18 cb 3 and 4 and 26 is all 4. Thinking if it is a big mess, but saving it is easy, only a 2 byte int.
the last way i can think of is saving it in 4 bits, like a flag for each. 0000 would be no checks, 1000 would be cb 1 checked, 1100 is cb 1 and 2, 1110 is cb 1,2 and 3. 1010 is cb 1 and 3, and so on. You can use the a loop and a mod(%) off each bit giving you the values of the check boxes backwards(4,3,2,1)
All for ways work, you can choose the best one that works for you, option 2 is the cleanest way to implement and save(as well as easiest), option 3 is a bit messy on the logic side, but saving in the db it is the second most space saving. Option 4 is the most space saving wise, since it really only takes 4 bit to save on the db, logic wise it is still fairly clean with just a loop and 4 variables.
There are certainly more options for you out there, but these are the only ones i can think of at the time of writing this. I would recommend option 2 and 4 for you if you want to just use 1 field.
If you are going to go with the logic I posted previously, then you can generalize that logic and use it for both things.
hope you get a better picture of what your options are
albert i really really appreciate your response,
let me explain my problem a bit clearly.
- i have a html form named index.php having 4 fields(username,password,email,and category),category field is a check box field having 4 options( lets say a,b,c,d)
- i have created a tble ‘userinfo’ in the data base for the abve form data with anadditional colom ‘id’(auto incremented).
[php]<?php include “config.php”;
$username=$_POST[‘uname’];
$email=$_POST[‘email’];
$password=md5($_POST[‘pass’]);
$category=$_POST[‘category’];
$insert=‘INSERT into userinfo(uname,email,pass,category)VALUES("’.$username.’","’.$email.’","’.$password.’","’.$category.’")’;
mysql_query($insert);
// Make sure query inserted user successfully
if ( !mysql_insert_id() )
{
die(“Error: User not added to database.”);
}
else
{
// Redirect to thank you page.
echo “THANKS FOR REGISTERING!!!”;
}
// end if
?>[/php]
this is how i inserted the data in the databse.
3. now what i want is that suppose a user fill the form with multiple options in th chkbox(lets say he choose a,c,d).i want to display the user when he logins his id,username and categories he choose similarly other users with other info ac to their selected category.
what i srched from net is that saving mutiple checkbox enteries in same colom cell issues you with 1st normal form problem. saving it in other tble is better.
SO plz help me with this.
let me share u my idea.u plz have your professional look on it.
idea is there is another tble with 2 colom one ‘id’ same as (userinfo tble) and scnd ‘category’ that user selects from chkbox options.now is this possible when user enters the data in the registration form goes into their respectble tbles. and when he logins get the rsulted page that i discussed abv??
i think its some kind of many to many relation…1 usr can have many categories and many categories can have one user. SO plz help if this idea is applicable.
AND LAST OF ALL SORRY FOR MY ENGLISH…
THAKNS ALOT!!
Hi Hasan,
First off, thank you for considering me a professional, but am actually far from one. I am currently a college student and I program as a hobby. I go in here to help people because I want to get more experience in programming and learn from others at the same time.
On to your code, first thing i would change is how you are grabbing information from the form, because there is the possibility that the user didn’t fill in a section, like username, and you don’t what create a user without a username. I would recommend you look into the isset() function of php: http://php.net/manual/en/function.isset.php
here is an example of what you want to do:
[php]if(isset($_POST(‘uname’)
{
$username = $_POST(‘uname’);
}
else
{
die(“Error: A username is required”);
}
[/php]
of course there are more elegant ways of through the error but this is just an example for you to build from
As for your check boxes, you need to name each one a unique name like category1,category2,… and $_POST each on individually to see which one is checked, doing the same thing above.
Now comes the adding to the db(database), I would split this up into two parts one is inserting the username, email and password into the userinfo table. After inserting you want to call the table again selecting the id for that particular user, so [php]$query = “SELECT id from userinfo where username = ‘$username’”;[/php]. For categories I would really recommend you not to cheat yourselves into saving space and go with another table and 4 fields, one for each category since you will be calling it so much and comparing with it so much (it will save a bunch if time if you have a big user db). With the id of the user, you can then[php] $insert = “INSERT into categories values (’$id’,’$category1’, ‘$category2’,’$category3’,’$category4’)”; [/php]
This category table should just have a foreign key referencing userinfo table linking the the column ‘id’ of the two tables.
when you want to get the other users with the same category selections as the current user the query will be a little more complex. When they login you want to grab the id as well when you are checking the username and password. Then you want to select the categories the current user is part of, and use those categories to select other userid with the same categories. With that you can go and select the usernames’ of those id in the userinfo table. I know it seems complex when written out but this can really be done in with two queries if you know how to use sql joins.
hope that helps and good luck,
Albert
Thank you Albert for helping us, we were stuck in this from past 10 days but can’t find a perfect solution. You helped us a a lot. Thank you very much for helping us. May God Bless you.
Albert!! thanks alot man, u are a genious soul.
ok…that isset() function taht u advised me to use.i think i have already done thay but not in php,i have used it as a html tag attribute im sure u know abt it the (“required”) attribute in input tag.it does the same functionality as u coded in php.hope u got it.
secondly that checkbox thing which u explained brilliantly, i m certain that it will solve my problem. i will let u know if i face any problem.
But again a deep thanks buddy…
One thing more can i have any of your id.so that i can contact u apart from this forum.
HASAN
sure, my email is [email protected], i check that email every day so if you email me there I should reply within the day. Good luck on your project guys.
Albert
hey albert!!!
In need of ur assistance again.
look what i did is, those checkboxes that i made in reg form were static so
i made them DYNAMIC i.e if i want to add another category i will not be doing
that in html coding instead i will do it from database.
here is the coding:
[php]
<input type=“checkbox” name="$entity[‘id’]" value="<?php echo $entity['name'];?>" id=’".$entity[‘id’]."’/><?php echo $entity['name'];?>
<?php endwhile;?>