php实现购物车功能(上)(2)

function display_categories($cat_array) //输出目录 { if(!is_array($cat_array)) { echo "<p>No categories currently available</p>"; return; } echo "<ul>"; foreach($cat_array as $row) { $url = "show_cat.php?catid=". $row['catid']; $title = $row['catname']; echo "<li>"; do_html_URL($url,$title); echo "</li>"; } echo "</ul>"; echo "<hr/>"; }

4.4 db_fns.php文件中的函数db_result_to_array()

function db_result_to_array($result) //结果到数组 { $res_array = array(); for($count = 0; $row = $result ->fetch_assoc(); $count++) $res_array[$count] = $row; return $res_array; }


Internet目录下的所有图书
 

由以下代码实现:

4.5 show_cat.php

<?php /** * @author switch * @copyright 2015 * 显示特定目录包含的所有图书 */ //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once('book_sc_fns.php'); session_start(); @$catid = $_GET['catid']; $name = get_category_name($catid); do_html_header($name); $book_array = get_books($catid); display_books($book_array); //如果是管理员,显示管理界面按钮 if(isset($_SESSION['admin_user'])) { display_button("index.php","continue","Continue Shopping"); display_button("admin.php","admin-menu","Admin Menu"); display_button("edit_category_form.php?catid=". $catid,"edit-category","Edit Category"); } else //否则显示主界面按钮 { display_button("index.php","continue-shopping","Continue Shopping"); } do_html_footer(); ?>

4.6 book_fns.php文件中的函数get_category_name()

function get_category_name($catid) //获取目录名 { $conn = db_connect(); //连接数据库 $query = "select catname from categories where catid = '". $catid ."'"; $result = @$conn ->query($query); if(!$result) //查询失败,原因为查询出错 return false; $num_cats = @$result ->num_rows; if($num_cats == 0) //查询失败,原因为无目录 return false; $row = $result ->fetch_object(); return $row ->catname; }

4.8 book_fns.php文件中的函数get_books()

function get_books($catid) //从数据库中获取图书 { if((!$catid) || ($catid == '')) //如果目录ID为空 return false; $conn = db_connect(); $query = "select * from books where catid = '". $catid ."'"; $result = @$conn ->query($query); if(!$result) //查询失败,原因为查询出错 return false; $num_books = @$result ->num_rows; if($num_books == 0) //查询失败,原因为无图书 return false; $result = db_result_to_array($result); return $result; }

4.9 output_fns文件中的函数display_books()

function display_books($book_array) //输出图书 { if(!is_array($book_array)) echo "<p>No books currently available in this category</p>"; else //有图书,建表 { echo "<table width = \"100%\" border=https://www.jb51.net/article/\"0\">"; foreach($book_array as $row) { $url = "show_book.php?isbn=". $row['isbn']; echo "<tr><td>"; // 如果图片存在 if(@file_exists("images/". $row['isbn'] .".jpg")) { $title = "<img src=https://www.jb51.net/article/\"images/". $row['isbn'] .".jpg\" style=https://www.jb51.net/article/\"border: 1px solid black\"/>"; do_html_URL($url,$title); } else echo " "; echo "</td><td>"; $title = $row['title'] ." by ". $row['author']; do_html_URL($url,$title); echo "</td></tr>"; } echo "</table>"; } echo "<hr/>"; }


PHP and MySQL Web Development的详细信息

由以下代码实现:

4.10 show_book.php

<?php /** * @author switch * @copyright 2015 * 显示特定图书的详细信息 */ //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once('book_sc_fns.php'); session_start(); $isbn = $_GET['isbn']; $book = get_book_details($isbn); do_html_header($book['title']); display_book_details($book); //设置继续按钮 $target = "index.php"; if($book['catid']) $target = "show_cat.php?catid = ". $book['catid']; //如果是管理员,显示编辑链接 if(check_admin_user()) { display_button("edit_book_form.php?isbn=". $isbn,"edit-item","Edit Item"); display_button("admin.php","admin-menu","Admin Menu"); display_button($target,"continue","Continue"); } else { display_button("show_cart.php?new=". $isbn,"add-to-cart","Add". $book['title']." To My Shopping Cart"); display_button($target,"continue-shopping","Continue Shopping"); } do_html_footer(); ?>

4.11 book_fns.php文件中的函数get_book_details()

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/233b49197e1240bf53f3b28b4b1c2f2e.html