ibda12u Posted March 6, 2006 Share Posted March 6, 2006 I have a question. I am using a php login script that checks an access level in a table. This access level is just a number in a column called level in the table.What I am wondering is, is there a way to input more than 1 number in the level column? IF so, is there something I can insert in between them, so that sql can tell that it's not just 1 number, but seperate numbers? Quote Link to comment Share on other sites More sharing options...
Gaia Posted March 6, 2006 Share Posted March 6, 2006 You could probably do something like seperate them with comma's (make sure the table type is set to text) then use the explode() function in PHP to put them in an array then use the in_array() function with an if statement?Not sure if that's the direction you're wanting to go or not. Quote Link to comment Share on other sites More sharing options...
ibda12u Posted March 6, 2006 Author Share Posted March 6, 2006 [!--quoteo(post=352258:date=Mar 6 2006, 04:01 PM:name=Gaia)--][div class=\'quotetop\']QUOTE(Gaia @ Mar 6 2006, 04:01 PM) [snapback]352258[/snapback][/div][div class=\'quotemain\'][!--quotec--]You could probably do something like seperate them with comma's (make sure the table type is set to text) then use the explode() function in PHP to put them in an array then use the in_array() function with an if statement?Not sure if that's the direction you're wanting to go or not.[/quote]I think that's the direction I'm looking for. I'm not super strong with explode and in_array yet (something to read up on. But I guess I'd like my code to at all the numbers in the array, and then allow access based on that.Ex. suppose a page has an access lvl requiring 12. My user has a 1,12,34 all in his level field.So I could explode that data into an array. Then have my access script check to see if 12 is in the array? Quote Link to comment Share on other sites More sharing options...
XenoPhage Posted March 6, 2006 Share Posted March 6, 2006 [!--quoteo(post=352271:date=Mar 6 2006, 05:13 PM:name=ibda12u)--][div class=\'quotetop\']QUOTE(ibda12u @ Mar 6 2006, 05:13 PM) [snapback]352271[/snapback][/div][div class=\'quotemain\'][!--quotec--]I think that's the direction I'm looking for. I'm not super strong with explode and in_array yet (something to read up on. But I guess I'd like my code to at all the numbers in the array, and then allow access based on that.Ex. suppose a page has an access lvl requiring 12. My user has a 1,12,34 all in his level field.So I could explode that data into an array. Then have my access script check to see if 12 is in the array?[/quote]That would work.. Of course, you lose some SQL functionality this way as the database will have no idea that there are multiple values in that column.The proper "SQL way" to do this is to create an additional table with the mappings. Quote Link to comment Share on other sites More sharing options...
Gaia Posted March 6, 2006 Share Posted March 6, 2006 Exactly. When you retrieve the data, it would be like 1,12,34...when you explode it, it will end up in an array.[code]$foo = array ( [0] => 1, [1] => 12, [2] => 34);[/code]Then with the in_array function you just wrap an if statement around it.[code]if ( in_array('1',$foo) ) { echo '1 was found in the array';} else { echo '1 was not found in the array';}[/code]Hope that helps some :).[i]EDIT[/i]: And yea, as Xeno said, you could create a new MySQL table listing the permissions and do it from a MySQL perspective. Quote Link to comment Share on other sites More sharing options...
ibda12u Posted March 6, 2006 Author Share Posted March 6, 2006 [!--quoteo(post=352280:date=Mar 6 2006, 04:19 PM:name=Gaia)--][div class=\'quotetop\']QUOTE(Gaia @ Mar 6 2006, 04:19 PM) [snapback]352280[/snapback][/div][div class=\'quotemain\'][!--quotec--]Exactly. When you retrieve the data, it would be like 1,12,34...when you explode it, it will end up in an array.[code]$foo = array ( [0] => 1, [1] => 12, [2] => 34);[/code]Then with the in_array function you just wrap an if statement around it.[code]if ( in_array('1',$foo) ) { echo '1 was found in the array';} else { echo '1 was not found in the array';}[/code]Hope that helps some :).[i]EDIT[/i]: And yea, as Xeno said, you could create a new MySQL table listing the permissions and do it from a MySQL perspective.[/quote]Hmm.. I'm guesing that'd be a table with a access level, an id, and an foreign key back to the original table? Quote Link to comment Share on other sites More sharing options...
wickning1 Posted March 6, 2006 Share Posted March 6, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Hmm.. I'm guesing that'd be a table with a access level, an id, and an foreign key back to the original table?[/quote]Correct. Another table is the way to go. Comma separated columns are bad. Bad! Quote Link to comment 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.