PHP将变量发送到另一个页面以进行excel导出

时间:2013-12-17 15:39:30

标签: php mysql excel

我有一个构建查询。基本上,用户可以将数据输入到文本框中,使用下拉选择和/或单选按钮。所有这些信息都被发送到一系列$ _GET变量,这些变量被添加到用于主查询的$ where变量中。

以下是一些构建查询的内容:

 <?php
   $bol = $_GET['bol'];
   $container = $_GET['container'];
   $city = $_GET['city'];

   $select = "";
   $where = "";

   if($container != ""){
      if( $where != "" ) $where .= " AND ";
       $where .= " dispatch_read.CONTAINER_NUMBER = 
           '".mysql_real_escape_string($container)."'";
         }
   if($bol != ""){
     if( $where != "" ) $where .= " AND ";
       $where .= " dispatch_read.BOL_NUMBER = 
           '".mysql_real_escape_string($bol)."'";
         }
   if($city != ""){
     if( $where != "" ) $where .= " AND ";
           $where .= " dispatch_read.ALTERNATE_POINT_IMPORT =
           '".mysql_real_escape_string($city)."'";
         }      

   if ($where != "")
     $select = "SELECT * FROM dispatch_read WHERE " . $where .";";
     $QueryResult = @mysql_query($select) or die ();
     $resnum = mysql_num_rows($QueryResult);    

此时,我可以从表中检索数据并将其显示在网格中。

我将继续展示一些显示网格的代码:

 if ($resnum == 0){
   echo "<div><h2>Your search returned no results</h2></div>";
   }
 else{
   echo "<table>\n";
   echo "<thead><tr>" .
   echo "<th>BOL</th>" .
   echo "<th>CONTAINER</th>" .
   echo "<td>CITY</th>" .
   echo "</tr></thead>\n";
 while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE){
   echo "<tbody><tr>";
   echo "<td>{$Row[BOL_NUMBER]}</td>";
   echo "<td>{$Row[CONTAINER_NUMBER]}</td>";
   echo "<td>{$Row[CITY]}</td>";
   echo "</tr></tbody>";
   echo "</table>\n";
   }
 }

还有更多搜索条件以及显示的行。

*更新*

在我的主页面(称为dispatch.php)中,我有一个调用名为getreport.php的页面的javascript函数:

  function getreport(){
    window.location="getreport.php?where=$where";
  }

上面代码的一部分,你可以看到我试图将$ where发送到该页面的位置。当然,这不起作用。

我需要做的是将$ where变量(将容器1或所有变量)发送到另一个页面,其中有另一个查询我将插入$ where变量。

基本上,用户应该能够打印出优秀的网格。我已经尝试了几种方法来做到这一点。我试图从URL中提取参数,但我认为这不会起作用,因为某些参数可能是空白的,我认为这会抛出查询。

总结一下,如何将$ where变量发送到另一个页面?

提前谢谢。

3 个答案:

答案 0 :(得分:1)

http://www.php.net/manual/en/session.examples.basic.php

您希望使用会话,因此用户可以存储请求之间使用的变量。

例如:

<?php
   $bol = $_GET['bol'];
   $container = $_GET['container'];
   $city = $_GET['city'];
session_start();
   $select = "";
   $_SESSION['where'] = "";

   if($container != ""){
      if( $_SESSION['where'] != "" ) $_SESSION['where'] .= " AND ";
       $_SESSION['where'] .= " dispatch_read.CONTAINER_NUMBER = 
           '".mysql_real_escape_string($container)."'";
         }
   if($bol != ""){
     if( $_SESSION['where'] != "" ) $_SESSION['where'] .= " AND ";
       $_SESSION['where'] .= " dispatch_read.BOL_NUMBER = 
           '".mysql_real_escape_string($bol)."'";
         }
   if($city != ""){
     if( $_SESSION['where'] != "" ) $_SESSION['where'] .= " AND ";
           $_SESSION['where'] .= " dispatch_read.ALTERNATE_POINT_IMPORT =
           '".mysql_real_escape_string($city)."'";
         }      

   if ($_SESSION['where'] != "")
     $select = "SELECT * FROM dispatch_read WHERE " . $_SESSION['where'] .";";
     $QueryResult = @mysql_query($select) or die ();
     $resnum = mysql_num_rows($QueryResult);   

答案 1 :(得分:0)

如果我正确读取此内容,您只需添加&amp; where = urlencode({your where var}),但您没有包含调用该页面的代码部分。

答案 2 :(得分:0)

试试这个:

<?php 

// Please connect to the database using PDO
$dbname = 'database';
$user = 'user';
$pass = '';

try {
    $DB = new PDO("mysql:host=localhost;dbname={$dbname}", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
} catch (PDOException $e) {
    die($e->getMessage());
}

// Get $_GET vars
$bol = $_GET['bol'];
$container = $_GET['container'];
$city = $_GET['city'];

$select = $where = '';

// If the var is empty, it will be false
if ($container) {
    // Using the PDO prepared statements tokens/variables
    $where = ' dispatch_read.CONTAINER_NUMBER = :container';
}
if ($bol) {
    if ( $where ) { $where .= ' AND ' }
    $where .= 'dispatch_read.BOL_NUMBER = :bol';
}
if ($city) {
    if ( $where ) { $where .= ' AND ' }
    $where .= 'dispatch_read.ALTERNATE_POINT_IMPORT = :city';
}

if ($where) {
    // Create the tokens/variables for pdo
    $vars[':container'] = $container;
    $vars[':bol'] = $bol;
    $vars[':city'] = $city;
    // Concatenate the query
    $select = "SELECT * FROM dispatch_read WHERE {$where}";
    // Prepare that query for execution
    $sttm = $DB->prepare( $select );
    // Executing the query with the $vars array
    $sttm->execute( $vars );
    // Get all the rows in a variable
    $allRows = $sttm->fetchAll(PDO::FETCH_ASSOC);
    // Instead of redirecting, just include the file
    include 'process_data.php'
}

然后在process_data.php

// process_data.php
// This is a really, really, REALLY dirty way to generate a Excel File, just for the sake of testing
// Use http://phpexcel.codeplex.com/ instead
<?php if (!$allRows): ?>
    <html>
    <body>
        <div><h2>Your search returned no results</h2></div>
    </body>
    </html>
<?php else: ?>
    <?php 
    ob_clean();
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment;filename=result.xls ");
    header("Content-Transfer-Encoding: binary ");
    ?>

    <html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <table>
            <thead>
                <tr>
                    <th>BOL</th>
                    <th>CONTAINER</th>
                    <th>CITY</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($allRows as $Row): ?>
                    <tr>
                        <td><?php echo $Row['BOL_NUMBER'] ?></td>
                        <td><?php echo $Row['CONTAINER_NUMBER'] ?></td>
                        <td><?php echo $Row['CITY'] ?></td>
                    </tr>
                <?php endforeach ?>
            </tbody>
        </table>
    </body>
    </html>
<?php endif ?>