Re: Create Search engine with multiple form fields

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Nasreen,

Well, I think the better solution should be a dynamic query depending on
what you have entered in the form. The switch case is not valid in this
particular case because it will only select one of the fields, so the best
solution should be like this:

$query = "select * FROM gig g, venue v, genre ge"; //$query is the
"incremental" string in which we are building the final query.

//check if there will be a "where section"

if ($number_of_fields_filled_in > 0)
{
   $query = $query . " WHERE ";

if (isset($gig_name) && $gig_name != "" && $number_of_fields_filled_in > 0 )
//evaluate whether the variable is set and it is not blank
{
   $query = $query . "g.gigName LIKE '%".$gig_name."%'";
   $number_of_fields_filled_in--;
   if ($number_of_fields_filled_in > 0)
   {
      $query = $query . " AND ";
   }
}
if (isset($sdate) && isvaliddate($sdate) && $number_of_fields_filled_in > 0
) //check if $sdate is a valid date...
{
   $query = $query . "g.gig_date LIKE '%".$sdate."%'";
   $number_of_fields_filled_in--;
   if ($number_of_fields_filled_in > 0)
   {
      $query = $query . " AND ";
   }
}

... Etc.

}

So you make an "if" statement for every input field checking whether the
variable of that field is set (with php function isset( )) and if it follows
the rule you want to - the name is not blank (first case); the date is valid
according to a function isvaliddate ( )... If everything is right, you
concatenate the query you had with the specific "WHERE"-part for that input
field.

Note that we have a $number_of_fields_filled_in to check how many fields
have been filled in. You can make a function so as to check how many fields
are checked. Also note that I've put "if ($number_of_fields_filled_in > 0)"
and it takes inside EVERY check on the field variables. So if there is not a
single one, you don't make a wrong query. You check how many "WHERE" clauses
will be, and in every "if" statement you enter, you check if the number of
fields entered is > 0 and once the query is completed for that variable, you
check if there will be more statements (so as to add the "AND").

Finally, the only thing you have to do is send the query $query for
processing in the database.

I think this is one of the possible ways to do it. Check if you understand
the code above and try implementing.

Of course there should be more ways to do it, but it is one of the
simpliest.

Regards,

Roberto


On Sun, Jun 29, 2008 at 17:54, Nasreen Laghari <nasreen_laghari@xxxxxxxxx>
wrote:

> Hi Roberto,
>
> You have got my point what I want to do. Do you think I should check and
> see which field's data has been entered and then make a dynamic query
> according to.
>
> Shall I use Switch- Case?? I have 6 fields so which way you think will be
> better to create dynamic query?
>
> Thank you
>
> Nasreen
> ----- Original Message ----
> From: Roberto Costumero Moreno <rcostu@xxxxxxxxx>
> To: Nasreen Laghari <nasreen_laghari@xxxxxxxxx>
> Sent: Sunday, June 29, 2008 4:36:34 PM
> Subject: Re:  Create Search engine with multiple form fields
>
> Hi,
>
> Let me see if I have understood what you want your application to do.
>
> You have a web form with several input fields, and ONE or SEVERAL can be
> filled in so as to search data, and I suppose you want your script to search
> data matching all filled in the form.
>
> With the actual Query, you are searching every content in the DB which has
> any coincidence with the data filled in the form. No matter which field is
> matching, it shows the complete data.
>
> If you change OR with AND, you are telling the DB to search if there is any
> data on it that matches EVERY field that you've sent. What happens if you
> don't fill any of the fields?
>
> The DB answers that it has no record like that, because comparison between
> the fields in DB and the input field is FALSE.
>
> All you have to do is to retrieve a correct "AND"-query with the parameters
> you've filled in.
>
> As an example imagine you fill only Date and Genre, so your query should
> look like this:
>
> select * FROM gig g, genre ge  WHERE g.gig_date LIKE '%".$sdate."%' AND
> ge.name LIKE '%".$genre."%'"
>
> So the script will search all data that has the Date "$sdate" and the Genre
> "$genre".
>
> Tell me if you've understood or if you need more help.
>
> Regads,
>
> Roberto
>
>
>
> On Sun, Jun 29, 2008 at 17:07, Nasreen Laghari <nasreen_laghari@xxxxxxxxx>
> wrote:
>
>> Hi,
>>
>> I have an HTML form which has more then one fields. User can precise its
>> search using one or more input fields E.g GigName,Date, Genre,Venue.
>> The data is coming from 3 tables.  Below Query brings one tables record
>> with each and every row of other two tables. Could any one please help me
>> sort out the query.
>>
>> select * FROM gig g, venue v, genre ge  WHERE g.gigName LIKE
>> '%".$gig_name."%' OR g.gig_date LIKE '%".$sdate."%' OR ge.name LIKE
>> '%".$genre."%' OR g.ticket_price LIKE '%".$ticket_price1."%' OR
>> g.ticket_price LIKE '%".$ticket_price2."%' OR v.venueName LIKE
>> '%".$vname."%'"
>>
>> If I replace OR with AND I dont get any result.
>>
>>
>> Regards
>>
>> Nasreen
>>
>>
>>
>
>
>
>

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux