Location: PHPKode > projects > Simple PHP Shopping cart - asaancart > asaancart v-0.9/shop/add_to_cart.php
<?php
session_start();
/*
asaancart - easy shopping cart solution
---------------------------------------

Copyright 2009 Nasir Ahmad Khan
Email: hide@address.com

This file is part of asaancart - open source easy shopping cart solution.

asaancart is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

asaancart is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with asaancart.  If not, see <http://www.gnu.org/licenses/>.

*/

include("../config/config.php");
include("classes/functions.php");

if (session_id() == "") session_start();
$session_id = session_id();
$product_id = $_GET['product_id'];
$page_heading = "My Shoping Cart";
$product_name = $_GET['product_name'];
$mode = $_GET['mode'];
$title = $page_heading;

//list brands:

$sql = "SELECT * FROM brands ORDER BY brand_name";
$results = mysql_query($sql);
$total_brand = mysql_num_rows($results);
$smarty->assign('total_brand', $total_cat);

while($row = mysql_fetch_assoc($results) )
{
	$all_brands[] = $row;		
}

$smarty->assign('all_brands', $all_brands);

/// end list brands

//list categories:

$sql = "SELECT * FROM category ORDER BY category_name";
$results = mysql_query($sql);
$total_cat = mysql_num_rows($results);
$smarty->assign('total_cat', $total_cat);

while($row = mysql_fetch_assoc($results) )
{
	$cat_list[] = $row;		
}

$smarty->assign('cat_list', $cat_list);

/// end list categories

//list parent categories
$smarty->assign('cat_parent_list', menu_shop_by_category()); 
//end parent cat

//related products
//products
$sql_related = "SELECT * FROM related_products WHERE product_id = '$product_id'";
$results_related = mysql_query($sql_related);
$total_products_related = 0;
$total_products_related = mysql_num_rows($results_related);
$smarty->assign('total_products_related', $total_products_related);
//$cnt_pro =0;
while($row_related = mysql_fetch_assoc($results_related) )
{
	
	//get product data
	$sql_pro_data = "SELECT * FROM products WHERE product_id='".$row_related['related_product_id']."'";
	$results_pro_data = mysql_query($sql_pro_data);
	$pro_data_cnt2 = 0;
	while($row_pro_data = mysql_fetch_assoc($results_pro_data) )
	{
		$related_product_list[] = $row_pro_data;
	}
	
	// end get pro data
	
	//$cnt_pro++;
	//get pro image
	$sql_related_img = "SELECT * FROM product_images WHERE product_id='".$row_related['related_product_id']."' ORDER BY is_main DESC";
	$results_related_img = mysql_query($sql_related_img);
	$img_cnt2 = 0;
	while($row_related_img = mysql_fetch_assoc($results_related_img) )
	{
		
			if($img_cnt2<1){
			$related_product_image[] = $row_related_img;
			$img_cnt2++;
			}
				
	}
	
	
	// end pro img			
}
$smarty->assign('related_product_list', $related_product_list);
$smarty->assign('related_product_image', $related_product_image);

//end related pro

//cross-selling products
//related products
//products
$sql_cross_selling = "SELECT * FROM cross_selling_products WHERE product_id = '$product_id'";
$results_cross_selling = mysql_query($sql_cross_selling);
$total_products_cross_selling = 0;
$total_products_cross_selling = mysql_num_rows($results_cross_selling);
$smarty->assign('total_products_cross_selling', $total_products_cross_selling);
//$cnt_pro =0;
while($row_cross_selling = mysql_fetch_assoc($results_cross_selling) )
{
	
	//get product data
	$sql_pro_data = "SELECT * FROM products WHERE product_id='".$row_cross_selling['cross_product_id']."'";
	$results_pro_data = mysql_query($sql_pro_data);
	$pro_data_cnt2 = 0;
	while($row_pro_data = mysql_fetch_assoc($results_pro_data) )
	{
		$cross_selling_product_list[] = $row_pro_data;
	}
	
	// end get pro data
	
	//$cnt_pro++;
	//get pro image
	$sql_cross_selling_img = "SELECT * FROM product_images WHERE product_id='".$row_cross_selling['cross_product_id']."' ORDER BY is_main DESC";
	$results_cross_selling_img = mysql_query($sql_cross_selling_img);
	$img_cnt2 = 0;
	while($row_cross_selling_img = mysql_fetch_assoc($results_cross_selling_img) )
	{
		
			if($img_cnt2<1){
			$cross_selling_product_image[] = $row_cross_selling_img;
			$img_cnt2++;
			}
				
	}
	
	
	// end pro img			
}
$smarty->assign('cross_selling_product_list', $cross_selling_product_list);
$smarty->assign('cross_selling_product_image', $cross_selling_product_image);

//end related pro
//end cross-selling

//get pro image
	$sql_img = "SELECT * FROM product_images WHERE product_id='".$product_id."' ORDER BY is_main DESC";
	$results_img = mysql_query($sql_img);
	$img_cnt = 0;
	while($row_img = mysql_fetch_assoc($results_img) )
	{
			if($img_cnt<1){
			$product_image = $row_img['image_filename'];
			}
	}
	
	
	// end pro img		
	
//get product price
//products
$sql = "SELECT * FROM products WHERE product_id = '$product_id' ORDER BY product_name";
$results = mysql_query($sql);
$total_products = mysql_num_rows($results);
$smarty->assign('total_products', $total_products);
while($row = mysql_fetch_assoc($results) )
{
	$product_price = $row['product_price'];
	$product_description = $row['product_description'];
}
//end get price	
//add to cart
if($mode == "add"){
//$product_qty = $_GET[''];
$session_id = session_id();
$product_qty = 1;
$total = $product_price*$product_qty;
	$is_found = 0;
	$sql_chk_pro = "SELECT * FROM my_cart WHERE product_id = '$product_id' AND session_id='$session_id'";
	$results_chk_pro = mysql_query($sql_chk_pro);
	$is_found = mysql_num_rows($results_chk_pro);
	
	if ($is_found==0){
	$sql = "INSERT INTO my_cart (session_id, product_id, product_name, product_description, product_price, product_image, product_qty, total) VALUES ('$session_id', '$product_id', '$product_name', '$product_description', $product_price, '$product_image', $product_qty, $total)";
	$results = mysql_query($sql);
	$smarty->assign('msg',"<div style='color:black; font-weight:normal;'>Done, <strong>$product_name</strong> added successfully into your cart</div>");
	}else{
	$smarty->assign('msg',"<div style='color:red; font-weight:bold;'>Sorry, <strong>$product_name</strong> already in your cart. Please update quantity if you want to add more.</div>");
	}// found chk
}

//end add to cart

// get grand total
$sql = "SELECT sum(total) FROM my_cart WHERE session_id = '$session_id'";
$results = mysql_query($sql);
while($row = mysql_fetch_array($results) )
{
	$grand_total = $row[0];
}
$smarty->assign('grand_total', $grand_total);
//end g total
//my cart
$sql = "SELECT * FROM my_cart WHERE session_id = '$session_id' ORDER BY product_name";
$results = mysql_query($sql);
$total_products = mysql_num_rows($results);
$smarty->assign('total_products', $total_products);
//$cnt_pro =0;
while($row = mysql_fetch_assoc($results) )
{
	$cart_product_list[] = $row;
}
//end my cart

$smarty->assign('cart_product_list', $cart_product_list);
$smarty->assign('page_heading', $page_heading);
$smarty->assign('product_id', $product_id);

//end manage

$smarty->assign('title',$title);
$smarty->display('site_add_to_cart.tpl');
?>
Return current item: Simple PHP Shopping cart - asaancart