:::

14-1 上課範例:index.php(完成版)

<?php
//引入共同檔
require_once "header.php";

//變數初始化
$op=isset($_REQUEST['op'])?$_REQUEST['op']:"";
$sn= isset($_REQUEST['sn'])? intval($_REQUEST['sn']) : "";

//流程控制
switch($op){

  case "insert":
  insert_article();
  header("location:{$_SERVER['PHP_SELF']}");
  break;
  
  case "edit":
  $main=article_form($sn);
  break;
  

  case "update":
  update_article($sn);
  header("location:{$_SERVER['PHP_SELF']}");
  break;

  case "delete":
  delete_article($sn);
  header("location:{$_SERVER['PHP_SELF']}");
  break;

  default:
  $main=empty($sn)?list_article():show_article($sn);
  break;
}


//套用樣板
theme("theme.html");

/*************** 功能函數區 **************/

//秀出某一篇文章
function show_article($sn=null){
  $now_seme=get_seme();

  $sql="update `tncomu_article` set `counter`=`counter`+1 where sn='$sn'";
  mysql_query($sql) or die("無法執行:".mysql_error());

  //設定SQL語法
  $sql="select a.* , b.class_name
  from `tncomu_article` as a left join `tncomu_class` as b on a.class_sn=b.class_sn
  where a.enable='1' and a.sn='$sn'";

  //執行SQL語法
  $result = mysql_query($sql) or die("無法執行:".mysql_error());

  $data=mysql_fetch_assoc($result);
  
  $data['content']=nl2br($data['content']);
  
  $main="
  <h1>「{$data['stud_name']}」的學習收藏</h1>
  <div style='text-align:right;margin:10px 0px;'>{$data['class_name']}</div>
  <div>{$data['content']}</div>
  <div style='text-align:right;margin:10px 0px;'>{$data['post_time']}</div>
  ";
  
  return $main;
}


//列出所有文章
function list_article(){
  $now_seme=get_seme();
  
  //設定SQL語法
  $sql="select a.* , b.class_name
  from `tncomu_article` as a left join `tncomu_class` as b on a.class_sn=b.class_sn
  where a.enable='1' and b.seme='$now_seme'
  order by a.post_time desc";

  //執行SQL語法
  $result = mysql_query($sql) or die("無法執行:".mysql_error());

  $main="
  <script>
  function delete_func(sn){
   var sure = window.confirm('確定要刪除此資料?');
   if (!sure)	return;
   location.href='{$_SERVER['PHP_SELF']}?op=delete&sn=' + sn;
  }
  </script>

  <table>
  <tr>
    <th>所屬班級</th>
    <th>學員姓名</th>
    <th>發布日期</th>
    <th>人氣</th>
    <th>相關功能</th>
  </tr>";

  $i=2;
  while($data=mysql_fetch_assoc($result)){
  
    $color=($i % 2)?"white":"#D0D0D0";
    $i++;
    
    $main.="
    <tr style='background-color:$color;'>
    <td>{$data['class_name']}</td>
    <td><a href='{$_SERVER['PHP_SELF']}?sn={$data['sn']}'>{$data['stud_name']}</a></td>
    <td>{$data['post_time']}</td>
    <td>{$data['counter']}</td>
    <td><a href='javascript:delete_func({$data['sn']})'>刪除</a> | <a href = '{$_SERVER['PHP_SELF']}?sn={$data['sn']}&op=edit' >編輯</a> </td>
    </tr>";
  }

  $main.="</table>";

  return $main;
}

