[eluser]theshiftexchange[/eluser] continued Part 2/3
Create a "roles.php" file in the same directory as the "users.php" file
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Roles
*
* This model represents roles. It operates the following two tables
*
* @package Tank_auth
* @author theshiftexchange
*/
class Roles extends Model
{
function __construct()
{
parent::__construct();
}
/**
* Get all user roles and return them as a string with a '%' seperating them
*
* @param int
* @return string
*/
function get_user_roles($user_id)
{
$query = $this->db->query("SELECT *
FROM users_roles
INNER JOIN roles ON users_roles.rolesID=roles.rolesID
WHERE userID = ".$user_id);
$roles = null;
foreach ($query->result() as $row)
{
$roles = ($roles."%".$row->name);
}
return $roles;
}
}
/* End of file roles.php */
/* Location: ./application/models/auth/roles.php */
Finally in your database create two tables;
Code:
CREATE TABLE IF NOT EXISTS `roles` (
`rolesID` int(16) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`rolesID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=24 ;
Code:
CREATE TABLE IF NOT EXISTS `users_roles` (
`userID` int(16) unsigned NOT NULL,
`rolesID` int(16) unsigned NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;