PHP之购物车

该文章记录了购物车的实现代码,仅供参考

book_sc_fns.php


output_fns.php


  
  
    <?php echo $title; ?>
    
  
  
  
Bookorama


No categories currently available

"; return; } echo "
    "; foreach ($cat_array as $row) { $url = "show_cat.php?catid=".$row['catid']; $title = $row['catname']; echo "
  • "; do_html_url($url, $title); echo "
  • "; } echo "
"; echo "
"; } function display_books($book_array) { //display all books in the array passed in if (!is_array($book_array)) { echo "

No books currently available in this category

"; } else { //create table echo ""; //create a table row for each book foreach ($book_array as $row) { $url = "show_book.php?isbn=".$row['isbn']; echo ""; } echo "
"; if (@file_exists("images/".$row['isbn'].".jpg")) { $title = ""; do_html_url($url, $title); } else { echo " "; } echo ""; $title = $row['title']." by ".$row['author']; do_html_url($url, $title); echo "
"; } echo "
"; } function display_book_details($book) { // display all details about this book if (is_array($book)) { echo ""; //display the picture if there is one if (@file_exists("images/".$book['isbn'].".jpg")) { $size = GetImageSize("images/".$book['isbn'].".jpg"); if(($size[0] > 0) && ($size[1] > 0)) { echo ""; } } echo "
    "; echo "
  • Author: "; echo $book['author']; echo "
  • ISBN: "; echo $book['isbn']; echo "
  • Our Price: "; echo number_format($book['price'], 2); echo "
  • Description: "; echo $book['description']; echo "
"; } else { echo "

The details of this book cannot be displayed at this time.

"; } echo "
"; } function display_checkout_form() { //display the form that asks for name and address ?>
Your Details
Name
Address
City/Suburb
State/Province
Postal Code or Zip Code
Country
Shipping Address (leave blank if as above)
Name
Address
City/Suburb
State/Province
Postal Code or Zip Code
Country

Please press Purchase to confirm your purchase, or Continue Shopping to add or remove items.


Shipping
TOTAL INCLUDING SHIPPING $

Credit Card Details
Type
Number
AMEX code (if required)
Expiry Date Month Year
Name on Card

Please press Purchase to confirm your purchase, or Continue Shopping to add or remove items

Item Price Quantity Total "; //display each item as a table row foreach ($cart as $isbn => $qty) { $book = get_book_details($isbn); echo ""; if($images == true) { echo ""; if (file_exists("images/".$isbn.".jpg")) { $size = GetImageSize("images/".$isbn.".jpg"); if(($size[0] > 0) && ($size[1] > 0)) { echo ""; } } else { echo " "; } echo ""; } echo " ".$book['title']." by ".$book['author']." \$".number_format($book['price'], 2)." "; // if we allow changes, quantities are in text boxes if ($change == true) { echo ""; } else { echo $qty; } echo "\$".number_format($book['price']*$qty,2)."\n"; } // display total row echo "   ".$_SESSION['items']." \$".number_format($_SESSION['total_price'], 2)." "; // display save change button if($change == true) { echo "     "; } echo ""; } function display_login_form() { // dispaly form asking for name and password ?>
Username:
Password:

Go to main site
Add a new category
Add a new book
Change admin password
\"".$alt."\"
"; } function display_form_button($image, $alt) { echo "
"; } ?>

book_fns.php

 query($query);
  if (!$result) {
    return false;
  }

  $num_cats = @$result -> num_rows;
  if ($num_cats == 0) {
    return false;
  }

  $result = db_result_to_array($result);
  return $result;
}

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;
}

function get_books($catid) {
  $conn = db_connect();
  $query = "select * from books where catid = ".$catid."";
  $result = @$conn -> query($query);
  if (!$result) {
    return false;
  }

  $num_cats = @$result -> num_rows;
  if ($num_cats == 0) {
    return false;
  }

  $result = db_result_to_array($result);
  return $result;
}

function get_book_details($isbn) {
  if ((!$isbn) || $isbn == '') {
    return false;
  }
  $conn = db_connect();
  $query = "select * from books where isbn = ".$isbn."";
  $result = @$conn -> query($query);
  if (!$result) {
    return false;
  }

  $row = @$result -> fetch_assoc();
  return $row;
}

function calculate_price($cart) {
  $price = 0;
  if (is_array($cart)) {
    $conn = db_connect();
    foreach ($cart as $isbn => $qty) {
      $query = "select price from books where isbn='".$isbn."'";
      $result = $conn -> query($query);
      if ($result) {
        $items = $result -> fetch_object();
        $item_price = $items -> price;
        $price += $item_price * $qty;
      }
    }
  }

  return $price;
}

function calculate_items($cart) {
  $items = 0;
  if (is_array($cart)) {
    $conn = db_connect();
    foreach ($cart as $isbn => $qty) {
      $items += $qty;
      }
  }

  return $items;
}