//輸入學習收藏的表單
function article_form($sn=''){

  $next_op="insert";

  //初始值設定
  $data['stud_name'] = $data['class_sn'] = $data['content'] = $data['enable'] = "";

  if($sn){
   //設定SQL語法
    $sql="select * from `tncomu_article` where sn='{$sn}'";

    //執行SQL語法
    $result=mysql_query($sql) or die("無法執行:".mysql_error());

    //擷取資料回來存到 $data
    $data=mysql_fetch_assoc($result);

    //還原下拉選單預設值

    $radio1=($data['enable']=="1")?"checked":"";
    $radio0=($data['enable']=="0")?"checked":"";
    $next_op="update";
  }


  $now_seme=get_seme();

  $sql="select class_sn,class_name from tncomu_class where access='1' and seme='{$now_seme}' ";
  $result=mysql_query($sql) or die($sql);
  $opt="";
  while(list($class_sn , $class_name) = mysql_fetch_row($result)){
    $selected = ($class_sn == $data['class_sn'])?"selected":"";
    $opt.="<option value='$class_sn' $selected>$class_name</option>";
  }


  $main="<h3 style='color:#0066CC'>輸入學習收藏</h3>
  <form action='{$_SERVER['PHP_SELF']}' method='post'>
    <table>
      <tr>
        <th>您的姓名:</th>
        <td><input type='text' name='stud_name' size='10' value='{$data['stud_name']}'></td>
        <th>{$now_seme}班級:</th>
        <td>
          <select name='class_sn'>
            <option value=''>請選擇{$now_seme}班級</option>
            $opt
          </select>
        </td>
      </tr>
      <tr>
        <th>學習收藏內容</th>
        <td colspan=3><textarea name='content' cols=50 rows=8>{$data['content']}</textarea></td>
      </tr>
      <tr>
        <th>設定密碼:</th>
        <td><input type='text' name='text_passwd' size='10'></td>
        <th>是否發布?</th>
        <td>
          <input type='radio' name='enable' value='1' id='enable' $radio1><label for='enable'>發布</label>
          <input type='radio' name='enable' value='0' id='unable' $radio0><label for='unable'>暫不發布</label>
          <input type='hidden' name='sn' value='$sn'>
          <input type='hidden' name='op' value='$next_op'>
          <input type='submit' value='儲存'>
        </td>
      </tr>
    </table>
  </form>
  ";
  return $main;
}


//執行儲存動作
function insert_article(){
  //過濾姓名
  $stud_name=trim($_POST['stud_name']);
  $stud_name=strip_tags($stud_name);
  $stud_name = (! get_magic_quotes_gpc()) ? addslashes($stud_name) : $stud_name;
  $stud_name=htmlspecialchars($stud_name);
  //過濾內容
  $_POST['content'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['content']) : $_POST['content'];
  $_POST['content']=htmlspecialchars($_POST['content']);
  //過濾密碼
  $_POST['text_passwd'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['text_passwd']) : $_POST['text_passwd'];
  
  $class_sn=intval($_POST['class_sn']);

  $sql="INSERT INTO `tncomu_article`(`stud_name`, `content`, `post_time`, `enable`, `class_sn`, `mode`, `text_passwd`) VALUES ('{$stud_name}' , '{$_POST['content']}' , now(), '{$_POST['enable']}', '{$class_sn}', '文字', '{$_POST['text_passwd']}')";
  mysql_query($sql) or die(mysql_error().$sql);
  return "儲存完畢";
}


//執行更新動作
function update_article($sn=''){
  //過濾姓名
  $stud_name=trim($_POST['stud_name']);
  $stud_name=strip_tags($stud_name);
  $stud_name = (! get_magic_quotes_gpc()) ? addslashes($stud_name) : $stud_name;
  $stud_name=htmlspecialchars($stud_name);
  //過濾內容
  $_POST['content'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['content']) : $_POST['content'];
  $_POST['content']=htmlspecialchars($_POST['content']);
  //過濾密碼
  $_POST['text_passwd'] = (! get_magic_quotes_gpc()) ? addslashes($_POST['text_passwd']) : $_POST['text_passwd'];

  $class_sn=intval($_POST['class_sn']);

  $sql="update `tncomu_article` set `stud_name`='{$stud_name}', `content`='{$_POST['content']}', `post_time`=now(), `enable`='{$_POST['enable']}', `class_sn`='{$_POST['class_sn']}' where `sn`='{$sn}'";
  mysql_query($sql) or die(mysql_error().$sql);
  return "儲存完畢";
}

//刪除文章資料
function delete_article($sn=null){

  //設定SQL語法
  $sql="delete from `tncomu_article` where sn='{$sn}'";

  //執行SQL語法
  mysql_query($sql) or die("無法執行:".mysql_error());

  //執行完轉向
  header("location: {$_SERVER['PHP_SELF']}");
}
?>

:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp27.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D571%26tbsn%3D20

書籍目錄

展開 | 闔起

線上使用者

299人線上 (74人在瀏覽線上書籍)

會員: 0

訪客: 299

更多…