前面讨论过同样的问题,我也知道,如果我们期望调用头函数,那么之前什么都不能发送到浏览器,空白也不能发送到。我在这里做得不对,我以前打印HTMLdiv标记,所以解决方案是只遵循一个基本策略,这样一切都可以按照我想要的方式工作!仅供参考,我可以使用AJAX(使用jQuery),只需从调用的php脚本返回JSON编码的输出作为响应(可能是结果部分,也可能是我在下面的代码中显示的错误)——一切都可以通过使用AJAX来实现。但由于这是一个搜索页面,我正在利用类似用户的功能在url中重写search_query,因此如果用户愿意,就不需要填写表单——因此使用GET方法。以下是我正在实现的代码:
<div class="two-third">
<?php if($_GET['err'] == "no_results_found") {
echo "<p class='error-box'>No results found for your query. Please search using different specific keywords.</p>";
} ?>
<form method="GET" action="<?php echo BASE_URL; ?>/content.php" id="search_content_form">
<fieldset>
<div>
<label>Search this site: </label>
<input type="text" title="Enter your query" class="form-poshytip" id="search_query" name="search_query" value="<?php echo $search_query; ?>">
</div>
<p><input type="submit" id="search_content_submit_button" value="Search Content"></p>
</fieldset>
</form>
<?php if(isset($search_query)) {
$query = "SELECT * FROM (
(SELECT 'event' AS content_type, event_id AS content_id, event_title AS content_title, publishing_time AS content_publishing_time FROM event WHERE event_title LIKE '%".$search_query."%' OR content LIKE '%".$search_query."%')
UNION
(SELECT 'article' AS content_type, article_id AS content_id, article_title AS content_title, publishing_time AS content_publishing_time FROM article WHERE article_title LIKE '%".$search_query."%' OR content LIKE '%".$search_query."%')
) content_result
ORDER BY content_result.content_publishing_time DESC
LIMIT 500
";
$result = mysql_query($query);
if(!$result) {
header("location:".BASE_URL."/content.php");
}
if(mysql_num_rows($result) == 0) {
header("location:".BASE_URL."/content.php?err=no_results_found");
}
$i = 0;
echo "<div id='search_content_results' class='list'>";
echo "Found <strong>".mysql_num_rows($result)."</strong> result(s) for search query: '"<strong>".$search_query."</strong>'"";
while($row = mysql_fetch_assoc($result)) {
// extract all needed varaibles from $row
$content_type = $row['content_type'];
$content_id = $row['content_id'];
$content_title = $row['content_title'];
$content_publishing_time = $row['content_publishing_time'];
echo "
<ul>
<li><a href='".BASE_URL."/".$content_type."s.php?".$content_type."_id=".$content_id."'>".($i+1).") <strong>".$content_title."</strong> <span style='font-size: 12px; text-decoration: italic;'>(Published On: ".date('M jS Y', strtotime($content_publishing_time)).")</span></a></li>
</ul>
";
$i++;
}
echo "</div>";
}
?>
</div>
<!-- ENDS two-third -->
仅供参考:上面的$search_query是$_GET['search_query']的别名
除了调用header()的if块之外,上面代码中的所有内容都可以工作。正如前面提到的,我知道原因。只要帮我检查页面顶部我一直在使用的IF块中的所有内容,然后当num_rows大于0时显示搜索表单,然后返回搜索查询的输出,或者如果MySQL查询结果的num_row斯为0,则显示错误(如上面代码中所示)。很抱歉我的语言不好(而且格式也不正确),因为我很着急
编辑:逻辑很简单:
- $_GET['search_query']然后执行查询并检查num_rows
- num_rows==0,然后用"?err=example_error_message"将重定向发送到同一页面,并在表单上方显示该错误消息,同时不打印结果输出
- num_rows>0然后打印$row值(比如说,最终输出),就像我在上面的代码中所做的那样
- 没有$_GET['search_query'],则只有搜索表单
所以请帮助我实现这些目标。
我不完全确定我是否理解你的问题,但无论如何我都会试一试。
当您使用PHP的header
函数时,您必须确保在调用header
函数之前不编写任何输出。
然而,有时这很难做到。为了解决这个问题,您需要使用输出缓冲。将ob_start()
(文档)作为脚本的第一行,将ob_end_flush
(文档)或ob_end_clean
(文档)用作最后一行(或在使用头函数之后)。这有效地缓冲了所有输出,允许您在任何输出被实际回显之前调用头函数(因为所有输出都被缓冲,直到您调用ob_end_flush
或ob_end_clean
)。
除了使用Joris提到的ob_start
外,请记住在设置Location
标头后始终使用exit
或die()
(除非在某些情况下您确切地知道自己在做什么)。