Problems with header command

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Problems with header command

Post by kdidymus »

I'm confused on this one. I've got a PHP / MySQL search engine on my site.

http://www.didymus.org.uk/tree.php

Enter a search query and click SEARCH and it works fine.

If no search parameter is specified and the SEARCH button is clicked, the code SHOULD re-direct to a page called search2.php.

However, it doesn't. It returns the following errror:
Warning: Cannot modify header information - headers already sent by (output started at /home/kdidymus/public_html/query.php:9) in /home/kdidymus/public_html/query.php on line 21
Not sure what I've done wrong. Here is the code for the query.php page:

Code: Select all

<?php
/* Program: query.php
 * Desc:    Displays search results.
 */
?>
<html>
<head><title>Individual Biographical Information</title></head>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" style="border-style: solid; border-width: 1px">
<?php
 
  // Get the search variable from URL
  $var = $_GET['q'] ; //search term passed by search.php
  $trimmed = trim($var); //trim whitespace from the stored variable
 
// rows to return
$limit=50; 
 
// check for an empty string and re-direct.
if ($trimmed == "")
  {
  header("Location: search2.php");
  exit;
  }
 
// check for a search parameter
if (!isset($var))
  {
  header("Location: search2.php");
  exit;
  }
  //connect to your database
  include_once("*******.inc.php");
  mysql_connect("$host","$user","$password"); //(host, username, password)
 
  //specify database
  mysql_select_db($database) or die("Unable to select database");
 
// Build SQL Query  
  $query = "select * from tree WHERE (urn LIKE \"%$trimmed%\" OR surname LIKE \"%$trimmed%\" OR forename LIKE \"%$trimmed%\" OR yearofbirth LIKE \"%$trimmed%\") order by surname,forename"; // EDIT HERE and specify your table and field names for the SQL query
 
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 $link = "http://www.didymus.org.uk/display.php?urn=";
 $rowht = "valign='top'";
 $rowcl = "bgcolor='#efffef' valign='top'";
 $text = "font face='Arial' size='1'";
 $hltext = "font face='Arial' size='1' color='#000000'";
 $nw = "nowrap";
 
// If we have no results, offer A-Z as alternative.
 
if ($numrows == 0)
  {
  echo "<p align='center'><font face='Arial' size='2' color='#00000'>Sorry, your search for <b>"" . $trimmed . ""</b> returned no results.</font></p>";
 
// google
  echo "<p align='center'><font face='Arial' size='2' color='#00000'>Click </font><a href='a-zsn.php'><font face='Arial' size='2' color='#00000'>here</font></a><font face='Arial' size='2' color='#00000'> to try the A-Z index <i>or</i> </font><a href='search.php'><font face='Arial' size='2' color='#00000'>here</font></a><font face='Arial' size='2' color='#00000'> to try another search.</font></p>";
exit;
  }
 
 
// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }
 
// get results
  $query .= " limit $s,$limit";
  $result = mysql_query ($query) or die(mysql_error());
 
 
 
// display what the person searched for
 
// begin to show results set
echo "<p align='center'><font face='Arial' size='3'><b>Results</b> for your search for <b>"" . $var . "&quot</b></p>";
$count = 1 + $s ;
 
// now you can display the results returned
 
  echo "<table cellspacing='0' width='450' align='center'>";
  echo "<tr><td colspan='4'><hr /></td></tr>";
  echo "<tr $rowht><td><$text><b>SURNAME</b></font></td><td><$text><b>FORENAME</b></font></td><td><$text><b>BIRTH YEAR</b></font></td><td><$text><b>VIEW</b></font></td>\n</tr>";
  echo "<tr><td colspan='4'><hr /></td></tr>";
  /* Display results in a table */
  $i=0;
  while ($row = mysql_fetch_array($result))
  {
  extract($row);
  $bg = ($i%2) ? $rowht : $rowcl;
  echo "<tr $bg><td><$text>$surname</font></td><td><$text>$forename</font></td><td><$text>$yearofbirth</font></td><td><a href='$link$urn'><$hltext>VIEW</font></td>\n</tr>";
  $i++;
  $count++ ; 
 
  }
 
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
 
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp&nbsp;";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }
 
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p align='center'>Showing results $b to $a of $numrows</p>";
  
?>
Any suggestions?!

Kris.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Problems with header command

Post by VladSun »

You can't use header() after you've echoed anything.
There are 10 types of people in this world, those who understand binary and those who don't
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Problems with header command

Post by kdidymus »

Ah. Okay.

Problem is, there is no echo above those lines. I'm still confused!

Sorry.

Kris
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Problems with header command

Post by VladSun »

Code: Select all

 
?>
<html>
<head><title>Individual Biographical Information</title></head>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" style="border-style: solid; border-width: 1px">
<?php
This is also considered an "echo"...
There are 10 types of people in this world, those who understand binary and those who don't
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: Problems with header command

Post by kdidymus »

You're a star. I swapped the file around a bit and it works fine now.

Thank you.

Kris.
Post Reply