:::

10-1 顯示單筆資料

二、 顯示單筆資料

  1. 流程加入編號判斷
    //index.php
    /********************流程判斷*********************/
    // 變數過濾
    /****省略******/
    $sn = isset($_REQUEST['sn']) ? (int) $_REQUEST['sn'] : "";
    switch ($op) {
        /****省略******/
    
        default:
            if (empty($sn)) {
                //列出所有事項
                list_all();
            } else {
                $content = find_one($sn);
                $op      = 'show_one';
            }
            break;
    }

     

  2. 改寫 find_one();
    //以流水號取得某筆資料
    function find_one($sn = "")
    {
        global $db;
    
        if (empty($sn)) {
            die(redirect_page($db->error));
        }
    
        $sql = "select * from list where `sn` = '{$sn}'";
    
        if (!$result = $db->query($sql)) {
            die(redirect_page($db->error));
        }
    
        $data = $result->fetch_assoc();
        if (empty($data)) {
            redirect_page('無此編號', 'index.php');
    
        } else {
            // 複選框$data['assign']
            $data['assign_arr'] = explode(';', $data['assign']);
            // die(var_dump($data));
            return $data;
        }
    
    }
  3. template/show_one.tpl
    <div class="container">
      <div class="jumbotron">
        <h2 class="text-center"><a href="{$action}"><i class="fa fa-reply"></i> {$content.title}</a></h2>
    
        <div class="alert alert-warning text-right">
          是否完成:
          {if $content.done}
              <span class="badge badge-success text-center"><i class="fas fa-check"></i></span>
          {else}
              <span class="badge badge-danger text-center"><i class="fas fa-times-circle"></i></span>
          {/if}
        </div>
    
        <div class="row">
            <label class="col-sm-3 text-right">
                描述
            </label>
            <div class="col-sm-9">
                {$content.directions|nl2br}
            </div>
        </div>
    
        <div class="row">
          <label class="col-sm-3 text-right">
            到期日
          </label>
          <div class="col-sm-9">
              {$content.end}
          </div>
        </div>
    
        <div class="row">
          <label class="col-sm-3 text-right">
            優先順序
          </label>
          <div class="col-sm-9">
              {$content.priority}
          </div>
        </div>
    
        <div class="row">
          <label class="col-sm-3 text-right">
            指派對象
          </label>
          <div class="col-sm-9">
              {$content.assign}
          </div>
        </div>
    
        <div class="row">
          <label class="col-sm-3 text-right">
            建立時間
          </label>
          <div class="col-sm-9">
              {$content.create_time}
          </div>
        </div>
    
        <div class="row">
          <label class="col-sm-3 text-right">
            最後更新時間
          </label>
          <div class="col-sm-9">
              {$content.update_time}
          </div>
        </div>
    
        <div class="text-right">
          <a href="{$action}?op=post_form&sn={$content.sn}" class="btn btn-warning">編輯</a>
          <a href="{$action}" class="btn btn-success">回首頁</a>
      </div>
      </div>
    </div>