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...

<!-- 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>
<!-- 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);
?>

how do i approach this task?

For example:

If you create a submit form field for the "view all" function. Then say name it "view_all".

<input type="submit" name="view_all" value="View All" />

Then when someone submits the form with the "view all" button instead of the regular "search" button, you'll get an HTTP_VAR with the index "view_all" with the value "View All".

Eg: If it was an HTTP POST then you would get

$_POST; // with the value 'View All'

So in your code you can branch the validation block:

if (!$lname && !isset($_POST['view_all']))
    {
           echo "<FONT COLOR=FF0000>You have not filled the required fields. Please try again.</FONT>";
           include "search.inc";
           exit;
    } elseif (isset($_POST['view_all']) {

// select everything (best if you add a limit and pagination)
    $sql = mysql_query("SELECT * FROM my_search_table LIMIT [pagination limit]") or die(mysql_error();

} else {

// your regular query
$sql = mysql_query("SELECT brideLname, groomLname FROM my_search_table WHERE brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'") or die(mysql_error();

}

Notice I've put the SQL statements in the validation blocks. Its the only thing that should depend on the validation.

hope that helps a bit..

This article has been dead for over six months. Start a new discussion instead.