Script for multiple selection PHP/MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • EOZyo

    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">&n bsp;<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($qu ery) or die(mysql_error ());

    while($row = mysql_fetch_arr ay($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

Working...