For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > May 2005 > form search









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author form search
overflo@telstra.com

2005-05-27, 3:58 am

I'm new to PHP & MySQL. I want to be able to process a form with 4
input fields according to how many fields are filled out. If only one
is filled, it searches on that, if two it searches accordingly etc. I
have all the select statements for each instance working I just don't
know the best way to call them. Would if be an if else statement (
perhaps if(!isset???) that would call each script or is there a better
way. Any help would be appreciated

news.tpg.com.au

2005-05-28, 3:57 am

Set a value depending on whether fields are filled out.

Return the values of the fields to your PHP script using $_POST and set
these to variables such as $a, $b, $c and $d.
Use strlen() on these variables to determine if they are non empty.

Set a variable called $sum to use the values 1, 2, 4 and 8 respectively for
each field filled in.

i.e
$sum = 0;
if (strlen($a) > 0), $sum = $sum+1;
if (strlen($b) > 0), $sum = $sum+2;

You can sum these together to get the 16 possible combinations from 0 to 15
(i.e. 2x2x2x2 = 16).
You can then use a switch statement based on the sum such as

switch($sum)
case 0: // some action if no fields were completed
case 1:
case 2:
....
case 15: // some action if all fields were completed
default:

You can also choose to omit or cancel out of certain cases by using the
break statement.
This method is good if you want to expand on your forms later on.
A new strlen() test and a quick copy and paste will duplicate the existing
cases for a new field

Just change the numbers for the cases and alter the commands to take the new
field into account and you're done.
Lots of flexibility in terms of performing different tasks however bear in
mind that because it doubles with each additional field, this could take a
long time to process with more than 10 fields.

2 ^ 4 = 16
2 ^ 10 = 1024 - (2 ^ 6 or 64 times longer)

Oh, and of course your original variables of $a, $b, $c, $d etc can still be
used for any data manipulation you like.

Hope that helped...
Now for my question :p

<overflo@telstra.com> wrote in message
news:1117172890.336511.224560@o13g2000cwo.googlegroups.com...
> I'm new to PHP & MySQL. I want to be able to process a form with 4
> input fields according to how many fields are filled out. If only one
> is filled, it searches on that, if two it searches accordingly etc. I
> have all the select statements for each instance working I just don't
> know the best way to call them. Would if be an if else statement (
> perhaps if(!isset???) that would call each script or is there a better
> way. Any help would be appreciated
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com