PHP将查询字符串传递给另一个页面

时间:2013-12-16 15:44:55

标签: php mysql export query-string export-to-excel

我有三个文件。主文件是dispatch.php。在此文件中,用户将进行各种选择,这些选择在另一个名为dispatch-grid.php的页面上使用查询构建器。在dispatch-grid.php中,查询被构建并显示到dispatch.php。

这是来自dispatch-grid.php的查询,它调用数据库并在dispatch.php上显示网格:

 <?php
   $select = "SELECT DISTINCT * FROM `dispatch_read`" . " WHERE " . $where . ";";
   $QueryResult = @mysql_query($select) or die ();
   $resnum = mysql_num_rows($QueryResult);

   if($resnum == 0){
      echo "<div>Your search returned no results</div>";
   }
   else {
     echo "<table>\n";
     echo "<thead><tr>" .
     echo "<th>BOL</th>" .
     echo "<th>CONTAINER</th>" .
     echo "<th>STATUS</th>" . 
     echo "</tr></thead>" .
     echo "<tbody>\n";

     while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE){
     echo "<tr>";
     echo "<td>{$Row[BOL]}</td>";
     echo "<td>{$Row[CONTAINER]}</td>";
     echo "<td>{$Row[STATUS]}</td>";
     echo "</tr></tbody>\n";
     echo "</table>\n";
     }
    }

我显示的行数量更多。我只是想尽可能地缩短它。

我可以使用此函数在dispatch.php上显示此网格:

 displayrecords();

此时,网格显示在dispatch.php上。我现在需要做的是将查询字符串从dispatch-grid.php($ select)传递到另一个名为getreport.php的页面,在那里我可以将网格导出到Excel工作表中。

在dispatch.php上,我有一个输入按钮:

 <input onclick="getreport()" type="button" value="Get Report" />

该按钮在同一页面上调用javascript函数:

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

此时,我可以打开Excel工作表,但它打开了空白。我不确定是否有必要展示,但这里是getreport.php上的代码:

 <?php
 include("include/database.php";

 global $header;
 global $data;
 global $ts;
 $ts = date('mdY-His');
 $sep = "\t";
 $filename = "excelfilename";

 *** UPDATE ***
 $sql = "SELECT DISTINCT * FROM dispatch_read WHERE " . $_GET['where'] . ";";

 $result = @mysql_query($sql) or die ("Couldn't execute query" . mysql_error());
 $file_ending = "xls";

 header("Content-Type: application/xls");
 header("Content-Disposition: attachment; filename=$filename.xls");
 header("Pragma: no-cache");
 header("Expires: 0");

 $sep = "\t";
 for ($i = 0; $i < mysql_num_fields($result); $i++) {
 echo mysql_field_name($result,$i) . "\t";
 }
 print("\n");
   while($row = mysql_fetch_row($result))
   {
     $schema_insert = "";
     for($j=0; $j<mysql_num_fields($result);$j++)
     {
       if(!isset($row[$j]))
         $schema_insert .= "NULL".$sep;
       elseif ($row[$j] != "")
         $schema_insert .= "$row[$j]".$sep;
       else
         $schema_insert .= "".$sep;
     }
   $schema_insert = str_replace($sep."$", "", $schema_insert);
   $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t";
   print(trim($schema_insert));
   print "\n";
 }
 ?>  

请忽略任何错别字。我能够显示网格没有问题。我的代码一直运行,直到他们单击getreport按钮。

我猜我需要做的是将查询($ select)从dispatch-grid.php发送到getreport.php,但我不知道该怎么做。

我尝试在dispatch.php上执行此操作:

 $query_string = $_SERVER['QUERY_STRING'];

然后我尝试将它发送到getreport.php,但是我没有成功发送它。

我将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

你可以简单地改变这个函数(getreport)来传递getreport.php所需的任何参数

function getreport(){
   window.location = "getreport.php?param1=hello&param2=world";
 }

所以在getrport.php中,你可以简单地访问这些参数,

echo $_GET['param1'];
echo $_GET['param2'];

<强>更新

你想要传递的是, $ select =“SELECT DISTINCT * FROM dispatch_read”。 “在哪里”。 $ where。 “;”;

我建议的是只将$ where变量值传递给getreport.php。

function getreport(){
   window.location = "getreport.php?where=hello"; // where param should hold the value of $where 
 }

所以在 getreport.php 中,您可以按照以下方式访问它并执行相同操作。

$ select =“SELECT DISTINCT * FROM dispatch_read”。 “在哪里”。 $ _GET ['where']。 “;”;