0

我想通过 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;
}
}
?>

任何想法 ?

谢谢 !

4

2 回答 2

0

在模型中创建一个函数,以便从数据库中返回所有项目的记录

 $response=$this->yourmodel->model_function();

然后尝试将 ul 中的结果数据循环为

 echo "ul";

 foreach($response as $projectData)
{
echo "<li><a href=http://www.domain.com/$projectData->Project>$projectData->Project</a></li>";
}

echo "</ul>";
于 2013-11-06T08:58:47.627 回答
0

我终于成功了。完成后,我们知道这并不难。

这是我查询数据库的函数:

public function getProjectsForUser()
    {
        $login_id = $this->session->userdata('User_id');
        $this->db->select('Project');
        $this->db->from('assigned_projects_ppeople a');
        $this->db->where('people_id', $login_id);
        $this->db->join('projects r', 'r.project_id = a.project_id');
        $query = $this->db->get();

        return $query;
    }

然后,我在我的控制器页面中确定了变量:

$this->load->model('project_model');
$query = $this->project_model->getProjectsForUser();
$args['available_projects'] = $query->result();
$this->load->view('home', $args);

在我的视图页面中插入的最终代码是:

<?php
    echo "<ul>";
    foreach($available_projects as $r)
    {
        echo '<li><a href="'.base_url($r->Project).'">'.$r->Project.'</a></li>';
    }
    echo "</ul>";
?>

我必须定义如何使用这样的 URI:

http://localhost/project/project_id

我认为就像博客方法,所以我要研究它。

谢谢您的帮助 !

于 2013-11-06T20:20:06.257 回答