sanfly Posted August 1, 2006 Share Posted August 1, 2006 Lets say I have a table in my database that stores "permission" values for my content management system.When the user logs in, I have a script that extracts these permissions and stores them as sessions to be used around the CMS as needed.I currently have a script that manually sets all these permissions (see code snippet below), but Im getting sick of manually updating and adding the code for new permissions every time I add a new module or function. What I would like to do is have a script that can automatically detect the permissions and set the sessions. The permission values are either 0 (not allowed) or 1 (allowed).The script I currently have is like this:[code]// Permissions include "db.php"; $q = "SELECT * FROM u_groups WHERE ugroup_id = '$user_groups'"; $r = mysql_query($q) or die(mysql_error()); $row = mysql_fetch_array($r); // Admin Panel $perm_adminpanel = $row['perm_adminpanel']; // Access to admin panel $_SESSION['perm_adminpanel'] = $perm_adminpanel; $perm_configadmin = $row['perm_configadmin']; // Configure Admin Settings $_SESSION['perm_configadmin'] = $perm_configadmin; $perm_setperm = $row['perm_setperm']; // Allowed to set permissions for groups $_SESSION['perm_setperm'] = $perm_setperm; $perm_addgroup = $row['perm_addgroup']; // Allowed to create new groups $_SESSION['perm_addgroup'] = $perm_addgroup; [/code]so I image it would go:[code]include "db.php"; $q = "SELECT * FROM u_groups WHERE ugroup_id = '$user_groups'"; $r = mysql_query($q) or die(mysql_error()); $row = mysql_fetch_array($r); // here would invoke some kind of loop? $value = $row[$name]; $_SESSION['name'] = $value; // end the loop[/code]If I havent been clear enough, please let me know and I will try and explain it better..... Quote Link to comment https://forums.phpfreaks.com/topic/16191-solved-automating-fetching-values-from-mysql-table/ Share on other sites More sharing options...
wildteen88 Posted August 1, 2006 Share Posted August 1, 2006 Use a foreach loop:[code=php:0]// loop through the $row array, setting up the sessions vars automatically.foreach($row as $key => $value){ $_SESSION[$key] = $value;}[/code]Make sure you have session_start(); as the first line in permission file, in order for the session variables to populate.EDIT: Come to think of it you might not need a foreach loop, instead your can just do this:[code=php:0]$_SESSION = mysql_fetch_array($r);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16191-solved-automating-fetching-values-from-mysql-table/#findComment-66943 Share on other sites More sharing options...
sanfly Posted August 1, 2006 Author Share Posted August 1, 2006 I had tried that foreach array, but for some reason each one came up twice, once with a numeric key, and once with the name of the key.However, since all my permissions start with "perm_", I ended up using the following to only set the named keys (not the numeric keys) as sessions:[code=php:0] $q = "SELECT * FROM u_groups WHERE ugroup_id = '$user_group'"; $r = mysql_query($q) or die(mysql_error()); $row = mysql_fetch_array($r); foreach( $row AS $key => $val ){ if(strstr($key, "perm_")){ //echo "$key, $val<br>"; $_SESSION[$key] = $val; } }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16191-solved-automating-fetching-values-from-mysql-table/#findComment-67385 Share on other sites More sharing options...
wildteen88 Posted August 2, 2006 Share Posted August 2, 2006 Use mysql_fetch_assoc as opposed to mysql_fetch_array. So use this:[code=php:0]$q = "SELECT * FROM u_groups WHERE ugroup_id = '$user_group'";$r = mysql_query($q) or die(mysql_error());$row = mysql_fetch_assoc($r){ foreach( $row AS $key => $val ) { if(strstr($key, "perm_")) { //echo "$key, $val<br>" $_SESSION[$key] = $val; } }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16191-solved-automating-fetching-values-from-mysql-table/#findComment-67729 Share on other sites More sharing options...
sanfly Posted August 2, 2006 Author Share Posted August 2, 2006 why Quote Link to comment https://forums.phpfreaks.com/topic/16191-solved-automating-fetching-values-from-mysql-table/#findComment-68095 Share on other sites More sharing options...
ryanlwh Posted August 2, 2006 Share Posted August 2, 2006 mysql_fetch_array fetches BOTH the associative and numeric keys, so you get the columns twice. however, mysql_fetch_assoc only fetches the associative keys, so it's better in this case. you can also get rid of that strstr thing since you no longer have the numeric keys. Quote Link to comment https://forums.phpfreaks.com/topic/16191-solved-automating-fetching-values-from-mysql-table/#findComment-68101 Share on other sites More sharing options...
sanfly Posted August 2, 2006 Author Share Posted August 2, 2006 Sweet, thanks for the explaination ;D Quote Link to comment https://forums.phpfreaks.com/topic/16191-solved-automating-fetching-values-from-mysql-table/#findComment-68159 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.