Jump to content

search results question


boo_lolly

Recommended Posts

i'm working on a simple search and results project. i don't have the database setup yet, but i'm working on that as we speak. i'd like for this to work the FIRST time i run it after the database is running. however, i'd like to add something to my search/results project and i need a little direction... i want to add an option on the search.php page to allow users to view ALL the contents of the SQL table. the search only needs to cycle through the contents of ONE table in the database. here's the catch, i've designed my results.php to bring up an error message if the user didn't input any information into one of the search fields. i need this "VIEW ALL" button to over-ride that. or at least have the same effect. here's what i have so far...
[code=php]
<!-- search.php -->
<HTML>
<HEAD><TITLE>Search Registry</TITLE></HEAD>

<BODY>

<form method="POST" action="results.php">

First Name:<input type="text" name="fname"><BR>

Last Name:<input type="text" name="lname"><FONT COLOR="FF0000" SIZE="-1">(required)</FONT><BR>

<TABLE><TR><TD>
<SELECT NAME="event_day">
<OPTION VALUE="">select a day
<OPTION VALUE="01">01
<OPTION VALUE="02">02
<OPTION VALUE="03">03
<OPTION VALUE="04">04
<OPTION VALUE="05">05
<OPTION VALUE="06">06
<OPTION VALUE="07">07
<OPTION VALUE="08">08
<OPTION VALUE="09">09
<OPTION VALUE="10">10
<OPTION VALUE="11">11
<OPTION VALUE="12">12
<OPTION VALUE="13">13
<OPTION VALUE="14">14
<OPTION VALUE="15">15
<OPTION VALUE="16">16
<OPTION VALUE="17">17
<OPTION VALUE="18">18
<OPTION VALUE="19">19
<OPTION VALUE="20">20
<OPTION VALUE="21">21
<OPTION VALUE="22">22
<OPTION VALUE="23">23
<OPTION VALUE="24">24
<OPTION VALUE="25">25
<OPTION VALUE="26">26
<OPTION VALUE="27">27
<OPTION VALUE="28">28
<OPTION VALUE="29">29
<OPTION VALUE="30">30
<OPTION VALUE="31">31
</SELECT>
</TD>
<TD>
<SELECT NAME="event_month">
<OPTION VALUE="">select a month
<OPTION VALUE="01">January
<OPTION VALUE="02">February
<OPTION VALUE="03">March
<OPTION VALUE="04">April
<OPTION VALUE="05">May
<OPTION VALUE="06">June
<OPTION VALUE="07">July
<OPTION VALUE="08">August
<OPTION VALUE="09">September
<OPTION VALUE="10">October
<OPTION VALUE="11">November
<OPTION VALUE="12">December
</SELECT>
</TD>
<TD>
<SELECT NAME="event_year">
<OPTION VALUE="">select a year
<OPTION VALUE="2002">2002
<OPTION VALUE="2003">2003
<OPTION VALUE="2004">2004
<OPTION VALUE="2005">2005
<OPTION VALUE="2006">2006
<OPTION VALUE="2007">2007
<OPTION VALUE="2008">2008
<OPTION VALUE="2009">2009
<OPTION VALUE="2010">2010
</SELECT>
</TD>
</TR>
</TABLE><BR>

<input type="SUBMIT" value="Search">
</form>

<?php

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$event_day = $_POST['event_day'];
$event_month = $_POST['event_month'];
$event_year = $_POST['event_year'];
?>

</BODY>
</HTML>
[/code]
[code=php]
<!-- RESULTS.PHP -->

<?php

@ $db = mysql_connect("ya, blah, blah");

if(!$db)
{
      echo "Error: Could not connect to the database. Please try again later.";
      exit;
}

trim($lname);
if (!$lname)
{
      echo "<FONT COLOR=FF0000>You have not filled the required fields. Please try again.</FONT>";
      include "search.inc";
      exit;
}

