iT邦幫忙

DAY 21
2

[鐵鷹練成]CodeIgniter + Bootstrap系列 第 18

[鐵鷹練成]CI之留言版[排序+資料總數]

進入鐵人賽倒數......
今天介紹兩個方法
資料排序 跟 資料總數
以code和註解來說明
controllers/gbook.php 修改一個方法

    //留言版頁面
    function index(){
        //改成另一種
        $data['query'] = $this->gbook_sql->all_order('gbook','time','desc');
        //資料數量
        $data['total'] = $this->gbook_sql->total('gbook');
        $this->load->view('gbook',$data);
    }

在views/gbook 修改一下 多一個總數

<?php
/*---------------------------------------*/
//........ 此頁面請放在view資料夾底下.......//
/*---------------------------------------*/
$link = 'http://localhost/test/';
?>

<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>公佈欄</title>
<!--以下是 Bootstrap 的文件包-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="<?=$link;?>css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="<?=$link;?>css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="<?=$link;?>js/jquery.js"></script>
<script src="<?=$link;?>js/bootstrap.min.js"></script>
<style>
#heig{
     height:10px;
}
#color{
     background-color: #E5E5E5;
}
</style>


<!--"container"為固定置中,請參考 http://kkbruce.tw/Bootstrap/Scaffolding 佈局篇-->
<div class="container">
  <p><div align="center"><strong><H2>留言板</H2></strong></div><div align="right"></div></p>
  <form id="form1" name="form1" method="post" action="<?=$link;?>index1.php/gbook/AddGbook" class="form-inline">
    <table width="1200" border="1"  class="table table-bordered table-condensed">
      <tr>
        <td id="color"><div id="heig"><strong>留言人:</strong>
            <input type="text" name="user" id="user" />
            <input name="time" type="hidden" id="time" value="<?=date('Y/m/d');?>" />
            <strong>驗證碼:</strong>
            <label>
              <input type="text" name="chck" id="chck" />
            </label>
            <img src="<?=$link;?>index1.php/gbook/Keycik"/><strong>
            </strong></div>
        </td>
        <td id="color"><strong>總共留言數量:
            <?=$total;?>
        </strong></td>
        <td id="color"><div align="center">
          <button class="btn" type="submit">留言</button>
        </div></td>
      </tr>
      <tr>
        <td colspan="3"><label>
          <div align="center">
            <textarea name="content" id="content" rows="3" class="span10"></textarea>
          </div>
        </label></td>
      </tr>
    </table>
  </form>
  <?php foreach($query->result_array() as $row):?>
  <table width="1200" border="1" class="table table-bordered table-condensed">
    <tr>
      <td id="color"><strong>
        <?=$row['user'];?>
      在  <?=$row['time'];?>  留言說:</strong></td>
    </tr>
    <tr>
      <td><?=$row['content'];?></td>
    </tr>
  </table>
  <?php endforeach; ?>
  <p> </p>
</div>

在models/gbook_sql.php 新增兩個方法

    //資料總數
    function total($table){
        $query = $this->db->count_all($table);
        return $query;
    }

    /*
     * 注意count_all只能針對沒有條件的,有條件的請使用count_all_results()
     * 條件 where like 等等
     */
   
    //排序
    function all_order($table,$title,$class){
        $query = $this->db->order_by($title,$class)->get($table);
        return $query;
    }

OK 留言版結束!!
明天來總整理~~~!
待續....


上一篇
[鐵鷹練成]CI之留言版[驗證碼]
下一篇
[鐵鷹練成]CI之留言版[方法總整理]
系列文
[鐵鷹練成]CodeIgniter + Bootstrap31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言