如何使用CodeIgniter和MySQL开发一个商业目录

96 阅读3分钟

使用CodeIgniter和MySQL开发一个企业目录

CodeIgniter是一个PHPMVC框架,用于快速构建Web应用程序。它提供了开箱即用的库,用于连接到MySQL等数据库,并执行各种任务,如发送电子邮件、上传文件和管理会话。

在这篇文章中,我们将使用CodeIgniter和MySQL来建立一个简单的商业目录。由于CodeIgniter是一个MVC框架,model 将与数据库有关,view 将是在Web浏览器上看到的HTML部分,controller 将是连接我们的模型和视图的逻辑。

先决条件

要完成这篇文章,你需要具备以下条件。

  • 安装了[CodeIgniter]
  • 安装了[XAMPP]
  • 具有[PHP]和[MySQL]的工作知识
  • 一个网络浏览器,我们将使用[谷歌浏览器]。

安装和设置

首先,我们将在我们的开发环境中安装XAMPP软件。该软件包预装了Apache、MySQL和PHP。

你可以决定单独安装它们,但在我们的案例中,我们将使用XAMPP来提供一个开发PHP和MySQL应用程序的环境。

我们还将设置CodeIgniter。首先访问他们的下载页面以获得最新版本,然后按照他们文档中提供的安装说明进行安装。

我们将解压缩文件夹并将其放在我们服务器的根目录下,在XAMPP中是htdocs ,并将其重命名为bizdir

为了验证一切是否按预期工作,我们将启动我们的Apache服务器,启动我们的网络浏览器,并访问链接http://localhost/bizdir。

数据库设置

我们将通过在MySQL控制台终端运行以下命令来创建一个名为bizdir 的数据库。

create database bizdir;

在创建的数据库中,我们将通过执行以下命令创建一个名为business_dir 的表。

create table business_dir
(
   id int auto_increment primary key,
   biz_name varchar(255) not null,
   cat varchar(255) not null,
   addr varchar(255) not null,
   tel varchar(255),
   website varchar(255),
   email varchar(255)
);

回到我们的项目目录,进入application/config/database.php ,通过设置$db[‘default’] 数组中的值,为我们的数据库编辑所需的配置。我们将需要编辑数据库的用户名、密码和名称,如下所示。

$db['default'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'root',       	// <-- your username
    'password' => '',               // <-- your password
    'database' => 'bizdir',         // <-- your database
    ...
    ...
    'save_queries' => TRUE
);

编写应用程序的代码

如前所述,CodeIgniter是一个MVC框架,意味着我们有模型、视图和控制器。在我们的例子中,模型是与数据库相关的。

视图是应用程序的前端;在Web浏览器中看到的HTML 。控制器是连接模型和视图的逻辑。控制器形成应用程序的业务逻辑。

模型

我们将首先定义我们的模型。我们在application/models 内创建一个名为Bizdir_model.php 的文件,并添加以下代码。

<?php
class Bizdir_model extends CI_Model {
    public function __construct()
    {
        $this->load->database();
    }
}

在上面的代码片段中,我们创建了一个名为Bizdir_model 的类,它继承了CodeIgniter框架中预置的CI_Model 类的属性。构造函数使用$this->db 来加载数据库。

接下来,我们必须在数据库中插入和检索数据。我们将通过添加一个方法get_bizdir() 来检索数据,如下所示。

public function get_bizdir()
    {
        $query = $this->db->get('business_dir');
        return $query->result_array();
    }

在函数get_bizdir 中,我们正在获取存储在目录表内的所有数据。该函数的目的与查询SELECT * FROM business_dir; 相同。

我们将通过PHPMyAdmin ,甚至是MySQL 控制台窗口,通过运行下面的命令将假数据插入到我们的表中。

INSERT INTO `business_dir` (`id`, `biz_name`, `cat`, `addr`, `tel`, `website`, `email`) VALUES
(1, 'VILLA ROSA KEMPINSKI', 'Hotels', 'Waiyaki Way, Nairobi', '+254 703 049 000', 'https://www.kempinski.com/en/nairobi/hotel-villa-rosa/', 'info@kempinski.com'),
(2, 'COOPERATIVE BANK OF KENYA', 'Financial Institution', 'Moi Avenue, Nairobi', '+254 701 255 265', 'https://www.coop-net.com', 'info@coop-net.com'),
(3, 'CHESTER HOTEL', 'Hotels', 'Kariba, Nakuru', '+254 705 442 636', 'https://www.chester.co.ke', 'info@chester.co.ke'),
(4, 'JAJOS FAST FOODS', 'Cafe', 'Freehold, Nakuru', '+254 708 699 536', 'https://www.jajos.co.ke', 'info@jajos.co.ke'),
(5, 'THIKA MOTOR HUB', 'Car Bazaar', 'Kenyatta Avenue, Thika', '+254 721 639 856', 'https://www.thika-hub.com', 'admin@thika-hub.com');

