:::

21-1 index.php

<?php
  //連線資料庫
  link_db("localhost" , "root" , "12345" , "tad");
  
  //針對 op 變數進行整理
  $op = isset($_REQUEST['op'])?$_REQUEST['op']:"";
  $sn = isset($_REQUEST['sn'])?intval($_REQUEST['sn']):"";   //intval() 強制轉成數字
  
  //流程控制
  switch($op){
  
    //儲存榮譽榜資料
    case "insert":
      insert();
      header("location:index.php");
    break;
    
    //發布表單
    case "post_form":
      $main = post_form($sn);
    break;
    
    //刪除
    case "delete":
      delete($sn);
      header("location:index.php");
    break;
    
    //更新榮譽榜資料
    case "update":
      update($sn);
      header("location:index.php");
    break;
    
    //列出所有榮譽榜
    default:
      $main = list_all();
    break;
  }  
  
?>

<!DOCTYPE html>
<html>
  <head>
    <title>榮譽榜發布</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
  </head>
    
  <body>
    <div class="row-fluid">
    
      <div class="span3"></div>
      
      <div class="span6">
        <?php echo $main;?>
      </div>
      
      <div class="span3"></div>
      
    </div>
    
    

    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

<?php
  //連線資料庫
  function link_db($location="localhost" , $id="" ,$passwd="" , $db=""){
    $link = mysql_connect($location , $id , $passwd);
    if($link){
      mysql_query("SET NAMES 'utf8'");
      mysql_select_db($db);
      return $link;
    }else{
      die("連線失敗!");
    }
  }

  //新增到資料庫  
  function insert(){
  
    $award_winner = addslashes($_POST['award_winner']);
    $award_date = addslashes($_POST['award_date']);
    $award_kind = addslashes($_POST['award_kind']);
    $award_content = addslashes($_POST['award_content']);
    $award_enable = addslashes($_POST['award_enable']);
    
    $sql = "INSERT INTO `honor`(`award_winner`, `award_date`, `award_kind`, `award_content`, `award_enable`, `post_date`) VALUES ('{$award_winner}' , '{$award_date}' , '{$award_kind}' , '{$award_content}' , '{$award_enable}' , now())";
    
    mysql_query($sql) or die(mysql_error());
  
  }
  
  //更新到資料庫  
  function update($sn=""){
  
    $award_winner = addslashes($_POST['award_winner']);
    $award_date = addslashes($_POST['award_date']);
    $award_kind = addslashes($_POST['award_kind']);
    $award_content = addslashes($_POST['award_content']);
    $award_enable = addslashes($_POST['award_enable']);
    

    $sql = "UPDATE `honor` SET 
    `award_winner` = '{$award_winner}',
    `award_date` = '{$award_date}',
    `award_kind` = '{$award_kind}',
    `award_content` = '{$award_content}',
    `award_enable` = '{$award_enable}',
    `post_date`= now() 
    WHERE `sn`= '{$sn}'";
    
    mysql_query($sql) or die(mysql_error());
  
  }
  
  //列出所有內容
  function list_all(){
  
    $sql = "SELECT * FROM `honor` where `award_enable` = '1' order by `award_date` desc ";
    $result = mysql_query($sql) or die(mysql_error());
    
    
    $table_content = "";
    while($data = mysql_fetch_assoc($result)){
    
      //將換行轉為<br>
      $award_content=nl2br($data['award_content']);
    
      $table_content .= "
      <tr>
      <td>{$data['award_winner']}</td>
      <td>{$data['award_date']}</td>
      <td>{$award_content}</td>
      <td nowrap>
        <a href='index.php?op=delete&sn={$data['sn']}' class='btn btn-danger btn-mini'>刪除</a>
        <a href='index.php?op=post_form&sn={$data['sn']}' class='btn btn-warning btn-mini'>修改</a>
      </td>
     </tr>
     ";  
    }
  
    $main="
    <h1>
    <img src='img/gold.png'>榮譽榜
    <a href='index.php?op=post_form' class='btn btn-info'>發布</a>
    </h1>
    
    <table class='table table-striped table-bordered'>
    <tr>
      <th>獲獎者</th>
      <th>得獎日期</th>
      <th>事由</th>
      <th nowrap>功能</th>
    </tr>
    $table_content
    </table>
    
    ";
    
    return $main;
  }
  
  
  //發布表單
  function post_form($sn=""){
     
    $sql = "SELECT * FROM `honor` where `sn` = '{$sn}'";
    $result = mysql_query($sql) or die(mysql_error());
    $data = mysql_fetch_assoc($result);
    
    $date=empty($sn)?date("Y-m-d"):$data['award_date'];
    
    //還原下拉選單預設值
    $selected_0 = ($data['award_kind']== "校內") ? "selected" : "";
    $selected_1 = ($data['award_kind']== "校外") ? "selected" : "";
    
    //還原單選鈕預設值
    $award_enable0 = ($data['award_enable']!= "1") ? "checked" : "";
    $award_enable1 = ($data['award_enable']== "1") ? "checked" : "";
    
    $op = empty($sn)? "insert" : "update";
    
    $form="
    <h1 class='text-center'>榮譽榜發布</h1>
        
    <form action='index.php' method='post' class='form-horizontal'>
    <div class='well'>
      <div class='control-group'>
        <label class='control-label' for='award_winner'>得獎者名稱:</label>
        <div class='controls'>
          <input type='text' id='award_winner' name='award_winner' value='{$data['award_winner']}' class='span12' placeholder='請輸入得獎者名稱'>
        </div>
      </div>

      <div class='control-group'>
        <label class='control-label' for='award_date'>得獎日期:</label>
        <div class='controls'>
          <input type='text' id='award_date' name='award_date' value='{$date}' class='span12' placeholder='得獎日期格式:2013-07-09'>
        </div>
      </div>

      <div class='control-group'>
        <label class='control-label' for='award_kind'>類別:</label>
        <div class='controls'>
          <select name='award_kind' id='award_kind' class='span12'>
            <option value='校內' $selected_0>校內</option>
            <option value='校外' $selected_1>校外</option>
          </select>
        </div>
      </div>

      <div class='control-group'>
        <label class='control-label' for='award_content'>得獎事由:</label>
        <div class='controls'>
          <textarea name='award_content' id='award_content' rows='4' class='span12'>{$data['award_content']}</textarea>
        </div>
      </div>

      <div class='control-group'>
        <label class='control-label' for='award_enable'>是否發布:</label>
        <div class='controls'>

          <label class='radio'>
            <input type='radio' name='award_enable' id='award_enable1' value='1' {$award_enable1}>是,立即發布
          </label>

          <label class='radio'>
            <input type='radio' name='award_enable' id='award_enable0' value='0' {$award_enable0}>否,暫時隱藏
          </label>


          <input type='hidden' name='op' value='{$op}'>
          <input type='hidden' name='sn' value='{$sn}'>
          <a href='index.php' class='btn'>回上頁</a>
          <input type='submit' value='送出' class='btn btn-primary'>
        </div>
      </div>
    </div>
  </form>
  ";
    
    return $form;  
  }
  
  
  //刪除 
  function delete($sn=""){
      
    $sql = "DELETE FROM `honor` WHERE `sn` = '{$sn}'";
    
    mysql_query($sql) or die(mysql_error());
  
  }
?> 

 


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp22.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D846%26tbsn%3D19

書籍目錄

展開 | 闔起

線上使用者

377人線上 (150人在瀏覽線上書籍)

會員: 0

訪客: 377

更多…