function calculate_shipping_cost() {
  return 20.00;
}
?>

db_fns.php

autocommit(TRUE);
   return $result;
}

function db_result_to_array($result) {
   $res_array = array();

   for ($count=0; $row = $result->fetch_assoc(); $count++) {
     $res_array[$count] = $row;
   }

   return $res_array;
}

?>

user_auth_fns.php

query("select * from admin
                         where username='".$username."'
                         and password = sha1('".$password."')");
  

  if (!$result) {
     return 0;
  }

  if ($result->num_rows>0) {
     return 1;
  } else {
     return 0;
  }
}

function check_admin_user() {
// see if somebody is logged in and notify them if not

  if (isset($_SESSION['admin_user'])) {
    return true;
  } else {
    return false;
  }
}

function change_password($username, $old_password, $new_password) {
// change password for username/old_password to new_password
// return true or false

  // if the old password is right
  // change their password to new_password and return true
  // else return false
  if (login($username, $old_password)) {

    if (!($conn = db_connect())) {
      return false;
    }

    $result = $conn->query("update admin
                            set password = sha1('".$new_password."')
                            where username = '".$username."'");
    if (!$result) {
      return false;  // not changed
    } else {
      return true;  // changed successfully
    }
  } else {
    return false; // old password was wrong
  }
}


?>

admin_fns.php


  
"; } ?>
Category Name:
align="center"> "; } ?>
"; } ?>
ISBN:
Book Title:
Book Author:
Category:
Price:
Description:
align="center"> "; ?>

Old password:
New password:
Repeat new password:

query($query); if ((!$result) || ($result->num_rows!=0)) { return false; } // insert new category $query = "insert into categories values (0, '".$catname."')"; $result = $conn->query($query); if (!$result) { return false; } else { return true; } } function insert_book($isbn, $title, $author, $catid, $price, $description) { // insert a new book into the database $conn = db_connect(); // check book does not already exist $query = "select * from books where isbn='".$isbn."'"; $result = $conn->query($query); if ((!$result) || ($result->num_rows!=0)) { return false; } // insert new book $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) { // change the name of category with catid in the database $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) { // change details of book stored under $oldisbn in // the database to new details in arguments $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) { // Remove the category identified by catid from the db // If there are books in the category, it will not // be removed and the function will return false. $conn = db_connect(); // check if there are any books in category // to avoid deletion anomalies $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) { // Deletes the book identified by $isbn from the database. $conn = db_connect(); $query = "delete from books where isbn='".$isbn."'"; $result = @$conn->query($query); if (!$result) { return false; } else { return true; } } ?>

data_valid_fns.php

 $value) {
     if ((!isset($key)) || ($value == '')) {
        return false;
     }
  }
  return true;
}

function valid_email($address) {
  // check an email address is possibly valid
  if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $address)) {
    return true;
  } else {
    return false;
  }
}

?>

order_fns.php

autocommit(FALSE);

  // insert customer address
  $query = "select customerid from customers where
            name = '".$name."' and address = '".$address."'
            and city = '".$city."' and state = '".$state."'
            and zip = '".$zip."' and country = '".$country."'";

  $result = $conn->query($query);

  if($result->num_rows>0) {
    $customer = $result->fetch_object();
    $customerid = $customer->customerid;
  } else {
    $query = "insert into customers values
            (0, '".$name."','".$address."','".$city."','".$state."','".$zip."','".$country."')";
    $result = $conn->query($query);

    if (!$result) {
       return false;
    }
  }

  $customerid = $conn->insert_id;

  $date = date("Y-m-d");

  $query = "insert into orders values
            (0, '".$customerid."', '".$_SESSION['total_price']."', '".$date."', '".'PARTIAL'."',
             '".$ship_name."', '".$ship_address."', '".$ship_city."', '".$ship_state."',
             '".$ship_zip."', '".$ship_country."')";

  $result = $conn->query($query);
  if (!$result) {
    return false;
  }

  $query = "select orderid from orders where
               customerid = '".$customerid."' and
               amount > (".$_SESSION['total_price']."-.001) and
               amount < (".$_SESSION['total_price']."+.001) and
               date = '".$date."' and
               order_status = 'PARTIAL' and
               ship_name = '".$ship_name."' and
               ship_address = '".$ship_address."' and
               ship_city = '".$ship_city."' and
               ship_state = '".$ship_state."' and
               ship_zip = '".$ship_zip."' and
               ship_country = '".$ship_country."'";

  $result = $conn->query($query);

  if($result->num_rows>0) {
    $order = $result->fetch_object();
    $orderid = $order->orderid;
  } else {
    return false;
  }

  // insert each book
  foreach($_SESSION['cart'] as $isbn => $quantity) {
    $detail = get_book_details($isbn);
    $query = "delete from order_items where
              orderid = '".$orderid."' and isbn = '".$isbn."'";
    $result = $conn->query($query);
    $query = "insert into order_items values
              ('".$orderid."', '".$isbn."', ".$detail['price'].", $quantity)";
    $result = $conn->query($query);
    if(!$result) {
      return false;
    }
  }

  // end transaction
  $conn->commit();
  $conn->autocommit(TRUE);

  return $orderid;
}

