最简单的CI模型:
注意:模型需要用到数据库
配置文件在appcation/config.php
这里我们要用到数据库,需要将databases.php中的
相关参数填写一下,具体不再赘述。,
1,关于MVC:
M层:
<?php class Nb_model extends CI_Model{ public function __construct() { $this->load->database(); } public function get(){ $query = $this->db->get('firm'); return $query->result_array(); } }
C层:
$data['url'] = $this->nb_model->get(); //var_dump($data);exit(); $this->load->view('nb',$data);
VIEW层:
<html> <head> <title>CI heiilo world</title> </head> <body> <!--循环输出数据--> <?php var_dump ($url); ?> </body> </html>
<html> <head> <title>CI heiilo world</title> </head> <body> <!--循环输出数据--> <?php foreach($url as $v): ?> <h1><?=$v['firm_id']?></h1> <?php endforeach?> </body> </html>
2,关于缓存:
$this->load->driver('cache',array('adapter'=>'apc','backup'=>'file')); if(!$foo = $this->cache->get('foo')) { $data['url'] = $this->nb_model->get(); //var_dump($data);exit(); $foo = $this->load->view('nb',$data); $this->cache->save('foo',$foo,300); }else{ $data['url'] = $this->cache->get('foo'); $foo = $this->load->view('nb',$data); //var_dump($foo); }
3,图片上传
view:
<html> <head> <title>网站</title> </head> <body> <form action="<?php echo base_url('index.php/user/upload'); ?>" method="post" enctype="multipart/form-data"> name:<input type="text" name="name" /> <br /><br /> image:<input type="file" name="userfile" size="20" /> <br /><br /> <input type="submit" name="" value="提交" /> </form> </body> </html>
注意:
其中(1)
CI框架 Call to undefined function base_url()解决办法
打开application/config/autoreload.php设置:
$autoload[‘helper’] = array(‘url’);
(2):如果出现链接方法访问不到,应该用192.168.8.111而不是localhost
4,数据+分页
public function list(){
$this->load->library('parser');
$this->load->database();
$this->load->library('pagination');
$data['news_rows'] = $this->db->query("
SELECT * FROM go_member where user_id < 100
") -> result_array();
//网址中取得参数,这里的uri_segment = 3
$config['base_url'] = base_url().'index.php/member/list';
$config['total_rows'] = count($data['news_rows']);
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['page_query_string'] = true;
//下面是一些css配置
/* $config['num_tag_open'] = '<div class="listPage2">';
$config['num_tag_close'] = '</div>';
$config['cur_tag_open'] = '<div class="listPage3"><a href="javascript:void(0)">';
$config['cur_tag_close'] = '</a></div>';
$config['prev_link'] = '<<';
$config['prev_tag_open'] = '<div class="listPage">';
$config['prev_tag_close'] = '</div>';
$config['next_link'] = '>>';
$config['next_tag_open'] = '<div class="listPage">';
$config['next_tag_close'] = '</div>';*/
//初始化,要分页啦
$this->pagination->initialize($config);
//开始分页
if(empty($_GET['per_page'])){
$start = 0;
}else{
$start = $_GET['per_page'];
}
$data['news'] = $this->db->query("
SELECT * FROM go_member
limit $start,10
") -> result_array();
if(empty($_GET['per_page'])){
$per_page = 1;
}else{
$per_page = $_GET['per_page'];
//$per_page = $per_page * 2;
}
$this->load->database();
$query = $this->db->query("SELECT * FROM go_member where user_id < 100 limit $per_page,2");
$this->parser->parse('list', $data);
}
前端:
<html>
<head>
<title>网站</title>
</head>
<body>
<?php
$count = 0;
foreach($news as $item){
$count++;
?>
<table>
<tr>
<td width="40%"><?php echo $item["user_id"];?></td>
<td width="30%"><?php echo $item["user_name"];?></td>
<td><a href="#"><?php echo $item["open_id"];?></a></td>
</tr>
</table>
<?php }?>
<div id="pagelist">
<ul>
<?php echo $this->pagination->create_links();?>
</ul>
</div>
</body>
</html>