2012-02-24 117 views
0

之前讨论过同样的问题,如果我们期望调用头部函数,我也知道没有任何东西可以发送到浏览器。我在这里做错了,我之前正在打印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']那么只有搜索表单

所以,请大家帮我实现这些。

+0

我不确定问题是什么。您知道标题调用不起作用,您是否要求人们查看代码? – Jim 2012-02-24 17:45:31

+0

所以它打破了,你知道原因? – 2012-02-24 17:47:02

+0

@Jim:是的,我知道解决方案,但无法重构我的代码以实现预期的输出,因为我想要。 – Vishal 2012-02-24 17:52:32

回答

2

我不完全确定自己是否理解了您的问题,但无论如何我都会放弃它。

当您使用PHP的header函数时,您必须确保在调用header函数之前不要写任何输出。

有时候这很难做到。为了解决这个问题,你需要使用输出缓冲。将ob_start()docs)作为脚本的第一行,并将ob_end_flushdocs)或ob_end_cleandocs)作为最后一行(或在使用标头函数之后)。这有效地缓冲了所有输出,这允许您在任何输出实际回显之前调用标题函数(因为所有输出都被缓冲,直到您拨打ob_end_flushob_end_clean)。

+1

工作就像一个魅力!除了使用这两个功能外,我不需要改变任何东西。 --- 还可以根据我的预期在上面的代码中用'isset($ search_query)'检查'strlen($ search_query)> 0'。谢谢你的方式。 – Vishal 2012-02-24 18:06:31

1

除了由里斯提到使用ob_start,记得要经常exitdie()设置Location头之后(除非在某些情况下,如果你知道自己在做什么)。

+0

是的,这是必须的,如果我没有,那么它会缓冲我不想成为的那些东西。也感谢你指出这一点。 – Vishal 2012-02-24 18:08:54