• 技术文章 >后端开发 >php教程

    php实现购物车功能(下)_PHP

    2016-05-28 11:49:37原创283
    接着上篇继续学习: 《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 ."//m.sbmmt.com/m/article/\" was added to the database.

    "; } else { echo "

    Category \"". $catname ."//m.sbmmt.com/m/article/\" 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 ""; } ?>
    Category Name:
    align="center"> <?php if($edit) { echo ""; } ?>
    <?php } function display_book_form($book = '') //显示图书表单 { //如果传入图书存在,进入编辑模式 $edit = is_array($book); ?>
    <?php if ($edit) { echo ""; } ?>
    ISBN:
    Book Title:
    Book Author:
    Category:
    Price:
    Description:
    align="center"> <?php if ($edit) echo "";?>
    <?php } function display_password_form() //显示更改密码表单 { ?>
    Old password:
    New password:
    Repeat new password:

    <?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购物车系统。我们还可以对它进行许多改进和提高:

    以上就是php实现购物车功能的全部代码,希望对大家的学习有所帮助。

    源码下载:购物车

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php 购物车
    上一篇:解析WordPress中的post_class与get_post_class函数_PHP 下一篇:使用PHP实现微信摇一摇周边红包_PHP
    PHP编程就业班

    相关文章推荐

    • 解说PHP框架 (1) 基本概念 • 用过WordPress,对WordPress插件熟习的前辈请进 • 逻辑或 题一个!解决方案 • 关于HTML登陆界面的有关问题。 • (转)PHP缓存的兑现

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网