2013-12-16 39 views
0

我有三个文件。主文件是dispatch.php。从这个文件中,用户可以进行各种选择,在另一个页面dispatch-grid.php上使用查询构建器。在dispatch-grid.php中,查询被构建并显示给dispatch.php。PHP将查询字符串传递到另一页

下面是从调度-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网格。我现在需要做的是将querystring从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按钮。

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

我试过dispatch.php这样做:

$query_string = $_SERVER['QUERY_STRING']; 

我然后试图发送到getreport.php,但我没有成功发送。

我将不胜感激任何帮助。

谢谢。

+0

什么值$在哪里成立? – dev1234

+0

$其中包含用户通过下拉选择进行的各种选择。我没有展示它,因为我不认为有必要展示。此时网格显示在dispatch.php上。我需要做的就是将$ select从dispatch-grid.php发送到getreport.php。 – HoodCoderMan

+0

查看下面的答案。 – dev1234

回答

0

你可以简单地改变这种功能(getreport)到所需要的任何PARAMS传递给getreport.php

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

所以在getrport.php,你可以简单地访问这些PARAMS为,

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

更新

你的意思是,$ select =“SELECT DISTINCT * FROM dispatch_read“。 “在哪里”。 $哪里。 “;”;

我的建议是只将$ 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']。 “;”;

+0

我不确定我是否遵循这一点。如何使用上面显示的内容向我发送$ select?提前感谢您的帮助。 – HoodCoderMan

+0

@JohnBeasley我更新了答案。检查 – dev1234

+0

我收到此错误:'where子句'中的未知列'$ where'。我究竟做错了什么? – HoodCoderMan

相关问题