之前討論過同樣的問題,如果我們期望調用頭部函數,我也知道沒有任何東西可以發送到瀏覽器。我在這裏做錯了,我之前正在打印HTML div標籤,所以解決方案就是遵循一個基本策略,以便一切都可以工作,我想成爲!僅供參考,我可以通過AJAX(使用jQuery),只需從被調用的PHP腳本返回JSON編碼輸出作爲響應(可能是結果部分或錯誤,我在下面的代碼中顯示)來做到這一點 - 所有事情都可以通過使用AJAX來實現。但是,因爲它是一個搜索頁面,我正在向用戶提供設備,例如URL中的search_query的url重寫,所以如果用戶需要,將不需要填寫表單 - 因此使用GET方法。下面是我的代碼我實現這一目標:PHP header未按預期方式工作
<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 -->
FYI:$ SEARCH_QUERY以上是$ _GET [ 'SEARCH_QUERY']一個別名
一切都在上面的代碼工作,除了當塊,其中header()被調用。如前所述,我知道原因。只要幫助我檢查頁面頂部的所有IF塊中的所有內容,然後顯示搜索表單,然後在num_rows大於0時顯示搜索查詢的輸出或顯示錯誤(如上面的代碼中所示)if num_rows MySQL查詢結果的結果爲0.對於我蹩腳的語言(也不適合格式化)抱歉。 )
編輯: 邏輯很簡單:
- $ _GET [ 'SEARCH_QUERY']然後執行查詢並檢查NUM_ROWS
- NUM_ROWS == 0則發送重定向到同一頁與「?err = example_error_message」並顯示上面的錯誤消息形式並不打印結果輸出在同一時間
- num_rows> 0然後打印$行值(讓我們說,最終輸出),因爲我已經做了以上代碼
- 沒有$ _GET [ 'SEARCH_QUERY']那麼只有搜索表單
所以,請大家幫我實現這些。
我不確定問題是什麼。您知道標題調用不起作用,您是否要求人們查看代碼? – Jim 2012-02-24 17:45:31
所以它打破了,你知道原因? – 2012-02-24 17:47:02
@Jim:是的,我知道解決方案,但無法重構我的代碼以實現預期的輸出,因爲我想要。 – Vishal 2012-02-24 17:52:32