mysql_select_db("registry_DB, $db);

$sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error();
$result = mysql_query($sql);
$num_result = mysql_num_rows($result);


echo "Number of matches: ". $num_result ."<br />";

if(!$result)
{
echo "Sorry, there were no matches for your query. Please try again.";
}
else
{
echo "<TABLE BORDER=1><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH>&nbsp;</TH></TR>";

for($i=0; $i < $num_result; $i++)
{
$row = mysql_fetch_array($result);
echo "<TR><TD>". $row['brideFname'] ." ". $row['brideLname'] ."</TD><TD>". $row['groomFname'] ." ". $row['groomLname'] ."</TD><TD>". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."</TD><TD>". $row['uID'] ."</TD></TR><br />";
}

echo "</TABLE>";
}
mysql_close($db);
?>
[/code]

how do i approach this task?
Link to comment
Share on other sites

[quote author=boo_lolly link=topic=115214.msg469004#msg469004 date=1163693387]
i'm working on a simple search and results project. i don't have the database setup yet, but i'm working on that as we speak. i'd like for this to work the FIRST time i run it after the database is running. however, i'd like to add something to my search/results project and i need a little direction... i want to add an option on the search.php page to allow users to view ALL the contents of the SQL table. the search only needs to cycle through the contents of ONE table in the database. here's the catch, i've designed my results.php to bring up an error message if the user didn't input any information into one of the search fields. i need this "VIEW ALL" button to over-ride that. or at least have the same effect. here's what i have so far...[/quote]


You could try this:

[code]<!-- search.php -->
<HTML>
<HEAD><TITLE>Search Registry</TITLE></HEAD>

// Snip

<input type="SUBMIT" value="Search">
</form>

<!-- NEW LINE -->
<a href=results.php?showall=TRUE">Show All</a>
<!-- NEW LINE -->
</BODY>
</HTML>[/code]


[code=php]
<!-- RESULTS.PHP -->

<?php
$showall = $_GET['showall'];

@ $db = mysql_connect("ya, blah, blah");

if(!$db)
{
      echo "Error: Could not connect to the database. Please try again later.";
      exit;
}

trim($lname);
if (!$lname)
{
      echo "<FONT COLOR=FF0000>You have not filled the required fields. Please try again.</FONT>";
      include "search.inc";
      exit;
}

if(!$showall)
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$event_day = $_POST['event_day'];
$event_month = $_POST['event_month'];
$event_year = $_POST['event_year'];
mysql_select_db("registry_DB", $db);

$sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error());
$result = mysql_query($sql);
$num_result = mysql_num_rows($result);


echo "Number of matches: ". $num_result ."<br />";

if(!$result)
{
echo "Sorry, there were no matches for your query. Please try again.";
}
else
{
echo "<TABLE BORDER=1><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH>&nbsp;</TH></TR>";

for($i=0; $i < $num_result; $i++)
{
$row = mysql_fetch_array($result);
echo "<TR><TD>". $row['brideFname'] ." ". $row['brideLname'] ."</TD><TD>". $row['groomFname'] ." ". $row['groomLname'] ."</TD><TD>". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."</TD><TD>". $row['uID'] ."</TD></TR><br />";
}

echo "</TABLE>";
}
mysql_close($db);
}
if($showall = "TRUE")
{
mysql_select_db("registry_DB", $db);

$sql = mysql_query("SELECT * FROM my_search_table") or die(mysql_error());
$result = mysql_query($sql);
$num_result = mysql_num_rows($result);

echo "Number of matches: ". $num_result ."<br />";

if(!$result)
{
echo "Sorry, there were no matches for your query. Please try again.";
}
else
{
echo "<TABLE BORDER=1><TR><TH>Bride</TH><TH>Groom</TH><TH>Event Date</TH><TH>&nbsp;</TH></TR>";

while($row = mysql_fetch_array($result))
{
extract($row);
echo "<TR><TD>$brideFname $brideLname</TD><TD>$groomFname $groomLname </TD><TD>$event_month / $event_day / $event_year</TD><TD>$uID</TD></TR><br />";
}

echo "</TABLE>";
}
}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.