:::

8-2 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"><i class="fa fa-pencil"></i> 發布新聞</a>';
  $error_msg=$news_list="";

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

    case 'db_error':
      $error_msg=error_msg("資料庫連線錯誤!!請檢查資料庫帳號、密碼是否正確!");
      break;

    case 'view':
      //列出單一內容
      $news_list=view($sn);
      $toolbar.="<a href='{$_SERVER['PHP_SELF']}?g2p={$g2p}' class='btn btn-success btn-block'><i class='fa fa-home'></i> 回新聞列表</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(){
    global $g2p;
    include_once "PageBar.php";

    link_db();

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

    $sql="select a.*,b.cate_title from eznews as a
    left join eznews_cate as b on a.cate_sn=b.cate_sn
    order by a.status='置頂' desc, a.post_time desc";

    $PageBar = new PageBar($sql, 10);
    $bar_arr=$PageBar->makeBar();
    $sql=$bar_arr['sql'];
    $bar=$bar_arr['bar'];

    //傳回值存到 $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>
    $bar
    <table class='table table-striped table-bordered'>
    <tr>
      <th class='col-md-2 text-center'>分類</th>
      <th class='col-md-5 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'];
      $top_label=$news['status']=="置頂"?"<span class='label label-danger'>置頂</span>":"";

      $color=$news['status']=="高亮"?"class='warning'":"";

      $news_list.="
      <tr $color>
        <td>
          {$news['cate_title']}
        </td>
        <td>
          <span class='label label-info'>{$news['counter']}</span>
          {$top_label}
          <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>
    $bar";

    return $news_list;
  }

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

    $sql="update `eznews` set `counter` = `counter` + 1 where sn='{$sn}'";
    mysql_query($sql) or die("{$sql}<br>".mysql_error());

    //讀取eznews資料表所有欄位,並指定某一筆特定資料
    $sql="select a.*,b.* from eznews as a
    left join eznews_cate as b on a.cate_sn=b.cate_sn
    where a.sn='$sn'";

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

    $news_content=$news['news_content'];

    //抓附檔
    $sql="select * from eznews_files where sn={$sn}";
    $result=mysql_query($sql) or die("{$sql}<br>".mysql_error());
    $files_list="";
    while($file=mysql_fetch_assoc($result)){
      //判斷是否為圖檔
      if(strpos($file['file_type'], "image")!==false){
        $show_file="<img src='uploads/{$file['file_new_name']}' style='width: 120px'>";
      }else{
        $show_file=$file['file_name'];
      }

      $files_list.="<li><a href='uploads/{$file['file_new_name']}' target='_blank'>{$show_file}</a></li>";
    }

    $one_news="<h1><span class='label label-info'>{$news['cate_title']}</span> {$news['news_title']}</h1>

    點閱數:{$news['counter']}

    <div class='well'>
      {$news_content}
    </div>
    $files_list
    ";

    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%2Ftad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1098%26tbsn%3D34

書籍目錄

展開 | 闔起

線上使用者

71人線上 (16人在瀏覽線上書籍)

會員: 0

訪客: 71

更多…