控制器

接下来,我们将创建一个控制器,从模型中获取数据并将其传递给我们的视图。我们将在application/controllers 目录下创建一个名为Bizdir.php 的文件,并添加以下代码。

<?PHP
class Bizdir extends CI_Controller {

   public function __construct()
   {
      parent::__construct();
      $this->load->model('bizdir_model');
   }

   public function index()
   {
      $data['directories'] = $this->bizdir_model->get_bizdir();
      $data['title'] = 'Business Directory';

      $this->load->view('templates/header', $data);
      $this->load->view('bizdir/index', $data);
      $this->load->view('templates/footer');
   }

   public function any()
   {
      $data['title'] = 'Business Directory: Any Page';
      $this->load->view('bizdir/any', $data);
   }
}

Bizdir 类继承了CI_Controller 类。在构造函数中,我们调用CI_Controller ;我们的父类。然后,下一行通过使用$this->load->model('bizdir_model'); ,使其可被访问来加载模型。

我们在索引方法中获取了所有的数据,并将其存储在$data['directories'] 。然后,另一个名为title 的键被添加到变量$data ,在代码的其余部分,我们传递该变量并渲染视图。

$data 将被我们的视图访问,如$title$directoriesany() 方法会重定向任何其他不匹配的URL。

视图

接下来,我们将为我们的应用程序创建视图。首先在application/views 文件夹中创建一个名为templates 的新目录,然后在templates 文件夹中创建两个文件:header.phpfooter.php

application/views/templates/header.php 文件将显示如下图所示。

<html>
<head>
   <title><?php echo $title; ?>: This is a business directory</title>
</head>
<body>
<h1 style="text-align: center"><?php echo $title; ?></h1>

application/views/templates/footer.php 文件将有下面的代码。

<p style="text-align: center; margin-top: 15px">&copy; 2021 Business Directory. All rights reserved.</p>
</body>
</html>

然后,我们可以继续创建一个名为bizdir 的新目录,并在位于application/views/bizdir/index.php 内的文件index.php 中写入以下代码。

<table>
<style>
table {
  border-collapse: collapse;
  width: 100%;
  font-family: arial, sans-serif;
}
td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}
tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
   <thead>
   <tr>
      <th>Business Name</th>
      <th>Category</th>
      <th>Address</th>
      <th>Phone</th>
      <th>Website</th>
      <th>Email</th>
   </tr>
   </thead>
   <tbody>
   <?php foreach ($directories as $dir): ?>
      <tr>
         <td><?php echo $dir['biz_name']; ?></td>
         <td><?php echo $dir['cat']; ?></td>
         <td><?php echo $dir['addr']; ?></td>
         <td><?php echo $dir['tel']; ?></td>
         <td><?php echo $dir['website']; ?></td>
         <td><?php echo $dir['email']; ?></td>
      </tr>
   <?php endforeach; ?>
   </tbody>
</table>

在标题部分,我们正在显示我们在控制器中定义的title 。在index.php 文件中,我们创建了一个循环,遍历存储在我们数据库中的企业列表。

每次循环都会创建一个新的<tr> ,所有的值都显示在<td>

我们将在application/views/bizdir 内创建any.php 文件,并添加以下代码。

<div style="text-align: center; margin-top: 50px; padding-top: 50px;">
   <h2>Error</h2>
   <p>Click <a href="">here</a> to go to home page</p>
</div>

路由

我们现在已经完成了MVC的设置,下一步是创建通往特定视图的路由。我们首先打开application/config/router.php 文件并编辑下面的代码。

$route['dir'] = 'bizdir';
$route['dir/(:any)'] = 'bizdir/any';
$route['default_controller'] = 'welcome';

在上面的代码片段中,键$route 出现在浏览器的URL中。然后,右边的值指向将被调用的控制器的方法。

如果只给了控制器的名字,比如在我们的例子中是bizdir ,它将调用index() 方法。另外,如果URL中包含dir/ 之后的内容,它将调用any() 方法。

现在我们可以访问http://localhost/bizdir/index.php/dir,以验证一切是否如预期的那样工作。

正如你所看到的,数据被从数据库中获取并在浏览器上呈现。

总结

很多功能都可以在应用程序中添加和实现。然而,这已经超出了本教程的目的。

本教程的主要目的是告诉你如何开始使用CodeIgniter。