Same problem was discussed earlier and I also knew that nothing, not also a whitespace can be sent to browser before if we’ve expected call to header function. I am doing wrong here, I am printing HTML div tags before, so solution is to just follow a basic strategy so as everything would work that I wanted to be! FYI, I can do this with AJAX (Using jQuery) by just returning JSON encoded output from called php script as a response (it may be result part or error I am displaying in below code)–everything would work by using AJAX. But as it’s a search page, I am availing facility to user like url rewriting for search_query in the URL so there will be no need to fill the form if user wants–Hence using GET method. Below is my code I’m achieving this:
<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 above is a alias of $_GET[‘search_query’]
Everything in above code works except the if blocks where header() is called. I know the reason, as mentioned earlier. Just help me to check everything at top of page which is in IF blocks I’ve been using, then display search form and then echoed output of search query when num_rows are more than 0 or display error (as in in above code) if num_rows of result of MySQL query is 0. Sorry for my poor language (also, not proper formating too) as I’m in a hurry. 😉
EDIT:
Logic is simple:
- $_GET[‘search_query’] then execute query and check for num_rows
- num_rows==0 then send redirect to same page with “?err=example_error_message” and display that error message above form and do not print the results output at the same time
- num_rows>0 then print $row values (let us say, final output) as I’ve done in above code
- No $_GET[‘search_query’] then only search form
So please help me to achieve these.
I’m not entirely sure whether I understand your question, but I’ll give it a go anyway.
When you are working with PHP’s
headerfunctions, you must indeed make sure not to write any output before the call to theheaderfunction.Sometimes this is however difficult to do. In order to solve this, you need to use Output buffering. Put
ob_start()(docs) as the very first line of your script, andob_end_flush(docs) orob_end_clean(docs) as the last line (or after the use of the header functions). This effectively buffers all output, which allows you to call the header functions before any output is actually echoed (as all output is buffered until you callob_end_flushorob_end_clean).