PDA

View Full Version : Value or Die?


rattlsnak
08-20-2008, 04:56 AM
Ok, more questions!

On one of my pages I have a search form with three available options to search (with their own searchfields, same form):

Lastname________
Team________
Division_____
[submit]

I have used the follwing code (see below) to perform a search depending on which textfield they enter data into. It works fine when they enter a value that is in the database, but the problem I'm having is:

A: If they enter something into the textfield that isn't in the database, the page appears blank. (nothing to echo) Because of the code I have there, it is obviously simply looking for something in the textfield, not whether it's in the DB.
How can I correct this? I have a page that says "no results found" but cant get it to redirect there.

B. I have more than one person with the same last name and need to display them all if requestes, but i cant seem to get the WHILE statement to work either. Is that the correct loop to use?

C. I also eventually want it to redirect to another 'pretty' page to display the results, but not sure where to put that header.

D. Is there a way to echo all those statements in a single place instead of repeating them?

**I have changed the names of the variables and such to protect the innocent! :)

$query1="SELECT * FROM bandits WHERE l_name='$l_name'";
$query2="SELECT * FROM bandits WHERE team='$team'";
$query3="SELECT * FROM bandits WHERE div='$division'";

if ($l_name)
{
$results=mysql_query($query1) or die (mysql_error());
$row = mysql_fetch_array($results);
echo $row['l_name'];
echo $row['team'];
echo $row['div'];
}
elseif ($team)
{
$results=mysql_query($query2) or die (mysql_error());
$row = mysql_fetch_array($results);
echo $row['l_name'];
echo $row['team'];
echo $row['div'];
}
elseif ($division)
{
$results=mysql_query($query3) or die (mysql_error());
$row = mysql_fetch_array($results);
echo $row['l_name'];
echo $row['team'];
echo $row['div'];
}
else
{ header("location:noresultsfound.php");
}
?>

rattlsnak
08-21-2008, 09:10 PM
ok, this worked, changed the order of a few things...

if ($l_name)
{
$results=mysql_query("SELECT * FROM bandits WHERE l_name='$l_name'") or die (mysql_error());
}
elseif ($team)
{
$results=mysql_query("SELECT * FROM bandits WHERE team='$team'") or die (mysql_error());
}
elseif ($division)
{
$results=mysql_query("SELECT * FROM bandits WHERE div='$division'") or die (mysql_error());
}
else
{
header("Location: noresultsfound.php");
exit();
}

if (mysql_num_rows($results) == 0)
echo "no results";

while ($row = mysql_fetch_array($results))
{
echo $row['l_name'];
echo $row['team'];
echo $row['div'];
echo '<br>';
}