php實現購物車功能(下),php實現購物車功能
接著上篇繼續學習: 《php實現購物車的功能(上)》
7、實現一個管理介面
登入介面
由以下代碼實現:
7.1 admin.php
<?php /** * @author switch * @copyright 2015 * 主管理菜單 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含。 require_once('book_sc_fns.php'); session_start(); if((@$_POST['username']) && (@$_POST['passwd'])) //嘗試登陸 { $username = $_POST['username']; $passwd = $_POST['passwd']; if(login($username,$passwd)) { $_SESSION['admin_user'] = $username; } else { do_html_header("Problem:"); echo "You could not be logged in.
You must be logged in to view this page.
"; do_html_URL('login.php','Login'); do_html_footer(); exit; } } do_html_header("Administration"); if(check_admin_user()) { display_admin_menu(); } else { echo "You are not authorized to enter the administration area.
"; do_html_URL('login.php','Login'); } do_html_footer(); ?>
7.2 user_auth_fns.php檔案中的函數login()
function login($username,$password) //登入 { $conn = db_connect(); //串連資料庫 if(!$conn) return 0; //檢查使用者名稱唯一性 $query = "select * from admin where username='". $username ."' and password = sha1('". $password ."')"; $result = $conn ->query($query); if(!$result) return 0; if($result ->num_rows > 0) return 1; else return 0; }
7.3 user_auth_fns.php檔案中的函數check_admin_user()
function check_admin_user() //檢查是否是管理員 { if(isset($_SESSION['admin_user'])) return true; else return false; }
管理主介面
由以下代碼實現:
7.4 output_fns.php檔案中的函數display_admin_menu()
function display_admin_menu() //輸出管理員菜單 { ?>
Go to main site
Add a new category
Add a new book
Change admin password
<?php } function display_button($target,$image,$alt) //顯示按鈕 { echo " "; }
目錄添加
目錄添加成功
目錄頁中可以看出多了Novel目錄
由以下代碼實現:
7.5 insert_category_form.php
<?php /** * @author switch * @copyright 2015 * 允許管理員向資料庫中添加一個目錄的表格 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含 require_once('book_sc_fns.php'); session_start(); do_html_header(); if(check_admin_user()) { display_category_form(); do_html_URL("admin.php","Back to administrtion menu"); } else { echo "You are not authorized to enter the administation area.
"; } do_html_footer(); ?>
7.6 insert_category.php
<?php /** * @author switch * @copyright 2015 * 向資料庫中插入新目錄 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含 require_once('book_sc_fns.php'); session_start(); do_html_header("Adding a category"); if(check_admin_user()) { if(filled_out($_POST)) { $catname =$_POST['catname']; if(insert_category($catname)) { echo "Category \"". $catname ."\" was added to the database.
"; } else { echo "Category \"". $catname ."\" could not be added to the database.
"; } } else { echo "You have not filled out the form. Please try again.
"; } do_html_URL("admin.php","Back to administration menu"); } else { echo "You are not authorised to view this page.
"; } do_html_footer(); ?>
管理員目錄介面
目錄編輯介面-可更新,刪除
目錄更新成功
目錄主介面可以看到該目錄更改成功
由以下代碼實現:
7.7 edit_category_form.php
<?php /** * @author switch * @copyright 2015 * 管理員編輯目錄的表單 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含。 require_once('book_sc_fns.php'); session_start(); do_html_header("Edit category"); if(check_admin_user()) { if($catname = get_category_name($_GET['catid'])) { $catid = $_GET['catid']; $cat = compact('catname','catid'); display_category_form($cat); } else { echo "Could not retrieve category details.
"; } do_html_URL("admin.php","Back to administration menu"); } else { echo "You are not authorized to enter the administration area.
"; } do_html_footer(); ?>
7.8 edit_category.php
<?php /** * @author switch * @copyright 2015 * 更新資料庫中的目錄 */ //require_once語句和require語句完全相同,唯一區別是PHP會檢查該檔案是否已經被包含過,如果是則不會再次包含。 require_once('book_sc_fns.php'); session_start(); do_html_header("Updating category"); if(check_admin_user()) { if(filled_out($_POST)) { if(update_category($_POST['catid'],$_POST['catname'])) { echo "Category was updated.
"; } else { echo "Category could not be updated.
"; } } else { echo "you have not filled out the form. Please try again.
"; } do_html_URL("admin.php","Back to administration menu"); } else { echo "You are not authorised to view this page.
"; } do_html_footer(); ?>
7.9 admin_fns.php
<?php /** * @author switch * @copyright 2015 * 管理指令碼使用的函數集合 */ function display_category_form($category = '') //顯示目錄表單 { //如果傳入存在目錄,進入編輯模式 $edit = is_array($category); ?> <?php if($edit) //允許刪除存在目錄 { echo " "; } ?> <?php } function display_book_form($book = '') //顯示圖書表單 { //如果傳入圖書存在,進入編輯模式 $edit = is_array($book); ?> <?php if ($edit) { echo " "; } ?> <?php } function display_password_form() //顯示更改密碼錶單 { ?>
<?php } function insert_category($catname) //目錄插入 { $conn = db_connect(); //資料庫連接 $query = "select * from categories where catname='". $catname ."'"; $result = $conn ->query($query); if((!$result) || ($result ->num_rows != 0)) return false; $query = "insert into categories values ('','". $catname ."')"; $result = $conn ->query($query); if(!$result) return false; else return true; } function insert_book($isbn,$title,$author,$catid,$price,$description) //圖書插入 { $conn = db_connect(); //串連資料庫 $query = "select * from books where isbn='". $isbn ."'"; $result = $conn ->query($query); if((!$result) || ($result ->num_rows != 0)) return false; $query = "insert into books values ('". $isbn ."','". $author ."','". $title ."', '". $catid ."','". $price ."','". $description ."')"; $result = $conn ->query($query); if(!$result) return false; else return true; } function update_category($catid,$catname) //更改目錄名稱 { $conn = db_connect(); //串連資料庫 $query = "update categories set catname='". $catname ."' where catid='". $catid ."'"; $result = @$conn ->query($query); if(!$result) return false; else return true; } function update_book($oldisbn,$isbn,$title,$author,$catid,$price,$description) { $conn = db_connect(); //串連資料庫 $query = "update books set isbn='". $isbn ."', title='". $title ."', author='". $author ."', catid='". $catid ."', price ='". $price ."', description='". $description ."' where isbn='". $oldisbn ."'"; $result = @$conn ->query($query); if(!$result) return false; else return true; } function delete_category($catid) //刪除目錄 { $conn = db_connect(); //串連資料庫 $query = "select * from books where catid='". $catid ."'"; $result = @$conn ->query($query); if((!$result) || (@$result ->num_rows > 0)) //如果該目錄有圖書,無法刪除該目錄 return false; $query = "delete from categories where catid='". $catid ."'"; $result = @$conn ->query($query); if(!$result) return false; else return true; } function delete_book($isbn) //刪除圖書 { $conn = db_connect(); //串連資料庫 $query = "delete from books where isbn='". $isbn ."'"; $result = @$conn ->query($query); if(!$result) return false; else return true; } ?>
7.10 目錄刪除操作,圖書添加,更新,刪除操作基本與上述操作差不多,這裡就不在示範,可以下載代碼查看
8、擴充
本項目建立了一個相當簡單的PHP購物車系統。我們還可以對它進行許多改進和提高:
- 在真正的線上商店,可能必須建立一些訂單記錄和實施系統——在這個系統中,使用者無法看到已經預定了的訂單。
- 顧客希望在不必與我們聯絡的前提下就能檢查到他們的訂單處理情況。使用者應當可以通過一種身分識別驗證方式使之能夠查看自己以前的訂單,並且也可以將操作與個人情況緊密地結合起來。也更方便我們收集一些使用者習慣資訊。
- 圖書的圖片可以通過FTP之類的服務傳輸到該網站的映像目錄並給它們取一個合適的名字。可以把檔案上傳到圖片插入頁,以使該操作方便一些。
- 可以添加使用者登入、個人化以及書目推薦、線上評論、會員制度、庫存層級檢查等。可以添加的功能是非常多的。
以上就是php實現購物車功能的全部代碼,希望對大家的學習有所協助。
源碼下載:購物車
您可能感興趣的文章:
- php 購物車的例子
- php 購物車執行個體(申精)
- php購物車實現代碼
- php網上商城購物車設計代碼分享
- PHP更新購物車數量(表單部分/PHP處理部分)
- 深入PHP購物車模組功能分析(函數講解,附源碼)
- php 購物車完整實現代碼
- PHP實現的比較完善的購物車類
- php利用cookies實現購物車的方法
- php購物車實現方法
http://www.bkjia.com/PHPjc/1088772.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1088772.htmlTechArticlephp實現購物車功能(下),php實現購物車功能 接著上篇繼續學習: 《php實現購物車的功能(上)》 7、實現一個管理介面 登入介面 由以下...