0

I need help on building a mysql query.

I have 2 tables : list and type.
List : id_list, name
Type : id_type, id_list, name

The id_list column in table Type is a reference for id_list in the table List.

My HTML [main.php] :

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
                function passList() {
                        var p=[];
                        $('input.first').each( function() {
                                if($(this).prop('checked')) {                                      
                                        p.push($(this).attr('rel'));
                                }
                        } );
                        var q=[];
                        $('input.second').each( function() {
                                if($(this).prop('checked')) {
                                        q.push($(this).attr('rel'));
                                }
                        } );
                        $.ajax( {
                                url:'process.php',
                                type:'POST',
                                data: {list:p,type:q},
                                success: function(res) {
                                $("#result").html(res);
                                }
                        });
                }

        </script>
</head>
<body>
        <input type="checkbox" class="first" rel="list1" onclick="passList()">list1<br />
        <input type="checkbox" class="first" rel="list2" onclick="passList()">list2<br />
        <input type="checkbox" class="first" rel="list3" onclick="passList()">list3<br />
        <input type="checkbox" class="first" rel="list4" onclick="passList()">list4<br />
       <br><br><br>
        <input type="checkbox" class="second" rel="type1" onclick="passList()">type1<br />
        <input type="checkbox" class="second" rel="type2" onclick="passList()">type2<br />
        <input type="checkbox" class="second" rel="type3" onclick="passList()">type3<br />
        <br><br><br> 
        <div id="result"> </div>        
</body>

I am passing the variables p and q as arrays p[] and q[] to process.php

process.php :

<?php
    if(isset($_POST['list'])&&!isset($_POST['type'])) {
      foreach ($_POST['list'] as $list) {
        echo "You selected $list<br />";
      }
    }

    if(isset($_POST['list'])&&isset($_POST['type'])) {
      foreach ($_POST['list'] as $list) {
        echo "You selected $list<br />";
      }
      foreach ($_POST['type'] as $type) {
        echo "The type is $type<br />";    //   SELECT * from list inner join type on list.id_list = type.id_type WHERE id_type = [array]
      }
    }
?>

Please help me understand how to create the MySQL query to include the array in WHERE clause.

Thank you

2
Contributors
1
Reply
16
Views
3 Years
Discussion Span
Last Post by LastMitch
0

Please help me understand how to create the MySQL query to include the array in WHERE clause.

I am a bit confused what you are trying to do?

You are using AJAX with PHP. It's always good to stick with one method.

What is your table structure? You didn't really provide one.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.