Connecting Tech Pros Worldwide Help | Site Map

Script for multiple selection PHP/MySQL

  #1  
Old May 11th, 2006, 07:15 PM
EOZyo
Guest
 
Posts: n/a
Hi, I would like to share this small script with you, i was looking for
2 days all over the net and i could find anything like this, thus i
decided to create one, i hope it could help somebody :)

We will create 2 documents:

1) form.html and,
2) handler.php

===================
/* form.html */

<table>
<form action="handler.php" method="post" name="myform">
<tr>
<td><select name="name1[]" multiple>
<option value="value1">Value1</option>
<option value="value2">Value2</option>
<option value="value3">Value3</option></td>
</tr>
<tr>
<td>
<input type="checkbox" name="name2[]" value="value1">Value1<br />
<input type="checkbox" name="name2[]" value="value2">Value2<br />
<input type="checkbox" name="name2[]" value="value3">Value3<br />
</td>
</tr>
<tr>
<td colspan="2">
<input type="reset" name="Reset">&nbsp;<input type="submit"
value="Submit">
</td>
</tr>
</form>
</table>

===================
/* handler.php */

<?php
// Connect to your database

mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$name1 = $_POST['name1'];
$name2 = $_POST['name2'];

for($name1array=0; $name1array < sizeof($name1); $name1array++)
{
if($name1array < (sizeof($name1)-1)) { $name1_cond = " OR "; }
else { $name1_cond = ""; }
$name1q = $name1q."`name1` LIKE
'".$name1[$name1array]."'$name1_cond";
}

for($name2array=0; $name2array < sizeof($name2); $name2array++)
{
if($name2array < (sizeof($name2)-1)) { $name2_cond = " OR "; }
else { $name2_cond = ""; }
$name2q = $name2q."`name2` LIKE
'".$name2[$name2array]."'$name2_cond";
}

$name1q = "($name1q)";
$name2q = "($name2q)";

$query = "SELECT * FROM `table` WHERE $name1q AND $name2q LIMIT 0,30";

$sql = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($sql)) // Get your results.
{
echo "$row['myrow1']."<br>\n";
echo "$row['myrow2']."<br>\n";
}
?>

Obviously, this has to be adapted to fit your needs, for me, everything
works, i'm actually using sessions with it and no problems so far.

I hope you find this useful :)

SALUX!!!
EOZyo

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to re-print values from combobox selection Mtek answers 4 June 27th, 2008 08:10 PM
Create script to insert 200 rows into table rhaazy answers 6 August 24th, 2007 08:55 PM
Shopping Cart ak1dnar answers 4 March 4th, 2007 01:08 PM
Multiple Array Selection From Database Instead of In-Page Arrays James answers 12 July 17th, 2005 12:39 AM