cookie版购物车
#region 添加到購物車AddShoppingCar
/// <summary>
/// 添加到購物車AddShoppingCar
/// </summary>
/// <param name="num">數量 如果存在產品 負數是減少
/// 正數是增加? 如果不存在? 直接增加</param>
/// <param name="id">貨物ID</param>
/// <param name="expires">cookies保存的天數</param>
/// <remarks>這里的方法就是把在原有的Cookie基礎上判斷是否有
/// 這個產品 如果有 在原有數量上增加 沒有 就直接增加 如果是負
/// 數 就是減少 如果負數的數量大于等于
/// 原有數量 設置為0 對應后面的讀出操作</remarks>
public static void AddShoppingCar(string num, string id, int expires)
{
??? if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null)
??? {
??????? System.Web.HttpCookie cookie;
??????? string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
??????? if (System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id.ToString()] == null)
??????? {
??????????? cookievalue = cookievalue + "&" + id + "=" + num;
??????? }
??????? else
??????? {
??????????? int num1 = int.Parse(System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id.ToString()].ToString()) + int.Parse(num);
??????????? if (num1 > 0)
??????????? {
??????????????? System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id.ToString()] = num1.ToString();
??????????? }
??????????? else
??????????? {
??????????????? System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id.ToString()] = "0";
??????????? }
??????????? cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
??????? }
??????? cookie = new System.Web.HttpCookie("Products", cookievalue);
??????? if (expires != 0)
??????? {
??????????? DateTime dt = DateTime.Now;
??????????? TimeSpan ts = new TimeSpan(expires, 0, 0, 20);
??????????? cookie.Expires = dt.Add(ts);
??????? }
??????? System.Web.HttpContext.Current.Response.AppendCookie(cookie);
??? }
??? else
??? {
??????? System.Web.HttpCookie newcookie = new HttpCookie("Products");
??????? if (expires != 0)
??????? {
??????????? DateTime dt = DateTime.Now;
??????????? TimeSpan ts = new TimeSpan(expires, 0, 0, 20);
??????????? newcookie.Expires = dt.Add(ts);
??????? }
??????? newcookie.Values[id.ToString()] = num;
??????? System.Web.HttpContext.Current.Response.AppendCookie(newcookie);
??? }
}
#endregion
#region 添加到購物車AddShoppingCar
/// <summary>
/// 添加到購物車AddShoppingCar
/// </summary>
/// <param name="num">數量 如果存在產品 負數是減少
/// 正數是增加? 如果不存在? 直接增加</param>
/// <param name="dt">
/// 循環讀出來的DATATABLE
/// </param>
/// <param name="id">貨物ID</param>
/// <param name="expires">cookies保存的天數</param>
/// <remarks>這里的方法就是把在原有的Cookie基礎上判斷是否有
/// 這個產品 如果有 在原有數量上增加 沒有 就直接增加 如果是負
/// 數 就是減少 如果負數的數量大于等于
/// 原有數量 設置為0 對應后面的讀出操作</remarks>
public static void AddShoppingCar(DataTable dt, int expires)
{
??????? if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null)
??????? {
??????????? System.Web.HttpCookie cookie;
??????????? string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
??????????? for (int i = 0; i < dt.Rows.Count; i++)
??????????? {
??????????????? if (System.Web.HttpContext.Current.Request.Cookies["Products"].Values[dt.Rows[i]["id"].ToString().ToString()] == null)
??????????????? {
??????????????????? cookievalue = cookievalue + "&" + dt.Rows[i]["id"].ToString() + "=" + dt.Rows[i]["num"].ToString();
??????????????? }
??????????????? else
??????????????? {
??????????????????? int num1 = int.Parse(System.Web.HttpContext.Current.Request.Cookies["Products"].Values[dt.Rows[i]["id"].ToString().ToString()].ToString()) + int.Parse(dt.Rows[i]["num"].ToString());
??????????????????? if (num1 > 0)
??????????????????? {
??????????????????????? System.Web.HttpContext.Current.Request.Cookies["Products"].Values[dt.Rows[i]["id"].ToString().ToString()] = num1.ToString();
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? System.Web.HttpContext.Current.Request.Cookies["Products"].Values[dt.Rows[i]["id"].ToString().ToString()] = "0";
??????????????????? }
??????????????????? cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
??????????????? }
??????????? }
??????????? cookie = new System.Web.HttpCookie("Products", cookievalue);
??????????? if (expires != 0)
??????????? {
??????????????? DateTime time1 = DateTime.Now;
??????????????? TimeSpan ts = new TimeSpan(expires, 0, 0, 20);
??????????????? cookie.Expires = time1.Add(ts);
??????????? }
??????????? System.Web.HttpContext.Current.Response.AppendCookie(cookie);
??????? }
??????? else
??????? {
??????????? System.Web.HttpCookie newcookie = new HttpCookie("Products");
??????????? if (expires != 0)
??????????? {
??????????????? DateTime time1 = DateTime.Now;
??????????????? TimeSpan ts = new TimeSpan(expires, 0, 0, 20);
??????????????? newcookie.Expires = time1.Add(ts);
??????????? }
??????????? for (int i = 0; i < dt.Rows.Count; i++)
??????????? {
??????????????? newcookie.Values[dt.Rows[i]["id"].ToString()] = dt.Rows[i]["num"].ToString();
??????????? }
??????????? System.Web.HttpContext.Current.Response.AppendCookie(newcookie);
??????? }
???
}
#endregion
#region 根據ID刪除產品RemoveShoppingCar
/// <summary>
/// 根據ID刪除產品RemoveShoppingCar
/// </summary>
/// <param name="id">產品ID</param>
/// <remarks>
/// 就是設置商品數量為0
/// 本來增加方法可以實現
/// 但是需要讀出來原有數量
/// 所以為了避免繁瑣 有此方法
/// </remarks>
public static void RemoveShoppingCar(string id)
{
??? if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] != null)
??? {
??????? System.Web.HttpCookie cookie;
??????? System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] = "0";
??????? string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
??????? cookie = new System.Web.HttpCookie("Products", cookievalue);
??????? System.Web.HttpContext.Current.Response.AppendCookie(cookie);
??? }
}
#endregion
#region 刪除購物車RemoveShoppingCar
/// <summary>
/// 刪除購物車RemoveShoppingCar
/// </summary>
/// <remarks>
/// 使購物車的Cookie為空
/// </remarks>
public static void RemoveShoppingCar()
{
??? if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Values.Count != 0)
??? {
??????? System.Web.HttpContext.Current.Request.Cookies["Products"].Expires = System.DateTime.Now.AddHours(-1);
??????? System.Web.HttpContext.Current.Response.AppendCookie(System.Web.HttpContext.Current.Request.Cookies["Products"]);
??? }
}
#endregion
#region 根據ID修改產品UpdateShoppingCar
/// <summary>
/// 根據ID修改產品UpdateShoppingCar
/// </summary>
/// <param name="id">產品ID</param>
/// <param name="num">產品數量</param>
/// <remarks>
/// 更新產品的數量
/// </remarks>
public static void UpdateShoppingCar(string id,string num)
{
??? if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] != null)
??? {
??????? System.Web.HttpCookie cookie;
??????? System.Web.HttpContext.Current.Request.Cookies["Products"].Values[id] = num;
??????? string cookievalue = System.Web.HttpContext.Current.Request.Cookies["Products"].Value;
??????? cookie = new System.Web.HttpCookie("Products", cookievalue);
??????? System.Web.HttpContext.Current.Response.AppendCookie(cookie);
??? }
}
#endregion
#region 得到所有的產品列表GetAllChoppingCar
/// <summary>
/// 得到所有的產品列表GetAllChoppingCar
/// </summary>
/// <returns>所有產品的數據集</returns>
/// <remarks>因為對DataTable操作比較方便(個人喜好)
/// 這里是把里面的字符串分割為Datatable</remarks>
public static DataTable GetAllChoppingCar()
{
??? if (System.Web.HttpContext.Current.Request.Cookies["Products"] != null && System.Web.HttpContext.Current.Request.Cookies["Products"].Value.Trim() != "")
??? {
??????? DataColumn dcid = new DataColumn("id");
??????? DataColumn dcnum = new DataColumn("num");
??????? DataTable dt = new DataTable();
??????? dt.Columns.Add(dcid);
??????? dt.Columns.Add(dcnum);
??????? string[] str = System.Web.HttpContext.Current.Request.Cookies["Products"].Value.Split('&');
??????? for (int i = 0; i < str.Length; i++)
??????? {
??????????? DataRow dr = dt.NewRow();
??????????? dr["id"] = (str[i].Split('='))[0].ToString();
??????????? dr["num"] = (str[i].Split('='))[1].ToString();
??????????? if (int.Parse((str[i].Split('='))[1].ToString()) != 0)
??????????? {
??????????????? dt.Rows.Add(dr);
??????????? }
??????? }
??????? return dt;
??? }
??? else
??? {
??????? return null;
??? }
}
#endregion
#region 關于購物車Cookie里的DataTable的操作GetCookieByDataTable
/// <summary>
/// 關于購物車Cookie里的DataTable的操作GetCookieByDataTable
/// </summary>
/// <param name="dt"></param>
/// <remarks>
/// 把讀出來的DataTable轉換為字符串
/// 根據的規則是自己定的 &符號是分開產品
/// =號是分開產品ID和產品價格
/// </remarks>
/// <returns></returns>
public static String GetCookieByDataTable(DataTable dt)
{
??? String datatable = "";
??? if (dt.Rows.Count > 0)
??? {
??????? for (int i = 0; i < dt.Rows.Count; i++)
??????? {
??????????? datatable = dt.Rows[i]["id"].ToString() + "=" + dt.Rows[i]["num"].ToString() + "&" + datatable;
??????? }
??? }
??? return datatable;
}
#endregion
#region 把字符串轉換為datatable? GetDataTable
/// <summary>
/// 把字符串轉換為datatable? GetDataTable
/// </summary>
/// <param name="datatable"></param>
/// <remarks>
/// 把Cookie里的字符串轉換為DataTable
/// 是GetCookieByDataTable的反操作
/// </remarks>
/// <returns></returns>
public static DataTable GetDataTable(string datatable)
{
??? DataColumn dcid = new DataColumn("id");
??? DataColumn dcnum = new DataColumn("num");
??? DataTable dt = new DataTable();
??? dt.Columns.Add(dcid);
??? dt.Columns.Add(dcnum);
??? if (!datatable.StartsWith("&"))
??? {
??????? string[] str = datatable.Split('&');
??????? for (int i = 0; i < str.Length; i++)
??????? {
??????????? DataRow dr = dt.NewRow();
??????????? dr["id"] = (str[i].Split('='))[0].ToString();
??????????? dr["num"] = (str[i].Split('='))[1].ToString();
??????????? if (int.Parse((str[i].Split('='))[1].ToString()) != 0)
??????????? {
??????????????? dt.Rows.Add(dr);
??????????? }
??????? }
??? }
??? else
??? {
??????? DataRow dr = dt.NewRow();
??????? dr["id"] = (datatable.Split('='))[0].ToString();
??????? dr["num"] = (datatable.Split('='))[1].ToString();
??????? if (int.Parse((datatable.Split('='))[1].ToString()) != 0)
??????? {
??????????? dt.Rows.Add(dr);
??????? }
??? }
??? return dt;
}
#endregion
轉載于:https://www.cnblogs.com/alon1982/archive/2008/11/28/1343206.html
總結
以上是生活随笔為你收集整理的cookie版购物车的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js参数使用时常犯的一个低级错误
- 下一篇: Apache服务器上部署的网站乱码问题的