Connecting Tech Pros Worldwide Help | Site Map

Script for multiple selection PHP/MySQL

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 11th, 2006, 06:15 PM
EOZyo
Guest
 
Posts: n/a
Default Script for multiple selection PHP/MySQL

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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.