如何在Codeigniter上使用数据表(特别是用于制作搜索框)?

der*_*rry 2 php codeigniter datatables

好吧,在我开始之前,我很抱歉英语不好,但我在编程方面的知识很少,特别是在我已经工作2周的codeigniter上.

然后我找到了数据表.我觉得有趣的是,我可以整合 - 或者你们所谓的任何东西 - 带有codeigniter的数据表.

我下载了ignited-datatables,附上它,但它仍然没有在我的项目上工作.

请帮忙吗?我是codeigniter和datatables的新手.不过,我会很感激所有的答案.

PS:只要问你们是否需要代码,我会告诉你;)

Adr*_*nXL 5

我想你已经有了一个正在运行的数据库.例如,假设我们有一个名为"cars"的表,其中包含汽车列表及其特征.

cars :
  id    |  brand  | model  | year
   1    |  Ford   | Escort | 1989
   2    |  Audi   | A4     | 2005
  ...   |  ...    | ...    | ...
Run Code Online (Sandbox Code Playgroud)

它如何在codeigniter上工作:

1 /配置文件


application/config/database.php中设置数据库连接值.

这里我们为数据库"mydatabase"设置本地服务器的连接.登录名/密码是myusername/mypassword.

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'myusername',
    'password' => 'mypassword',
    'database' => 'mydatabase',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'autoinit' => TRUE,
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请访问:http://www.codeigniter.com/user_guide/database/configuration.html

2 /型号


Codeigniter是一个MVC框架.这意味着您必须将数据库(模型)访问与您显示的内容(视图)或任何其他处理(控制器)分开.在application/models /中,我们创建了一个名为Cars.php的新模型:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cars extends CI_Model
{   
}
Run Code Online (Sandbox Code Playgroud)

在我们的新课程中,我们将编写一个能够获得表中所有汽车的功能.CodeIgniter使用名为Active Record的ORM.总而言之,它是一种使查询编写更容易和更安全的工具.

public function getAllCars()
{
    $query = $this->db->get('cars');
    //This how you write *SELECT * FROM cars* with Active Record

    return $query->result(); //The result is an array of objects, each row = an object
}
Run Code Online (Sandbox Code Playgroud)

更多详细信息:http : //www.codeigniter.com/user_guide/general/models.html http://www.codeigniter.com/user_guide/database/active_record.html

3 /控制器


现在,我们需要创建一个新的控制器.所以在应用程序/控制器中创建一个名为Cars.php的新文件:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cars extends CI_Controller 
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {

    }

}
Run Code Online (Sandbox Code Playgroud)

在我们的新控制器内部,我们将打电话给我们的模型.

public function index()
{
    //First we need to load the model
    $this->load->model('cars');

    //Now we need to get our car list using the function we write on our model
    $car_list = $this->cars->getAllCars();

   //Finally, we send are list to the view so we can display it.
   $data["cars_lst"] = $car_list;
   $this->load->view("mycars", $data); //We are building this view in the next step.
}
Run Code Online (Sandbox Code Playgroud)

更多详细信息:http://www.codeigniter.com/user_guide/general/controllers.html

4 /查看


最后一步,输出.在application/views /中创建一个新文件mycars.php.请记住,在我们的控制器中,我们调用了该视图并向其发送了一个数组,其中car_list位于索引"car_lst".我们现在可以在我们的视图中使用该数组索引作为var,它将包含我们的汽车列表(还记得模型结果吗?).

<!DOCTYPE html>
<html>

    <head>
         <!-- Meta, title, CSS, ... -->
    </head>

    <body>
        <table class="table table-hover">
             <thead>
                 <tr>
                     <th>Id</th>
                     <th>Brand</th>
                     <th>Model</th>
                     <th>Year</th>
                 </tr>
             </thead>

             <?php foreach ($car_lst as $c): ?>

             <tr>
                 <td><?php echo $c->id; ?></td>
                 <td><?php echo $c->brand; ?></td>
                 <td><?php echo $c->model; ?></td>
                 <td><?php echo $c->model; ?></td>              
             </tr>

             <?php endforeach; ?>

        </table>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

5 /Voilà!


现在,如果我们使用http://localhost/mysite/index.php/cars/访问我们的应用程序,我们应该会看到我们的汽车列表.

这是如何处理codeigniter中的数据库.当然,这是一个简单的预览,但你应该能够理解它是如何工作的,并找到自己的解决方案.