:::

5-1 index.php

<?php

  /*----引入檔案----*/
  require_once "config.php";
  require_once "function.php";

  /*----整理傳進來的變數或變數初始值----*/
  $op=isset($_REQUEST['op'])?$_REQUEST['op']:"";
  $sn=isset($_REQUEST['sn'])?intval($_REQUEST['sn']):"";
  $toolbar='<a href="post.php" class="btn btn-primary btn-block">發布新聞</a>';


  /*----流程控制----*/
  switch ($op) {
    case 'view':
      //列出單一內容
      $news_list=view($sn);
      $toolbar.="<a href='{$_SERVER['PHP_SELF']}' class='btn btn-success btn-block'>回新聞列表</a>";
      break;

    case 'delete':
      //刪除單一內容
      delete($sn);
      header("location: {$_SERVER['PHP_SELF']}");
      break;

    default:
      //列出所有內容
      $news_list=list_news();
      break;
  }

  /*----輸出----*/
  show_page('list_tpl');




  /*----所有函數----*/

  //列出所有內容
  function list_news(){

    link_db();

    //讀取eznews資料表所有欄位(日期大到小排列)
    $sql="select * from eznews order by post_time desc";

    //傳回值存到 $result 以供抓取資料用
    $result=mysql_query($sql) or die("{$sql}<br>".mysql_error());

    $news_list="
    <script>
    function del_func(sn){
     var sure = window.confirm('確定要刪除此資料?');
     if (!sure) return;
     location.href='index.php?op=delete&sn=' + sn;
    }
    </script>

    <h1>列出所有新聞</h1>
    <table class='table table-striped table-bordered'>
    <tr>
      <th class='col-md-7 text-center'>新聞標題</th>
      <th class='col-md-3 text-center'>發布時間</th>
      <th class='col-md-2 text-center'>功能</th>
    </tr>

    ";
    //取回資料庫一筆資料,並以欄位名稱為索引的資料陣列
    while($news=mysql_fetch_assoc($result)){

      $title=empty($news['news_title'])?"無標題":$news['news_title'];

      $news_list.="
      <tr>
        <td>
          <a href='{$_SERVER['PHP_SELF']}?op=view&sn={$news['sn']}'>{$title}</a>
        </td>
        <td class='text-center'>{$news['post_time']}</td>
        <td class='text-center'>
          <a href='javascript:del_func({$news['sn']})' class='btn btn-xs btn-danger'>刪除</a>
          <a href='post.php?op=modify&sn={$news['sn']}' class='btn btn-xs btn-warning'>編輯</a>
        </td>
      </tr>
      ";
      //$news_list=$news_list."<li>{$news['news_title']}</li>";
    }
    $news_list.="</table>";

    return $news_list;
  }

  //顯示單一頁面
  function view($sn=""){
    link_db();

    //讀取eznews資料表所有欄位,並指定某一筆特定資料
    $sql="select * from eznews where sn='$sn'";

    //傳回值存到 $result 以供抓取資料用
    $result=mysql_query($sql) or die("{$sql}<br>".mysql_error());
    $news=mysql_fetch_assoc($result);

    $news_content=nl2br($news['news_content']);

    $one_news="<h1>{$news['news_title']}</h1>
    <div class='well'>
      {$news_content}
    </div>";

    return $one_news;
  }

  //刪除單一頁面
  function delete($sn=""){
    link_db();

    //刪除指定某一筆特定資料
    $sql="delete from eznews where sn='$sn'";

    mysql_query($sql) or die("{$sql}<br>".mysql_error());

  }
?>

 


:::

搜尋

QR Code 區塊

https%3A%2F%2Fwww.tad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D34%26tbdsn%3D1064

書籍目錄

展開 | 闔起

線上使用者

95人線上 (24人在瀏覽線上書籍)

會員: 0

訪客: 95

更多…