doumingchen3628 2017-08-22 10:09
浏览 60
已采纳

使用复选框,php从数据库中搜索和检索多个值

I have 3 column in my database as bodybuild, ethnictype and skincolor in table accounts. I need to search all the three columns at a time to fetch values from it using check boxesthese is my view.

<h4 style="margin-left: 10px; color: gray;">Body Build Types</h4><ol><input type="checkbox" name="bodybuild[]" value="1" />Six Pack<br><input type="checkbox" name="bodybuild[]" value="2" />Fat<br><input type="checkbox" name="bodybuild[]" value="3" />Thin<br></ol><h4  style="margin-left: 10px; color: gray;">Ethnic Types</h4><ol><input type="checkbox" name="ethnictype[]" value="4" />Arab<br><input type="checkbox" name="ethnictype[]" value="5" />Indian<br></ol><h4  style="margin-left: 10px; color: gray;">Skin Color Types</h4><ol><input type="checkbox" name="skincolor[]" value="6" />Black<br><input type="checkbox" name="skincolor[]" value="7" />White<br></ol><input style="margin-left: 10px" type="submit" value='Search' /><br><br><input type="hidden" name="tab1" value="search"></form>

Column bodybuild contain 3 values as 1, 2 and 3

Column ethnictype contain 2 values as 4 and 5

Colum skincolor contain 2 values as 6 and 7

I want to pass the values selcted in check boxes from view page into url like this

/search/1&2&3&4&5&6&7 - when selecting all check boxes and

/search/1&4&6 - when selecting three values (1,4,6) or any other ways.

I can fetch single values from table (as /search/1) but not multiple values.

Please help. Thank you!!!

  • 写回答

1条回答 默认 最新

  • dongmo3413 2017-08-25 09:44
    关注

    At last with the help of AJAX I got the solutions to my question. Thanks to all of them who had supported me. Iam posting the code for further candidates.

    index.php

    <body>
    <h1>Demo</h1>
    <table id="employees"><thead>
    <tr><th>Name</th>
    <th>BodyBuild</th>
    <th>EthnicType</th>
    <th>Skincolor</th></tr>
    </thead><tbody></tbody>
    </table>
    <div id="filter">
    <h2>Filter options</h2>
    <div>
    <h4>Body Build</h4>
    <input type="checkbox" id="car" name="sixpack">
    <label for="car">Six Pack</label>
    </div>
    <div>
    <input type="checkbox" id="car" name="fat">
    <label for="car">Fat</label>
    </div>
    <div>
    <input type="checkbox" id="car" name="thin">
    <label for="car">Thin</label>
    </div>
    <div>
    <h4>Ethnic Type</h4>
    <input type="checkbox" id="language" name="arab">
    <label for="language">Arab</label>
    </div>
    <div>
    <input type="checkbox" id="language" name="indian">
    <label for="language">Indian</label>
    </div>
    <div>
    <h4>Skin Color</h4>
    <input type="checkbox" id="nights" name="black">
    <label for="nights">Black</label>
    </div>
    <div>
    <input type="checkbox" id="nights" name="white">
    <label for="nights">White</label>
    </div>
    </div>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    function makeTable(data){
    var tbl_body = "";
    $.each(data, function() {
    var tbl_row = "";
    $.each(this, function(k , v) {
    tbl_row += "<td>"+v+"</td>";
    })
    tbl_body += "<tr>"+tbl_row+"</tr>";
    })
    return tbl_body;
    }
    function getEmployeeFilterOptions(){
    var opts = [];
    $checkboxes.each(function(){
    if(this.checked){
    opts.push(this.name);
    }
    });
    return opts;
    }
    function updateEmployees(opts){
    $.ajax({
    type: "POST",
    url: "search",
    dataType : 'json',
    cache: false,
    data: {filterOpts: opts},
    success: function(records){
    $('#employees tbody').html(makeTable(records));
    }
    });
    }
    var $checkboxes = $("input:checkbox");
    $checkboxes.on("change", function(){
    var opts = getEmployeeFilterOptions();
    updateEmployees(opts);
    });
    updateEmployees();
    </script>
    </body>
    

    search.php

    <?php
    $pdo = new PDO('mysql:host=localhost;dbname=castingkall', 'root', '');
    $select = 'SELECT name,bodybuild,ethnictype,skincolor';
    $from = ' FROM accounts';
    $where = ' WHERE TRUE';
    $opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
    if (in_array("sixpack", $opts)){
    $where .= " AND bodybuild = 1";
    }
    if (in_array("fat", $opts)){
    $where .= " AND bodybuild = 2";
    }
    if (in_array("thin", $opts)){
    $where .= " AND bodybuild = 3";
    }
    if (in_array("arab", $opts)){
    $where .= " AND ethnictype = 4";
    }
    if (in_array("indian", $opts)){
    $where .= " AND ethnictype = 5";
    }
    if (in_array("black", $opts)){
    $where .= " AND skincolor = 6";
    }
    if (in_array("white", $opts)){
    $where .= " AND skincolor = 7";
    }
    $sql = $select . $from . $where;
    $statement = $pdo->prepare($sql);
    $statement->execute();
    $results = $statement->fetchAll(PDO::FETCH_ASSOC);
    $json = json_encode($results);
    echo($json);
    ?>
    

    Just change the database name, table name, column name and values according to your data.

    Hope you find this useful

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题