我想通过 PHP 从我的数据库中创建一个列表(我使用 CodeIgniter)。
这是我的数据库:
CREATE DATABASE IF NOT EXISTS `flux_demo` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `flux_demo`;
CREATE TABLE IF NOT EXISTS `people` (
`people_id` int(11) NOT NULL AUTO_INCREMENT,
`Active` int(1) DEFAULT NULL,
`Login` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`Mail` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Name` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Bio` text COLLATE utf8_unicode_ci NOT NULL,
`Register Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Last Activity` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`Thumbnail` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`people_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `projects` (
`project_id` int(11) NOT NULL AUTO_INCREMENT,
`Project` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Description` text COLLATE utf8_unicode_ci NOT NULL,
`Status` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Thumbnail` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`Start Date` date NOT NULL,
`End Date` date NOT NULL,
PRIMARY KEY (`project_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `assigned_projects_ppeople` (
`people_id` int(11) NOT NULL,
`project_id` int(11) NOT NULL,
PRIMARY KEY (`people_id`,`project_id`),
FOREIGN KEY (people_id) REFERENCES people (people_id),
FOREIGN KEY (project_id) REFERENCES projects (project_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
如您所见,我使用多对多表来建立项目和人员之间的关系。在 CodeIgniter 中,我使用会话类来存储会话数据('id'、'Name'、'Login'、'Thumbnail')。我的视图页面是一个使用 ul 和 li 标签的简单列表。
<?php
$this->load->model('project_model');
$this->project_model->getProjectsForUser();
echo "<ul>";
foreach($result as $r)
{
echo "<li><a href=http://www.domain.com/$r->Project>$r->Project</a></li>";
}
echo "</ul>";
?>
我的问题是如何创建与会话用户匹配的列表?我的 uri 段将是:http ://domain.com/project_name
重新编辑:我创建了模型:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Project_model extends CI_Model{
public function getProjectsForUser()
{
$query = $this->db->query(
'SELECT r.project_id, r.Project
FROM assigned_projects_ppeople a JOIN people p
ON a.people_id = p.people_id JOIN projects r
ON a.project_id = r.project_id
GROUP BY r.project_id, r.Project'
);
$data = $query->row_array();
return $data;
}
}
?>
任何想法 ?
谢谢 !