?>

index.php

Please chose a category: 

"; $cat_array = get_categories(); display_categories($cat_array); // If login as admin, show add, delete, edit cat link if (isset($_SESSION['admin_user'])) { display_button("admin.php", "admin-menu", "Admin Menu"); } do_html_footer(); ?>

login.php


logout.php

Logged out.

"; do_html_url("login.php", "Login"); } else { // if they weren't logged in but came to this page somehow echo "

You were not logged in, and so have not been logged out.

"; do_html_url("login.php", "Login"); } do_html_footer(); ?>

admin.php

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("Administrtion"); if (check_admin_user()) { display_admin_menu(); }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

insert_book_form.php

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

insert_book.php

Book ".stripslashes($title)." was added to the database.

"; }else { echo "

Book ".stripslashes($title)." could not be added to the database.

"; } }else { echo "

You have not filled out the form. Please try again

"; } }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

insert_category_form.php

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

insert_category.php

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

"; } }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

delete_book.php

Book was deleted.

"; }else { echo "

Book could not be deleted.

"; } }else { echo "

No Book specified. Please try again

"; } do_html_url("admin.php", "Back to administation menu"); }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

delete_category.php

Category was deleted.

"; }else { echo "

Category could not be deleted.

"; } }else { echo "

No category specified. Please try again

"; } do_html_url("admin.php", "Back to administation menu"); }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

edit_book_form.php

Could not retrieve book details.

"; } do_html_url("admin.php", "Back to administration menu"); }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

edit_book.php

Book was updated.

"; }else { echo "

Book could not be updated.

"; } }else { echo "

You have not filled out the form. Please try again

"; } do_html_url("admin.php", "Back to administation menu"); }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

edit_category_form.php

Could not retrieve catogory details.

"; } do_html_url("admin.php", "Back to administration menu"); }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

edit_category.php

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 administation menu"); }else { echo "

You are not authorized to enter the administration area.

"; } do_html_footer(); ?>

change_password_form.php


change_password.php

You have not filled out the form completely.
Please try again.

"; do_html_url("admin.php", "Back to administration menu"); do_html_footer(); exit; } else { $new_passwd = $_POST['new_passwd']; $new_passwd2 = $_POST['new_passwd2']; $old_passwd = $_POST['old_passwd']; if ($new_passwd != $new_passwd2) { echo "

Passwords entered were not the same. Not changed.

"; } else if ((strlen($new_passwd)>16) || (strlen($new_passwd)<6)) { echo "

New password must be between 6 and 16 characters. Try again.

"; } else { // attempt update if (change_password($_SESSION['admin_user'], $old_passwd, $new_passwd)) { echo "

Password changed.

"; } else { echo "

Password could not be changed.

"; } } } do_html_url("admin.php", "Back to administration menu"); do_html_footer(); ?>

checkout.php

There are no items in your cart

"; } display_button("show_cart.php", "continue-shopping", "Continue Shopping"); do_html_footer(); ?>

process.php

Thank you for shopping with us.Your order has been placed.

"; display_button("index.php", "continue-shopping", "Continue Shopping"); }else { echo "

Could not process your card.Please contact the card issuer or try again.

"; display_button("purchase.php", "back", "Back"); } }else { echo "

You did not fill in all the fields, please try again.


"; display_button("purchase.php", "back", "Back"); } do_html_footer(); ?>

purchase.php

Could not store data, please try again.


"; display_button('checkout.php', 'back', 'back'); } }else { echo "

You did not fill in all the fields, please try again.


"; display_button('checkout.php', 'back', 'back'); } do_html_footer(); ?>

show_book.php


show_cart.php

 $qty) {
        if ($_POST[$isbn] == '0') {
            unset($_SESSION['cart'][$isbn]);
        }else {
            $_SESSION['cart'][$isbn] = $_POST[$isbn];
        }
    }

    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items'] = calculate_items($_SESSION['cart']);
}

do_html_header("Your shopping cart");

// Display the cart
if ($_SESSION['cart'] && (array_count_values($_SESSION['cart']))) {
    display_cart($_SESSION['cart']);
}else {
    echo "

There are no items in your cart


"; } $target = "index.php"; if ($new) { $detail = get_book_details($new); if ($detail['catid']) { $target = "show_cat.php?catid=".$detail['catid']; } } display_button($target, "continue-shopping", "Continue Shopping"); display_button("checkout.php", "go-to-checkout", "Go To Checkout"); do_html_footer(); ?>

show_cat.php


说明

全部文件下载地址:https://pan.baidu.com/s/1pL50Ql9

你可能感兴趣的:(PHP之购物车)