举个按关键词搜索结果分页的例子,
1.视图HTML
<form action="/index.php/search/index/index.html" method="get">
<p>请输入书名、作者、出版社中的一个或多个来查询。</p>
<p><input type="text" name="s" value="" size="64" /> <input type="submit" value="搜索" /></p>
</form>
</div>
即表单提交到名叫search的controller和名叫index的action, 其中包含了一些动态参数,不是纯列表,故相对比较复杂,后面会提到。
2.控制器
$keyword = $this->input->get ( 's' );
$offset = $this->input->get ( 'offset' );
if (empty ( $offset )) {
$offset = 0;
}
if (! empty ( $keyword )) {
$this->load->model ( 'book_model' );
$this->load->library ( 'pagination' );
$per_page = 10;
$config ['num_links'] = 5;
$config ['base_url'] = '/index.php/' . $this->router->class . '/' . $this->router->method . '/?s=' . $keyword;
$config ['per_page'] = $per_page;
$config ['total_rows'] = $this->Book_Model->find_by_name ( $keyword, NULL, NULL, true );
$config ['page_query_string'] = false;
$config ['query_string_segment'] = 'offset'; //重新定义记录起始位置的参数名,默认为per_page
$this->pagination->initialize ( $config );
$data ['books'] = $this->Book_Model->find_by_name ( $keyword, $per_page, $offset );
$this->load->view ( 'search', $data );
} else {
$this->load->view ( 'search' );
}
}
因为config.php中默认的enable_query_strings是false, 起始位置始终在最后,这样出来的结果类似/index.php/search/index/?s=中国/10,页码取不到,需要将此配置改为false;
3.模型
if ($is_total) {//总数
$query = $this->db->query ( "select count(id) as cnt from {$this->_book} where book_name like '%{$name}%'" );
if ($query->num_rows () > 0) {
$row = $query->row ();
$ret = $row->cnt;
}
}else{//列表
$query = $this->db->query ("select * from {$this->_book} where book_name like '%{$name}%' limit {$offset}, {$per_page}");
$ret = $query->result ();
}
return $ret;
}
摘自:http://blog.nengzuo.com/?p=1180
本文链接
摘要: /** 燕十八 公益PHP培训 课堂地址:YY频道88354001 学习社区:www.zixue.it **/
class db {
private $mysqli; //数据库连接
private $options; //SQL选项
private $tableName; //表名
public function __construct($tabName) {
$this->tableName = $tabName;
$this->db ();
}
private function db() {
$this->mysqli = new mysqli ( 'localhost', 'root', '', 'hdcms' );
$this->mysqli->query("SET NAMES GBK");
}
public function fields($fildsArr) {
if (empty ( $fildsArr )) {
$this->options ['fields'] = '';
}
if (is_array ( $fildsArr )) {
$this->options ['fields'] = implode ( ',', $fildsArr );
} else {
$this->options ['fields'] = $fildsArr;
}
return $this;
}
public function order($str) {
$this->options ['order'] = "ORDER BY " . $str;
return $this;
}
public function select() {
$sql = "SELECT {$this->options['fields']} FROM {$this->tableName} {$this->options['order']}";
return $this->query ( $sql );
}
private function query($sql) {
$result = $this->mysqli
->query ( $sql );
$rows = array ();
while ( $row = $result->fetch_assoc () ) {
$rows [] = $row;
}
return $rows;
}
private function close() {
$this->mysqli
->close ();
}
function __destruct() {
$this->close ();
}
}
$chanel = new db ( "hdw_channel" );
$chanelInfo = $chanel->fields ( 'id,cname,cpath' )
->select ();
echo "<pre>";
print_r ( $chanelInfo );
class a {
protected function aa(){
echo 222;
}
}
class b extends a{
function bb(){
$this->aa();
}
}
$c = new b();
$c->bb();
public 公有的:本类,子类,外部对象都可以调用
protected 受保护的:本类 子类,可以执行,外部对象不可以调用
private 私有的:只能本类执行,子类与外部对象都不可调用
本文链接
1、将该函数添加到Utils类中,该类专门存一些跑龙套功能
/**
* Miscellaneous utility methods.
*/
final class Utils {
private function __construct() {
}
/**
* Get IP address
* @return string IP address string
*/
public static function getIpAddress() {
return $_SERVER["REMOTE_ADDR"];
}
}
2、调用
本文链接