//=====================================
// Controller
//=====================================
public function __construct()
{
parent::__construct();
$this->load->library('myclass');
$this->load->helper('url');
$this->load->library('pagination'); //載入分頁
$this->load->helper('form');
//載入資料庫行為
$this->load->model('sql_base');
//確認權限
$this->myclass->is_power_check();
}
public function index()
{
$data['man_top'] = $this->load->view('manage/man_top','', true); // include top
$data['page_title'] = $this->config->item('bk_title');
//====================
// 分頁設定
//====================
$sql = "select * from info i
left join class_item c on c.class_item_id=i.class_item_id
where 1 ";
$info_title = $this->input->get('info_title');
if($info_title!="")
{$sql = $sql . "and i.info_title like '%".$info_title."%' ";}
$class_item_id = $this->input->get('class_item_id');
if($class_item_id!="")
{$sql = $sql . "and i.class_item_id=".(int)$class_item_id." ";}
$sql = $sql . "order by i.info_date desc,i.info_id desc";
//計算記錄總數
$config['total_rows'] = $this->sql_base->sql_result_row($sql);
//設置本頁路徑
$config['base_url'] = site_url()."/manage/info?".$this->myclass->remove_qs_key($_SERVER['QUERY_STRING'],'per_page');
//設置每頁顯示記錄數
$config['per_page'] = 3;
//取目前分頁的參數(依路徑位置,例:/manage/info/index/1,第4個參數)
$config['uri_segment'] = 4;
//設置分頁導航條樣式
$config['first_link'] = '首頁';
$config['last_link'] = '末頁';
$config['next_link'] = '下一頁>';
$config['prev_link'] = '<上一頁';
$config['page_query_string'] = TRUE; //使用參數方式
$offset=(int)$this->input->get('per_page');
//分頁初始化
$this->pagination->initialize($config);
//查詢返回陣列
$query = $this->sql_base->sql_result($sql." limit ".$offset.",".$config['per_page']);
//分頁連結
$data['options'] = $this->sql_base->get_dropdownlist("select * from class_item order by class_item_sort",'class_item_id','class_item_name');
$data['page_links'] = $this->pagination->create_links();
$data['query'] = $query;
$this->load->view('manage/info', $data); //將date帶入View
}
//=====================================
// view
//=====================================
<form action="<?=site_url();?>/manage/info" method="get" name="srh">
<tr>
<td><table width="697" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF" class="tablebgcolor">
<tr>
<td width="39" class="titlebgcolor2">
<div align="left" class="word01">分類</div></td>
<td width="643" class="titlebgcolor2"><?php
echo form_dropdown('class_item_id', $options,'');
?></td>
</tr>
<tr>
<td class="titlebgcolor2"><div align="left" class="word01">標題</div></td>
<td class="titlebgcolor2"><label>
<input type="text" name="info_title" id="info_title">
<span class="word01">(請入關鍵字)
<input name="submit" type="submit" value="查詢" >
</span></label></td>
</tr>
</table></td>
</tr></form>
//顯示結果列表
foreach ($query->result_array() as $row){
echo $row['info_id'];
}
//顯示分頁
echo $page_links;
//=====================================
// model
//=====================================
//return sql array
public function sql_result($sql){
return $this->db->query($sql);
}
//run sql
public function sql_run($data){
$this->db->query($data);
}
//return sql total row
public function sql_result_row($sql){
$query = $this->db->query($sql);
return $query->num_rows();
}
//return 下拉選單
function get_dropdownlist($sql,$id,$name,$ct1=0)
{
$result = $this->db->query($sql);
$return = array();
if($result->num_rows() > 0){
if($ct1==0){$return[''] = '請選擇';}
foreach($result->result_array() as $row){
$return[$row[$id]] = $row[$name];
}
}
return $return;
}