|
发表于 2010-6-15 15:26:04
|
显示全部楼层
本帖最后由 xpengzp 于 2010-6-19 22:16 编辑
Controller:
PHP复制代码
public function arraytest () {
$this->load->Model('test');
$this->load->library('showpage');
$pagesize = 3; // 每页显示数量
//dump($_SERVER['PHP_SELF'], 'PHP_SELF');
preg_match("/index.php\/.*?\/(.*?)\/(\d.*)/",$_SERVER['PHP_SELF'],$tt);
//dump($tt, 'TT');
if ($tt) {
$page = $tt[2];
} else {
$page = 1;
}
if($this->input->post('name', true) != null) {
$this->test->count_record(array('name' => $this->input->post('name', true)));
$total = $this->test->count_record(array('name' => $this->input->post('name', true)));
$data['rows'] = $this->test->find_All(array('name' => $this->input->post('name', true)), null, 'name', $page*$pagesize-$pagesize, $pagesize);
} else {
$this->test->count_record();
$total = $this->test->count_record();
$data['rows'] = $this->test->find_All(null, null, null, $page*$pagesize-$pagesize, $pagesize);
}
$this->showpage->showpage($total, $pagesize);
$data['page'] = $this->showpage->getContent();
$this->load->view('welcome_message',$data); 复制代码
Model 代码:
PHP复制代码
class Test extends Model {
public function __construct () {
parent ::Model();
}
// 统计符合条件的记录数
public function count_record ($conditions = null) { // $conditions = array();
if (!empty($conditions)) {
$this->db->like($conditions);
}
$this->db->from('ci_areacity');
return $this->db->count_all_results();
}
/**
* 返回符合条件的记录集
*
* @var array $conditions
* @var array $fields
* @var string $order
* @var int $page
* @var int $pagesize
* @return array
*/
public function find_All ($conditions = null, $Fields = null, $order = null, $page = 0, $pagesize = 10) {
if(!empty($conditions)) {
$this->db->like($conditions);
}
if (!empty($Fields)) {
$this->db->select($Fields);
}
if (!empty($order)) {
$this->db->order_by($order);
}
$this->db->limit($pagesize, $page);
$query = $this->db->get('ci_areacity');
return $query->result_array();
}
}
复制代码
View 代码:
HTML复制代码
<table width="370" border="1" cellspacing="1" cellpadding="0">
<tr>
<td height="40" colspan="4"><table width="666" border="0" cellspacing="0" cellpadding="0">
<?php echo form_open('admin/arraytest'); ?>
<tr>
<td width="30" height="21"> </td>
<td width="63">名称: </td>
<td width="175"><input type="text" name="name" id="textfield" /></td>
<td width="50"><input type="submit" name="button" id="button" value="提交" /></td>
<td width="48"> </td>
</tr>
<?php echo form_close(); ?>
</table></td>
</tr>
<tr>
<td height="24" colspan="4"> </td>
</tr>
<tr>
<td><div align="center">ID </div></td>
<td><div align="center">Code </div></td>
<td><div align="center">Name </div></td>
<td><div align="center">Action </div></td>
</tr>
<?php foreach($rows as $row): ?>
<tr>
<td height="24"><?php echo $row['id']; ?></td>
<td><?php echo $row['code']; ?></td>
<td><?php echo $row['name']; ?></td>
<td align="center">编辑 | 删除 </td>
</tr>
<?php endforeach; ?>
<tr>
<td height="24" colspan="4"> </td>
</tr>
<tr>
<td height="24" colspan="4" align="right"><?php echo $page; ?></td>
</tr>
</table>
复制代码
注:本例没有使用showpage类中的getStart()方法 |
-
|