-1

So, i used my google friend but i couldn't find a solution anywhere. The situation is the following, given 3 multiple selects, from which you can select several options separately and list only those that are in it. I means that in SQL query:

select * from people where name like '%2%' and age like '%1%' and work like '%2%'

Here is the code:

<form action="" method="post">
    <select class="selectpicker" id="name" name="name[]" multiple data-live-search="true">
  <option value="1">1</option>
    <option value="2">2</option>
</select>
<select class="selectpicker" id="age" name="age[]" multiple data-live-search="true">
  <option value="1">1</option>
    <option value="2">2</option>
</select>
<select class="selectpicker" id="work" name="work[]" multiple data-live-search="true">
  <option value="1">1</option>
    <option value="2">2</option>
</select>
</form>
Stylee
  • 11
  • 2
  • how are these values sent from HTML page to PHP script? Is that with regular form submit or javascript? – Professor Abronsius Sep 14 '21 at 09:55
  • What's your question? Where are you stuck? – Shadow Sep 14 '21 at 10:27
  • You forgot to ask a question or describe a specific problem. And the code provided is so minimal that it gives us no indication of what your actual issue is. It's also unclear what the actual requirements are for how the code should behave when you run the query. e.g. must the user choose something from every select? Or can they leave some of the options unselected? What should the query do in those cases? etc. Please describe your problem clearly and in more detail. See [ask] and how to create a [mre] if you need more guidance. Thanks. – ADyson Sep 14 '21 at 10:40
  • My question is that if all selected use it, put a filter that includes the search criteria as I wrote in sql to filter out conditions, I just need to conjure it into php – Stylee Sep 14 '21 at 10:41
  • @ADyson u have right, I hurried. – Stylee Sep 14 '21 at 10:48
  • @ProfessorAbronsius regular form. – Stylee Sep 14 '21 at 10:50
  • "I just need" isn't a question though. What have you tried? Where are you stuck with that, exactly? What specifically is puzzling you about the process? The answer below _probably_ has what you need, as that person has very kindly provided you a ready-made solution (probably because this kind of thing is not a new problem, there are no doubt examples you could study online of the same kind of concept), but you may not be so lucky with future questions - it's better to ask something specific with a clear problem description. – ADyson Sep 14 '21 at 10:51

2 Answers2

0

If the values in the select menus are as disclosed above - ie:integers then I think you could try something like the following assuming I interpreted the question/request correctly.

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        /*
            filter the POST values - ensur ethey are integers
        */
        $_POST['name']=filter_input( INPUT_POST, 'name', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY );
        $_POST['age']=filter_input( INPUT_POST, 'age', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY );
        $_POST['work']=filter_input( INPUT_POST, 'work', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY );
        
        # base sql query
        $sql='select * from `people`';
        
        # arrays to store `like` clauses for each selected option
        $a=$b=$c=array();
        
        /*
            process all selected options and add to respective array
        */
        if( !empty( $_POST['name'] ) ){
            foreach( $_POST['name'] as $value ){
                if( !empty( $value ) ) $a[]='`name` like "%'.$value.'%"';
            }
        }
        if( !empty( $_POST['age'] ) ){
            foreach( $_POST['age'] as $value ){
                if( !empty( $value ) ) $b[]='`age` like "%'.$value.'%"';
            }
        }
        if( !empty( $_POST['work'] ) ){
            foreach( $_POST['work'] as $value ){
                if( !empty( $value ) ) $c[]='`work` like "%'.$value.'%"';
            }
        }
        /*
            construct final sql
        */
        if( !empty( $a ) or !empty( $b ) or !empty( $c ) ) $sql.=' where ';
        if( !empty( $a ) )$sql.= sprintf('( %s )', implode( ' OR ', $a ) );
        if( !empty( $b ) )$sql.= ' AND ' . sprintf('( %s )', implode( ' OR ', $b ) );
        if( !empty( $c ) )$sql.= ' AND '. sprintf('( %s )', implode( ' OR ', $c ) );
        
        #what you do with it now is up to you... 
        exit( $sql );
    }
    
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
        <style>
            select{width:100px}
        </style>
    </head>
    <body>
    
        <form method='post'>
            <select class='selectpicker' name='name[]' multiple data-live-search='true'>
                <option value=1>1
                <option value=2>2
            </select>
            <select class='selectpicker' name='age[]' multiple data-live-search='true'>
                <option value=1>1
                <option value=2>2
            </select>
            <select class='selectpicker' name='work[]' multiple data-live-search='true'>
                <option value=1>1
                <option value=2>2
            </select>
            
            <input type='submit' />
        </form>
        
    </body>
</html>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • I’m glad you helped, but unfortunately it’s not just numbers in it. :( – Stylee Sep 14 '21 at 10:50
  • 1
    @Stylee if it's not just numbers, then you'd need to parameterise your query instead of just using the filter_input – ADyson Sep 14 '21 at 10:51
-1

I think you wanted list of Records based on Multiple Dropdown values. Could you try with Sql query ?

$names = implode(",", $_POST['name']);
$ages = implode(",", $_POST['age']); 
$works = implode(",", $_POST['work']); 

 $sql= "SELECT * FROM `people` WHERE name IN ($names) AND age IN ($ages) AND work IN ($works)";

Thanks

Thanjeys
  • 24
  • 4
  • Hi Thanjeys. Thanks for trying to answer the question. If you want to give someone some advice, just comment on the post and do not use `answer`. This helps others to find answers and the user to test something new, which you think might help to find the answer. – Dinkheller Sep 14 '21 at 11:13
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 14 '21 at 11:51