English 中文(简体)
向Excel出口一个 php表格
原标题:Exporting a php table to Excel

我正在着手进行一项“基地”转让,......这是在“营地”内的新任务。 并且已经开发了一个汽车,希望用户能够选择点击一个县,出口桌上的数据,然后生产出来。

这是我迄今为止的法典:

    <?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</h2>";
}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 .="<tr align= center >";
      $cartOutput .= <td><a href="../home.php?id=  . $item_id .  ">  .$product_name .  </a><br/><img src="../inventory_images/  . $item_id .  .jpg" alt="  . $product_name .  " width="40" height="52" border="1"/></td> ;
      $cartOutput .= <td>  . $details .  </td> ;
      $cartOutput .= <td><form action="cart.php" method="post">
      <input name="quantity" type="text" value="  . $each_item[ quantity ] .  " size="1" maxlength="3" />
      <input name="adjustBtn  . $item_id .  " type="image" value="change" src="../images/buttons/button_save.gif"/>
      <input name="item_to_adjust" type="hidden" value="  . $item_id .  " />
      </form></td> ;
      //$cartOutput .= <td>  . $each_item[ quantity ] .  </td> ;
      $cartOutput .= <td>  . $price .  </td> ;
      $cartOutput .= <td>  . $pricetotal .  </td> ;
      $cartOutput .= <td><form action="cart.php" method="post">
      <input name="deleteBtn  . $item_id .  " type="image" value="X" src="../images/buttons/button_delete.gif"/>
      <input name="index_to_remove" type="hidden" value="  . $i .  " /
      ></form></td> ;
      $cartOutput .= </tr> ;
      $i++;
   }

  $cartTotal="<div align= right >Your Total is KSHs. ".$cartTotal."</div>";
 }
?>
问题回答

如果你绝对需要将数据出口到本地的Excel文档中,请查看PHPExcel。 它是一个精良的图书馆,用于操纵Excel文档,但对于PHP的一位新来者来说,会很复杂。 否则,如果你恢复出口CSV并转而改用Excel文档,那么你就会发现本地的富饶功能。

<?
header( Content-type: application/vnd.ms-excel );
    header("Content-Disposition: attachment; filename=catalogo.xls");
    header("Pragma: no-cache");
    header("Expires: 0"); 

$table = "<table>";
$table .="<tr><td>Hello world</td></tr>";
$table .="</table>";

echo $table
?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签