如何打印(导出)到 MS-Word 搜索结果 (PHP)
How to print (export) to MS-Word search result (PHP)
我正在尝试使用PHP[=导出到MS Word搜索结果24=] 但我的代码不起作用。
这是我的代码:
<?php
error_reporting(0);
include("configsample.php");
?>
<html>
<title>Home</title>
<head>
<meta charset="UTF-8">
<title>Flat Login Form</title>
</head>
<body>
<form class="login-form" method="post" action="viewstucon.php">
<select class="s" id="course" name="course">
<option value="">Select Course</option>
<option value="Bachelor of Secondary Education"<?php if (!(strcmp("Bachelor of Secondary Education", $_POST["course"]))) {echo "selected=\"selected\"";} ?>>Bachelor of Secondary Education</option>
<option value="Bachelor of Science in Automotive Technology"<?php if (!(strcmp("Bachelor of Science in Automotive Technology", $_POST["course"]))) {echo "selected=\"selected\"";} ?>>Bachelor of Science in Automotive Technology</option>
<option value="Bachelor of Science in Electrical Technology"<?php if (!(strcmp("Bachelor of Science in Electrical Technology", $_POST["course"]))) {echo "selected=\"selected\"";} ?>>Bachelor of Science in Electrical Technology</option>
</select>
<input class="sy" name="from" type="text" id="from" size="10" width="100%" value="<?php echo $_REQUEST["from"]; ?>" placeholder="From"/>
<input class="sy" name="to" type="text" id="to" size="10" width="100%" value="<?php echo $_REQUEST["to"]; ?>" placeholder="To"/>
<button type="submit" name="submit" >Filter</button>
</form>
<table width="100%" border="1" style="border: 1px solid #999999; border-collapse: collapse; margin:0 auto; background:#ffffff; border-radius:20px;">
<tr style="background:#3366cc; color:#ffffff; font:normal 15px Tahoma; height:40px;">
<th>Research Title</th>
<th>Year</th>
<th>Proponent(s)</th>
</tr>
<?php
if ($_REQUEST["course"]<>'') {
$search_course = " AND restu_course='".mysql_real_escape_string($_REQUEST["course"])."'";
}
if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year BETWEEN '".mysql_real_escape_string($_REQUEST["from"])."' AND '".mysql_real_escape_string($_REQUEST["to"])."'".$search_course;
} else if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["from"])."'".$search_course;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["to"])."'".$search_course;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_id>0".$search_course;
}
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result))
{
echo'<tr style="font:normal 12px Tahoma; color:#333333;">
<td style="padding:10px;">'.$row['restu_title'].'</a></td>
<td style="padding:10px;">'.$row['restu_year'].'</td>
<td style="padding:10px;">'.$row['restu_by'].'</td>
</tr>';
}
}else
{
echo'<tr><td colspan="5">No results found.</td>';
}
?>
</table>
<a href="print.php">Export to Word</a>
</body>
</html>
这里是 print.php 代码:
<?php
error_reporting(0);
include("configsample.php");
?>
<?php
HEADER("Content-Type: application/msword");
HEADER("Content-Disposition: attachment; filename=database_dump.doc");
HEADER("Pragma: no-cache");
HEADER("Expires: 0");
if ($_REQUEST["course"]<>'') {
$search_course = " AND restu_course='".mysql_real_escape_string($_REQUEST["course"])."'";
}
if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year BETWEEN '".mysql_real_escape_string($_REQUEST["from"])."' AND '".mysql_real_escape_string($_REQUEST["to"])."'".$search_course;
} else if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year >= '".mysql_real_escape_string($_REQUEST["from"])."'".$search_course;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["to"])."'".$search_course;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_id>0".$search_course;
}
echo"<table border='1' width='100%' style='border: 1px solid #999999; border-collapse: collapse; margin:0 auto; background:#ffffff; border-radius:20px;'>
<tr style='background:#3366cc; color:#ffffff; font:normal 15px Tahoma; height:40px;'>
<th>Research No.</th>
<th>Course</th>
<th>Year</th>
</tr>";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
echo'<tr style="font:normal 12px Tahoma; color:#333333;">
<td style="padding:10px;">'.$row['restu_id'].'</td>
<td style="padding:10px;">'.$row['restu_course'].'</td>
<td style="padding:10px;">'.$row['restu_year'].'</td>
</tr>';
}
echo"</table>";
}
?>
这里的问题是它导出所有 Table 列表而不是仅导出搜索结果。
您在print.php中的$_REQUEST数组是空的,页面不知道搜索的参数。
您可以尝试通过 GET 方法通过这种方式传递参数:
正在替换
<a href="print.php">Export to Word</a>
来自
<a href="print.php?<?php
echo "course=".$_REQUEST["course"]."&from=".$_REQUEST["from"]."&to=".$_REQUEST["to"];
?>">Export to Word</a>
在第一个文件中
我正在尝试使用PHP[=导出到MS Word搜索结果24=] 但我的代码不起作用。
这是我的代码:
<?php
error_reporting(0);
include("configsample.php");
?>
<html>
<title>Home</title>
<head>
<meta charset="UTF-8">
<title>Flat Login Form</title>
</head>
<body>
<form class="login-form" method="post" action="viewstucon.php">
<select class="s" id="course" name="course">
<option value="">Select Course</option>
<option value="Bachelor of Secondary Education"<?php if (!(strcmp("Bachelor of Secondary Education", $_POST["course"]))) {echo "selected=\"selected\"";} ?>>Bachelor of Secondary Education</option>
<option value="Bachelor of Science in Automotive Technology"<?php if (!(strcmp("Bachelor of Science in Automotive Technology", $_POST["course"]))) {echo "selected=\"selected\"";} ?>>Bachelor of Science in Automotive Technology</option>
<option value="Bachelor of Science in Electrical Technology"<?php if (!(strcmp("Bachelor of Science in Electrical Technology", $_POST["course"]))) {echo "selected=\"selected\"";} ?>>Bachelor of Science in Electrical Technology</option>
</select>
<input class="sy" name="from" type="text" id="from" size="10" width="100%" value="<?php echo $_REQUEST["from"]; ?>" placeholder="From"/>
<input class="sy" name="to" type="text" id="to" size="10" width="100%" value="<?php echo $_REQUEST["to"]; ?>" placeholder="To"/>
<button type="submit" name="submit" >Filter</button>
</form>
<table width="100%" border="1" style="border: 1px solid #999999; border-collapse: collapse; margin:0 auto; background:#ffffff; border-radius:20px;">
<tr style="background:#3366cc; color:#ffffff; font:normal 15px Tahoma; height:40px;">
<th>Research Title</th>
<th>Year</th>
<th>Proponent(s)</th>
</tr>
<?php
if ($_REQUEST["course"]<>'') {
$search_course = " AND restu_course='".mysql_real_escape_string($_REQUEST["course"])."'";
}
if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year BETWEEN '".mysql_real_escape_string($_REQUEST["from"])."' AND '".mysql_real_escape_string($_REQUEST["to"])."'".$search_course;
} else if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["from"])."'".$search_course;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["to"])."'".$search_course;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_id>0".$search_course;
}
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result))
{
echo'<tr style="font:normal 12px Tahoma; color:#333333;">
<td style="padding:10px;">'.$row['restu_title'].'</a></td>
<td style="padding:10px;">'.$row['restu_year'].'</td>
<td style="padding:10px;">'.$row['restu_by'].'</td>
</tr>';
}
}else
{
echo'<tr><td colspan="5">No results found.</td>';
}
?>
</table>
<a href="print.php">Export to Word</a>
</body>
</html>
这里是 print.php 代码:
<?php
error_reporting(0);
include("configsample.php");
?>
<?php
HEADER("Content-Type: application/msword");
HEADER("Content-Disposition: attachment; filename=database_dump.doc");
HEADER("Pragma: no-cache");
HEADER("Expires: 0");
if ($_REQUEST["course"]<>'') {
$search_course = " AND restu_course='".mysql_real_escape_string($_REQUEST["course"])."'";
}
if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year BETWEEN '".mysql_real_escape_string($_REQUEST["from"])."' AND '".mysql_real_escape_string($_REQUEST["to"])."'".$search_course;
} else if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year >= '".mysql_real_escape_string($_REQUEST["from"])."'".$search_course;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_year = '".mysql_real_escape_string($_REQUEST["to"])."'".$search_course;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE restu_id>0".$search_course;
}
echo"<table border='1' width='100%' style='border: 1px solid #999999; border-collapse: collapse; margin:0 auto; background:#ffffff; border-radius:20px;'>
<tr style='background:#3366cc; color:#ffffff; font:normal 15px Tahoma; height:40px;'>
<th>Research No.</th>
<th>Course</th>
<th>Year</th>
</tr>";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
echo'<tr style="font:normal 12px Tahoma; color:#333333;">
<td style="padding:10px;">'.$row['restu_id'].'</td>
<td style="padding:10px;">'.$row['restu_course'].'</td>
<td style="padding:10px;">'.$row['restu_year'].'</td>
</tr>';
}
echo"</table>";
}
?>
这里的问题是它导出所有 Table 列表而不是仅导出搜索结果。
您在print.php中的$_REQUEST数组是空的,页面不知道搜索的参数。 您可以尝试通过 GET 方法通过这种方式传递参数:
正在替换
<a href="print.php">Export to Word</a>
来自
<a href="print.php?<?php
echo "course=".$_REQUEST["course"]."&from=".$_REQUEST["from"]."&to=".$_REQUEST["to"];
?>">Export to Word</a>
在第一个文件中