<?php
class
Article
extends
CI_Controller{
public
$tips
;
function
__construct(){
parent::__construct();
$this
->load->library(
'myclass'
);
$this
->load->database();
$this
->load->helper(
'url'
);
$this
->tips=
array
(
'required'
=>
' [%s] 是必须填写的!'
,
'is_unique'
=>
'此 [%s] 已经存在,请输入其它名称!'
,
'min_lenght'
=>
' [%s] 最小长度为 [%s]'
,
'max_length'
=>
'[%s] 最大长度为 [%s]'
);
}
function
index(){
echo
"这里是文章的首页"
;
echo
"<br />"
;
$this
->load->helper(
'url'
);
$addr
=site_url(
'article/article_list'
);
echo
"<a href='$addr'>查看文章</a>"
;
$addr
=site_url(
'article/article_page'
);
echo
"<a href='$addr'>查看分页</a>"
;
}
function
article_list(){
echo
"这里是文章列表"
;
$this
->load->database();
echo
"<br />"
;
$query
=
$this
->db->where(
"id >"
,5)->select(
'id,title'
)->from(
'article'
)->order_by(
'id'
,
'desc'
)->limit(4)->get();
$info
=
$query
->result_array();
$this
->myclass->p(
$info
);
echo
"第一条记录的标题:"
.
$info
[0][
'title'
];
echo
"<br />"
;
echo
"第二条记录的标题:"
.
$info
[1][
'id'
];
echo
"<br />"
;
echo
"表article中共有这么些记录:"
.
$this
->db->count_all(
'article'
);
echo
"<br />"
;
echo
"本次共查询出这么些条记录:"
.
$query
->num_rows();
}
function
article_page(
$page
=1){
$config
=
array
();
$this
->load->database();
$config
[
'total_rows'
]=
$this
->db->select(
'*'
)->from(
'article'
)->count_all_results();
$config
[
'per_page'
]=5;
$this
->load->helper(
'url'
);
$config
[
'base_url'
]=site_url(
'article/article_page'
);
$config
[
'num_links'
]=100;
$config
[
'use_page_numbers'
]=true;
$config
[
'first_link'
]=
'首页'
;
$config
[
'last_link'
]=
'尾页'
;
$config
[
'next_link'
]=
'下一页'
;
$config
[
'full_tag_open'
]=
'<p>'
;
$config
[
'full_tag_close'
]=
'</p>'
;
$this
->load->library(
'pagination'
);
$this
->pagination->initialize(
$config
);
echo
$this
->pagination->create_links();
$page
=
$page
?
intval
(
$page
):1;
$start
=(
$page
-1)*
$config
[
'per_page'
];
$query
=
$this
->db->select(
'*'
)->from(
'article'
)->limit(
$config
[
'per_page'
],
$start
);
$info
=
$query
->get()->result_array();
$this
->myclass->p(
$info
);
echo
$this
->pagination->create_links();
}
protected
function
_page(
$total_rows
,
$per_page
,
$base_url
){
$config
=
array
();
$config
[
'total_rows'
]=
$total_rows
;
$config
[
'per_page'
]=
$per_page
;
$this
->load->helper(
'url'
);
$config
[
'base_url'
]=site_url(
$base_url
);
$config
[
'num_links'
]=100;
$config
[
'use_page_numbers'
]=true;
$config
[
'first_link'
]=
'首页'
;
$config
[
'last_link'
]=
'尾页'
;
$config
[
'next_link'
]=
'下一页'
;
$config
[
'full_tag_open'
]=
'<p>'
;
$config
[
'full_tag_close'
]=
'</p>'
;
$this
->load->library(
'pagination'
);
$this
->pagination->initialize(
$config
);
return
$this
->pagination->create_links();
}
function
page(
$page
=1){
$config
[
'per_page'
]=5;
$page
=
$page
?
intval
(
$page
):1;
$start
=(
$page
-1)*
$config
[
'per_page'
];
$query
=
$this
->db->select(
'*'
)->from(
'article'
)->limit(
$config
[
'per_page'
],
$start
);
$info
=
$query
->get()->result_array();
return
$info
;
}
function
article_add(){
$this
->load->library(
'form_validation'
);
$chinesetips
=
$this
->tips;
$this
->form_validation->set_message(
$chinesetips
);
$this
->form_validation->set_rules(
'title'
,
'标题'
,
'trim|required|is_unique[article.title]|min_length[6]|max_length[12]'
);
$this
->form_validation->set_rules(
'content'
,
'内容'
,
'required'
);
$this
->form_validation->set_rules(
'tag'
,
'状态'
,
'required'
);
if
(
$this
->form_validation->run()==true){
echo
"表单验证成功!"
;
print_r(
$this
->input->post());
$data
=
$this
->input->post();
unset(
$data
[
'Submit'
]);
$data
[
'add_time'
]=
date
(
'Y-m-d H:i:s'
);
$data
[
'views'
]=
'0'
;
$st
=
$this
->db->insert(
'article'
,
$data
);
if
(
$st
){
echo
"数据插入成功!"
;
echo
"新的id为:"
.
$this
->db->insert_id();
}
}
else
{
echo
"表单验证失败!"
;
echo
"<br />"
;
echo
validation_errors();
}
}
function
article_add_viewer(){
$this
->load->helper(
'url'
);
$this
->load->view(
'article_add'
);
}
function
article_links(){
$addr
=site_url(
'article/article_mod_viewer/19'
);
echo
"<a href='$addr'>修改19</a>"
;
}
function
article_mod_viewer(
$id
){
if
(
$id
==
""
){
echo
"没有传递参数"
;
exit
;
}
$this
->load->helper(
'url'
);
$query
=
$this
->db->select()->from(
'article'
)->where(
'id'
,
$id
)->get();
$info
=
$query
->row_array();
print_r(
$info
);
$this
->load->view(
'article_mod'
,
$info
);
}
function
abc(
$val
){
$this
->form_validation->set_message(
'abc'
,
'不行'
);
return
true;
}
function
article_mod(){
$this
->load->library(
'form_validation'
);
$chinesetips
=
$this
->tips;
$this
->form_validation->set_message(
$chinesetips
);
$this
->form_validation->set_rules(
'title'
,
'标题'
,
'trim|required|min_length[6]|max_length[12]|callback_abc'
);
$this
->form_validation->set_rules(
'content'
,
'内容'
,
'required'
);
$this
->form_validation->set_rules(
'tag'
,
'状态'
,
'required'
);
if
(
$this
->form_validation->run()==true){
echo
"表单验证成功!"
;
print_r(
$this
->input->post());
$data
=
$this
->input->post();
$id
=
$data
[
'id'
];
unset(
$data
[
'id'
]);
unset(
$data
[
'Submit'
]);
$data
[
'add_time'
]=
date
(
'Y-m-d H:i:s'
);
$data
[
'views'
]=
'0'
;
$st
=
$this
->db->where(
'id'
,
$id
)->update(
'article'
,
$data
);
if
(
$st
){
echo
"数据修改成功"
;
}
else
{
echo
"数据修改失败"
;
}
}
else
{
echo
"表单验证失败!"
;
echo
"<br />"
;
echo
validation_errors();
}
}
function
article_del(
$id
=
''
){
if
(
$id
==
""
){
}
$id
=
array
(17,18,19);
$this
->db->where_in(
'id'
,
$id
)->
delete
(
'article'
);
$st
=
$this
->db->affected_rows();
echo
$st
;
if
(
$st
){
echo
"数据删除成功!"
;
}
else
{
echo
"数据删除失败!"
;
}
}
}
?>