PHP和MySQL Web開發從新手到高手,第8天-建立categories管理頁面

來源:互聯網
上載者:User

標籤:

1. 建立categories管理頁面

  主要包含以下幾個頁面:

  A. index.php, 準備各種變數資料.供展示頁面使用.

  B. categories.html.php, 顯示categories.

  C. form.html.php, 用於編緝或添加作者的頁面.

頁面郊果:

 

2. categories頁面的主要流程

2.1 是否已登入

if (!user_is_login()){      include ‘../login.html.php‘;      exit();  }

2.2 是否有許可權

if (!user_has_role(‘Content Editor‘)){      $error = ‘only Content Editor may access this page...‘;      include ‘../access.denied.html.php‘;      exit();  }

2.3 添加分類目錄

// add categories  if (isset($_GET[‘add‘])) {      include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/db.inc.php‘;      try {            $pageTitle = ‘Add category‘;          $action = ‘addCategory‘;          $categoryText = ‘‘;                    $id = ‘‘;          $button = ‘Add category‘;                        include ‘form.html.php‘;          exit();        } catch (PDOException $e) {          $error = ‘Error getting list of jokes to edit...‘.$e->getMessage();          include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/error.html.php‘;          exit();      }  }    // add categories  if (isset($_GET[‘addCategory‘]) && $_POST[‘categoryText‘] != ‘‘) {      include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/db.inc.php‘;             try {          $sql = ‘INSERT INTO category(name) VALUES(:categoryText);‘;          $s = $pdo->prepare($sql);                        $s->bindValue(‘:categoryText‘, $_POST[‘categoryText‘]);            $s->execute();      } catch (PDOException $e) {          $error = ‘Error adding category...‘.$e->getMessage();          include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/error.html.php‘;          exit();      }      header(‘Location: .‘);      exit();  }

2.4 編緝分類目錄

// edit categories  if (isset($_POST[‘action‘]) and $_POST[‘action‘] == ‘Edit‘) {      include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/db.inc.php‘;      try {          $sql = ‘SELECT name FROM category WHERE id = :id‘;          $s = $pdo->prepare($sql);          $s->bindValue(‘:id‘, $_POST[‘id‘]);          $s->execute();            $row = $s->fetch();            $pageTitle = ‘Edit category‘;          $action = ‘editCategory‘;                    $categoryText = $row[‘name‘];                    $id = $_POST[‘id‘];          $button = ‘Update category‘;          include ‘form.html.php‘;          exit();        } catch (PDOException $e) {          $error = ‘Error getting list of category to edit...‘.$e->getMessage();          include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/error.html.php‘;          exit();      }  }    // edit categories  if (isset($_GET[‘editCategory‘]) && $_POST[‘categoryText‘] != ‘‘) {      include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/db.inc.php‘;            try {          $sql = ‘UPDATE category SET              name = :name              WHERE id = :id;‘;          $s = $pdo->prepare($sql);                     $s->bindValue(‘:name‘, $_POST[‘categoryText‘]);          $s->bindValue(‘:id‘, $_POST[‘id‘]);                    $s->execute();      } catch (PDOException $e) {          $error = ‘Error updating categories...‘.$e->getMessage();          include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/error.html.php‘;          exit();      }            header(‘Location: .‘);      exit();  }
2.5 刪除分類目錄
if (isset($_POST[‘action‘]) and $_POST[‘action‘] == ‘Delete‘) {            include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/db.inc.php‘;            // delete jokecategory entries      try {          $sql = ‘DELETE FROM jokecategory WHERE categoryid = :id‘;          $s = $pdo->prepare($sql);          $s->bindValue(‘:id‘, $_POST[‘id‘]);          $s->execute();                } catch (PDOException $e) {          $error = ‘Error deleting jokecategory...‘.$e->getMessage();          include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/error.html.php‘;          exit();      }                  // delete category entries      try {          $sql = ‘DELETE FROM category WHERE id = :id‘;          $s = $pdo->prepare($sql);          $s->bindValue(‘:id‘, $_POST[‘id‘]);          $s->execute();      } catch (PDOException $e) {          $error = ‘Error deleting category...‘.$e->getMessage();          include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/error.html.php‘;          exit();      }      header(‘Location: .‘);      exit();  }

2.6 顯示分類目錄

// show all categories in database...  try {      include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/db.inc.php‘;      $querySql = "SELECT id, name FROM category;";      $result = $pdo->query($querySql);            foreach ($result as $row) {          $categories[] = array(‘id‘=>$row[‘id‘], ‘name‘=>$row[‘name‘]);                }                      //if (count($categories) > 0)       {          $sql = "SELECT COUNT(*) AS RCT FROM category;";          $s = $pdo->prepare($sql);          $s->execute();          $row = $s->fetch();          $rowCount = $row[‘RCT‘];          include ‘categories.html.php‘;      }   } catch (PDOException $e) {      $error = "query category failed...".$e->getMessage();            include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHP_MySQL_Web/includes/error.html.php‘;      exit();  }

PHP和MySQL Web開發從新手到高手,第8天-建立categories管理頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.