[WEB4LIB] PHP help: array data into select statement

Mark Pecaut pecautm at missouri.edu
Mon Nov 6 12:25:43 EST 2000


What about this:
do_options.php:

<html>
<body>
<?
$sql = "select * from table where options = ";
$or="";                                    
while (list ($key, $val) = each ($options) )
{
  $sql = "$sql ${or}'${val}'";
  $or=" or ";
};
echo $sql;     
?>
</body>
</html>

-Mark

On Mon, Nov 06, 2000 at 06:24:20AM -0800, Bob Long wrote:
> I don't know if there are any PHP wizards on the list, but I'll throw this
> out there on the chance there are.
> 
> I would like to be able to collect data from a list of checkboxes and use
> that as the basis for a search. Something like this:
> 
> <form method="post" action="do_options.php">
> <input type="checkbox" name="options[]" value="option1">Option 1
> <input type="checkbox" name="options[]" value="option2">Option 2
> <input type="checkbox" name="options[]" value="option3">Option 3
> 
> <input type="submit" value="Submit">
> </form>
> 
> The []s cause PHP to put the values from any checked box into an array
> named $options.
> 
> My question then is how to configure the do_options.php script to put that
> array data into a select statement that would ultimately translate into
> something like:
> 
> "select * from table where options = 'option1' or 'option2' or 'option3'";
> 
> The two problems as I see it are, a) as stated above, how to get the data
> from the array into a form usable in the select statement and b) how to
> loop based on the number of boxes checked so that 'where options ='
> contains one, two, or three values.
> 
> Any suggestions?
> 
> Even if you're doing this is some other language (asp, coldfusion, perl)
> I'd be interested in seeing how it's done.
> 
> Bob Long, Eastern Shore Regional Library, Inc.
> Automation Technical Assistant
> 410 479 0776	(v)
> 410 548 5807	(f)
> bob at esrl.lib.md.us


More information about the Web4lib mailing list