Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Exporting php table to Excel

moudyboymoudyboy Posts: 1Member
hey i am new in php and i created a cart, i would like to add a button to the cart that will enable users to export the cart into excell..

Please help, this is my code so far:-
[code]<?php
session_start();
if (!isset($_SESSION["customer"])) {
header("location: customer_login.php");
exit();
}
//error script
error_reporting(E_ALL);
ini_set('display_errors','1');
//connect to the database
include "../storescripts/connect_to_mysql.php";
?>
<?php
///////////////////////////////////////////////////////////////////////////////////
// SECTION ONE
///////////////////////////////////////////////////////////////////////////////////
if(isset($_POST['pid'])){
$pid=$_POST['pid'];
$wasFound=false;
$i=0;

//if the cart session is set or empty
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])< 1){
//Runs if the cart is empty
$_SESSION["cart_array"]= array(0 => array("item_id"=>$pid,"quantity"=>1));
}else{
//Runs if the cart has at least one item in it
foreach($_SESSION["cart_array"] as $each_item){
$i++;
while(list($key,$value)= each($each_item)){
if($key == "item_id"&&$value==$pid){
//the ite is in the cart..hence we adjust the quantity
array_splice($_SESSION["cart_array"],$i-1,1,array(array("item_id"=>$pid,"quantity"=>$each_item['quantity']+1)));
$wasFound=true;
}//close if conditio
}//close while loop
}//close foreach loop
if($wasFound==false){
array_push($_SESSION["cart_array"],array("item_id"=>$pid,"quantity"=>1));
}
}
header("location: cart.php");
}
?>
<?php
///////////////////////////////////////////////////////////////////////////////////
// SECTION TWO
///////////////////////////////////////////////////////////////////////////////////
//if usser chooses to empty their sopping cart
if(isset($_GET['cmd'])&& $_GET['cmd']=="emptycart"){
unset($_SESSION["cart_array"]);
}
?>
<?php
///////////////////////////////////////////////////////////////////////////////////
// SECTION THREE
///////////////////////////////////////////////////////////////////////////////////
//if usser chooses to empty their sopping cart
if(isset($_POST['item_to_adjust'])&& $_POST['item_to_adjust']!=""){
//execute some code
$item_to_adjust=$_POST['item_to_adjust'];
$quantity=$_POST['quantity'];
$quantity=preg_replace('#[^0-9]#i', '',$quantity);
if($quantity >= 1000){$quantity=999;}
if($quantity < 1){$quantity= 1;}
$i=0;
foreach($_SESSION["cart_array"] as $each_item){
$i++;
while(list($key,$value)= each($each_item)){
if($key == "item_id"&&$value==$item_to_adjust){
//the ite is in the cart..hence we adjust the quantity
array_splice($_SESSION["cart_array"],$i-1,1,array(array("item_id"=>$item_to_adjust,"quantity"=>$quantity)));
}//close if conditio
}//close while loop
}//close foreach loop
}
?>
<?php
///////////////////////////////////////////////////////////////////////////////////
// SECTION FOUR
///////////////////////////////////////////////////////////////////////////////////
if(isset($_POST['index_to_remove'])&&$_POST['index_to_remove']!=""){
//access the array and run code to remove
$key_to_remove= $_POST['index_to_remove'];
if(count($_SESSION["cart_array"])<=1){
unset($_SESSION["cart_array"]);
}else{
unset($_SESSION["cart_array"]["$key_to_remove"]);
sort($_SESSION["cart_array"]);
}
}
?>
<?php
///////////////////////////////////////////////////////////////////////////////////
// SECTION FIVE
///////////////////////////////////////////////////////////////////////////////////

$cartOutput="";
$cartTotal="";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"])< 1){
$cartOutput="<h2 align='center'>Your Cart Is Empty";
}else{
$i=0;
foreach($_SESSION["cart_array"] as $each_item){
$item_id=$each_item['item_id'];
$sql=mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while($row=mysql_fetch_array($sql)){
$product_name=$row["product_name"];
$price=$row['price'];
$details=$row['details'];
}
$pricetotal= $price*$each_item['quantity'];
$cartTotal=$pricetotal + $cartTotal;

//setlocale(LC_MONETARY,"en_KSHs");
//$pricetotal= money_format("%10.2n", $pricetotal);
//dynamic table assembly

$cartOutput .="";
$cartOutput .='' .$product_name . '
' . $product_name . '';
$cartOutput .='' . $details . '';
$cartOutput .='



';
//$cartOutput .='' . $each_item['quantity'] . '';
$cartOutput .='' . $price . '';
$cartOutput .='' . $pricetotal . '';
$cartOutput .='

';
$cartOutput .='';
$i++;
}

$cartTotal="
Your Total is KSHs. ".$cartTotal."
";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



Your Cart




<?php include_once("../templates/template_header3.php");?>











<?php echo $cartOutput; ?>

Product Product Details Quantity Unit Price
(KHSs.)
Total
(KHSs.)
Remove
<?php echo $cartTotal; ?>



Empty Your Cart



<?php include_once("../templates/template_footer.php");?>



[/code]

Sign In or Register to comment.