Ok, so basically my code works perfectly for what I want to do, so I don’t actually have a problem that needs helping. That being said, the reason I came here is because people on another site ridiculed my methods but offered no insight as to why the way I was doing it was wrong/bad so I would love to hear your opinions.
Here’s my tables.
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`thumbnail` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=53;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(100) NOT NULL,
`type` tinyint(1) NOT NULL,
`name` varchar(50) NOT NULL,
`userOf` varchar(100) NOT NULL,
`adminOf` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `userOf` (`userOf`),
KEY `adminOf` (`adminOf`),
KEY `username` (`username`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=25;
The only relevant parts are the primary id of the categories table and the “userOf” and “adminOf” fields on the users table. Those two columns contain a comma separated list of the category ids.
I have this function to create two arrays which I then call at the beginning of each page it’s needed:
[php]
function checkUser_cat($userid,$type) {
global $con;
$query = “SELECT {$type} FROM users WHERE id = ‘{$userid}’”;
$users = mysqli_query($con, $query);
$user = mysqli_fetch_assoc($users);
$array = explode(",",$user[$type]);
mysqli_free_result($users);
return $array;
}
$userOf = checkUser_cat($_SESSION[“userid”],“userOf”);
$adminOf = checkUser_cat($_SESSION[“userid”],“adminOf”);
if (!in_array($catId,$userOf)){
$_SESSION[“msg”] = “You are not authorized to view this page, please log in.”;
redirect_to(“index.php”);
}
[/php]
anyone who has the category id in the adminOf column also has it in the userOf column, but not necessarily vice-versa.
after those arrays are defined I use “if in array” statements to determine what content will be generated for users or admins.
So basically my question is, is this a poor way of doing this?
Thank you for your input.