尊龙凯时人生就是搏

ÔõôÓÃThinkPHPʵÏÖÒ»¸ö¹ºÎï³µ¹¦Ð§

Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÊý¾Ý¿âÀ´´æ´¢ÎÒÃǵÄÉÌÆ·ºÍ¶©µ¥ÐÅÏ¢¡£¸´ÖƲ¢Õ³ÌùÒÔÏÂSQL´úÂëµ½phpMyAdmin»òÆäËûMySQL¿Í»§¶ËÖУ¬¼´¿É½¨ÉèÊý¾Ý¿â£º

CREATE DATABASE cart DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

È»ºó£¬ÎÒÃÇÐèÒª½¨ÉèÁ½¸ö±íÀ´´æ´¢ÉÌÆ·ºÍ¶©µ¥ÐÅÏ¢¡£ÏÂÊöSQLÓï¾äÊǽ¨Éè¡°products¡±ºÍ¡°orders¡±Á½¸ö±íµÄ£º CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(50), price DECIMAL(10,2) ); CREATE TABLE orders ( order_id INT PRIMARY KEY, product_id INT, order_date DATE, amount INT, FOREIGN KEY (product_id) REFERENCES products(product_id) );

CREATE TABLE products (
 id int(11) NOT NULL AUTO_INCREMENT,
 name varchar(255) NOT NULL,
 description text NOT NULL,
 price float NOT NULL,
 PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

µÇ¼ºó¸´ÖÆ

CREATE TABLE orders (
 id int(11) NOT NULL AUTO_INCREMENT,
 user_id int(11) NOT NULL,
 product_id int(11) NOT NULL,
 quantity int(11) NOT NULL,
 created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

µÇ¼ºó¸´ÖÆ

ÏÖÔÚ£¬ÎÒÃÇÐèÒªÉèÖÃÎÒÃǵÄÓ¦ÓóÌÐò¡£Ê¹ÓÃComposer×°ÖÃThinkPHP¿ò¼Ü£º

composer create-project topthink/think tp5 –prefer-dist

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡± £»

½Ó×Å£¬°ÑÏÂÃæµÄ´úÂ븴ÖƲ¢Õ³Ìùµ½tp5/application/common.phpÎļþÀȫ¾Ö×ÊÖúº¯Êý¡°getCart¡±½«±»½¨É裬ÒÔ»ñÈ¡Óû§¹ºÎï³µÐÅÏ¢

<?php
use app\index\model\Cart;
function getCart()
{
$user_id = 1; // ´Ë´¦Ä¬ÈÏÓû§IDΪ1£¬ÏÖʵӦÓÃÖÐÓ¦¸Ã´Ó»á»°ÖлñÈ¡Óû§ID
$cart = Cart::where('user_id', $user_id)->select();
return $cart;
}

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÃûΪ¡°Cart¡±µÄÄ£×ÓÀ´ÖÎÀíÓû§¹ºÎï³µÖеÄÏîÄ¿¡£

<?php
namespace app\index\model;
use think\Model;
class Cart extends Model
{
protected $table = 'orders';

static function add($product_id, $quantity)
{
    $user_id = 1; // ´Ë´¦Ä¬ÈÏÓû§IDΪ1£¬ÏÖʵӦÓÃÖÐÓ¦¸Ã´Ó»á»°ÖлñÈ¡Óû§ID
    $order = new Cart();
    $order->user_id = $user_id;
    $order->product_id = $product_id;
    $order->quantity = $quantity;
    $order->save();
}

static function remove($id)
{
    Cart::destroy($id);
}
}

µÇ¼ºó¸´ÖÆ

ÎÒÃÇÏÖÔÚÄܹ»Í¨¹ýʹÓá°Cart¡±Ä£×ÓÔÚÓ¦ÓóÌÐòÖÐÌí¼Ó»òɾ³ý¹ºÎï³µÖеÄÉÌÆ·¡£Ê¹ÓÃÒÔÏ´úÂ뽫ÉÌÆ·Ìí¼Óµ½¹ºÎï³µ£º

Cart::add($product_id, $quantity);

µÇ¼ºó¸´ÖÆ

¶ø½«ÉÌÆ·´Ó¹ºÎï³µÖÐɾ³ýµÄ´úÂëÈçÏ£º

Cart::remove($id);

×îºó£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÃûΪ¡°Cart¡±µÄ¿ØÖÆÆ÷£¬²¢Ìí¼ÓÁ½¸öÒªÁ죺һ¸öÓÃÓÚÏÔʾ¹ºÎï³µÄÚÈÝ£¬ÁíÒ»¸öÓÃÓÚ½«ÉÌÆ·Ìí¼Óµ½¹ºÎï³µ¡£

<?php
namespace app\index\controller;
use app\index\model\Cart;
class CartController extends BaseController
{
public function index()
{
    $cart = getCart();
    $this->assign('cart', $cart);
    return $this->fetch();
}

public function add()
{
    $product_id = input('post.product_id');
    $quantity = input('post.quantity');

    Cart::add($product_id, $quantity);

    $this->success('Ìí¼ÓÀÖ³É', url(/help/&));
}
}

µÇ¼ºó¸´ÖÆ

Íê³ÉÉÏÊö°ì·¨ºó£¬ÎÒÃÇÒѾ­Àֳɽ¨ÉèÁËÒ»¸ö¼òÆӵĹºÎï³µÓ¦ÓóÌÐò¡£ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý»á¼ûCartControllerµÄindexÒªÁìÀ´ÏÔʾ¹ºÎï³µÄÚÈÝ£¬²¢Í¨¹ý»á¼ûCartControllerµÄaddÒªÁìÀ´½«ÉÌÆ·Ìí¼Óµ½¹ºÎï³µÖС£

ÒÔÉϾÍÊÇÔõôÓÃThinkPHPʵÏÖÒ»¸ö¹ºÎï³µ¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