前言:纯粹个人笔记,好记性比不上个烂笔头
1.安装wamp+下载codeigniter 3.X
2.welcome controler, welcome message view
3.views/Pages/home.php, controllers/Page.php
public function index()
{
// $this->load->view('welcome_message');
// echo 'hello ci3 from me';
$data['title'] = 'welcome to my ci3 world';
$data['message'] = 'It is a good day';
$this->load->view('Pages/home',$data);
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title><?php echo $title;?></title>
</head>
<body>
<h1>Hello ci3 world from me</h1>
<?php echo $message;?>
</body>
</html>
4.config/config.php
$config['base_url'] = 'http://localhost/ci3';
autoload.php
$autoload['helper'] = array('url');
5.第一个model例子
controllers/New_con.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class New_con extends CI_Controller {
public function index()
{
$this->load->model('New_model');
$this->New_model->model_result();
$sec_mod['currency'] = $this->New_model->second_model();
// print_r($sec_mod);
$this->load->view('new_view',$sec_mod);
}
public function new_func()
{
echo 'Hello world';
}
}
models/New_model.php
<?php
class New_model extends CI_Model{
public function model_result()
{
echo 'This is the model';
}
public function second_model(){
$curr = array('us'=>'usd','uk'=>'pound','eu'=>'euro');
return $curr;
}
}
?>
views/new_view.php
hello world
<?php print_r( $currency);?>
6.一个样本数据库(ci3)
CREATE DATABASE `ci3` /*!40100 DEFAULT CHARACTER SET utf8 */
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`post_title` text NOT NULL,
`post_description` text NOT NULL,
`post_author` text NOT NULL,
`post_date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
INSERT INTO `posts` (`id`, `post_title`, `post_description`, `post_author`, `post_date`) VALUES (NULL, 'ci3', 'ci3 is a good framework', 'yyy', '2020-06-02'), (NULL, 'ci4', 'ci4 is a good framework', 'xxx', '2020-06-02')
设置好config/database.php然后取出数据
controllers/New_con.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class New_con extends CI_Controller {
public function index()
{
$this->load->model('New_model');
$data['posts'] = $this->New_model->database();
$this->load->view('new_view',$data);
}
public function new_func()
{
echo 'Hello world';
}
}
views/new_view.php
hello world
<?php //print_r( $currency);
foreach($posts as $post){
echo $post->post_title;
echo '<br>';
echo $post->post_date;
}
?>
models/New_model.php
<?php
class New_model extends CI_Model{
public function database(){
$this->load->database();
$sql = $this->db->query("SELECT * FROM posts");
$result = $sql->result();
return $result;
}
}
?>