You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

原生PHP员工详情展示代码迁移至CodeIgniter框架求助

Hey there! Let's migrate your raw PHP employee display over to CodeIgniter's MVC structure properly. I'll break this down into clear, actionable steps with code snippets that fit CI's best practices.

First: Configure Your Database

Ditch that raw mysqli_connect — CodeIgniter handles database connections centrally. Head to application/config/database.php and update the default database settings to match your original setup:

$db['default'] = array(
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '', // Add your actual password if you have one
    'database' => 'testing_ci',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Step 1: Create the Model (Data Layer)

Models handle all database interactions. Create application/models/Employee_model.php with these methods to fetch employee data:

<?php
class Employee_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database(); // Load CI's database library
    }

    // Fetch all employees from the database
    public function get_all_employees() {
        $query = $this->db->get('employees'); // Replace 'employees' with your actual table name
        return $query->result(); // Returns an array of objects
    }

    // Fetch a single employee by ID
    public function get_employee_details($employee_id) {
        $query = $this->db->get_where('employees', array('id' => $employee_id));
        return $query->row(); // Returns a single object
    }
}

Step 2: Create the Controller (Request Handler)

Controllers act as the middleman between models and views. Create application/controllers/Employee.php:

<?php
class Employee extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Employee_model'); // Load our employee model
        $this->load->helper('url'); // Load URL helper to generate clean links
    }

    // Display the list of all employees
    public function index() {
        $data['employees'] = $this->Employee_model->get_all_employees();
        $this->load->view('employees_list', $data); // Pass data to the list view
    }

    // Display details for a single employee
    public function details($employee_id) {
        $data['employee'] = $this->Employee_model->get_employee_details($employee_id);

        // If no employee found, show 404 error
        if (!$data['employee']) {
            show_404();
        }

        $this->load->view('employee_details', $data); // Pass data to the details view
    }
}

Step 3: Create the Views (Presentation Layer)

Views handle all HTML rendering. We'll need two views: one for the employee list, and one for individual details.

View 1: Employee List (application/views/employees_list.php)

<h1>员工列表</h1>
<ul style="list-style: none; padding: 0;">
    <?php foreach ($employees as $emp): ?>
        <li style="margin: 10px 0; padding: 8px; border-bottom: 1px solid #eee;">
            <strong><?= $emp->name ?></strong>
            <a href="<?= site_url('employee/details/' . $emp->id) ?>" style="margin-left: 15px; padding: 4px 8px; background: #007bff; color: white; text-decoration: none; border-radius: 4px;">查看详情</a>
        </li>
    <?php endforeach; ?>
</ul>

View 2: Employee Details (application/views/employee_details.php)

<h1><?= $employee->name ?> 的详细信息</h1>
<div style="padding: 15px; border: 1px solid #eee; border-radius: 8px; max-width: 400px;">
    <p><strong>邮箱:</strong> <?= $employee->email ?></p>
    <p><strong>职位:</strong> <?= $employee->position ?></p>
    <p><strong>入职日期:</strong> <?= $employee->hire_date ?></p>
    <!-- Add more fields here based on your database table structure -->
</div>
<p style="margin-top: 15px;">
    <a href="<?= site_url('employee') ?>" style="color: #007bff; text-decoration: none;">返回员工列表</a>
</p>

Quick Notes to Get It Working:

  • Make sure your database table is named employees (adjust the model if your table has a different name)
  • For CodeIgniter 4 users: The structure is slightly different (use namespaces, App\Models\EmployeeModel etc.), let me know if you need CI4-specific code
  • You can auto-load the URL helper and database library in application/config/autoload.php to avoid loading them in every controller:
    $autoload['helper'] = array('url');
    $autoload['libraries'] = array('database');
    
  • Test it by visiting http://your-ci-project-url/index.php/employee (remove index.php using CI's URL rewriting if you want cleaner URLs)

内容的提问来源于stack exchange,提问作者bankzAladin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:34:31