JQuery Mobile Mysql数据库过滤系统

时间:2017-01-19 01:05:52

标签: javascript php jquery mysql jquery-mobile

我希望为我的数据库记录建立一个过滤系统。 我可以加载记录,但我希望使用JQuery Mobile下拉栏过滤多个关键字的记录。但是我不知道如何把这一切放在一起。

以下是JQuery Mobile下拉选项的代码:              

        <label for="select-h-6a">Genre</label>
        <select name="select-h-6a" id="select-h-6a">
            <option value="#">Rock</option>
            <option value="#">Pop</option>
            <option value="#">Dance</option>
        </select>
        <label for="select-h-6b">Role</label>
        <select name="select-h-6b" id="select-h-6b">
            <option value="#">Singer</option>
            <option value="#">Drummer</option>
            <option value="#">Guitar</option>
        </select>
        <label for="select-h-6c">Members</label>
        <select name="select-h-6c" id="select-h-6c">
            <option value="#">1</option>
            <option value="#">2</option>
            <option value="#">3</option>
        </select>
    </fieldset>
</form>

这是我目前的代码,其中包含帖子标题和帖子内容,我可以添加更多内容,但目前只有那些选项:

 $posts = getAllPosts();

<div id="searchpost">
      <div class="ui-bar ui-bar-a">
       <div id="postTitle">

        <?php echo $posts[$p]["post_title"];?></div>
      </div>
      <div class="ui-body ui-body-a">
         <div id="postContent">
        <?php echo $posts[$p]["post_content"];}?></div>
      </div>
      </div>

和功能:

function getAllPosts() {
    require "config.php";
    $posts = $c->query ( "SELECT * FROM posts" );

    if ( $posts->num_rows > 0 ) {

        while( $row = $posts->fetch_assoc() )  {

                $postData[] = array( "user_id" => $row[ "user_id" ], "post_title" => $row[ "post_title" ], "post_content" => $row[ "post_content" ] );
            }
    } else {
        return "No Data";
    }
    return $postData;
}

然而,正如您所看到的,它专门用于调用数据库中的内容。如何显示我在下拉菜单中选择的内容而不是详细信息。

这可以添加到多个选定的选项中,例如选择摇滚,歌手和2个成员,只显示所有这三个中的记录。我假设如果我不想要选项2中的特定内容我可以拥有所有选项它会搜索该类别中的所有内容。

所以,我需要帮助: - 编写代码,搜索我的数据库记录并显示所有这些代码,以解决在JQuery Mobile下拉栏中选择的所有选项。

我希望你明白我需要帮助的是什么。

修改

我的数据库: Image

连接:(config.php)

<?php

    $c = mysqli_connect( "localhost", "user", "pass", "database");

    // Check connection
    if (mysqli_connect_errno())
    {

        echo "Failed to connect to MySQL: " . mysqli_connect_error();

    }

?>

1 个答案:

答案 0 :(得分:0)

我假设这个问题是关于如何基于提交的表单数据构建S​​QL查询。如果您使用PHP PDO扩展,它可能更安全(并且通常更容易)。 php.net将帮助你。

您的查询在转移到PDO后(强烈推荐)看起来大致如此。

$sql = ‘select * from posts 
    where genre=:genre
    and role=:role
    and members=:members’;

$sth = $dbh->prepare($sql);
$sth->execute(
    array(‘:genre’ => $_GET[‘genre’],
          ‘:role’ => $_GET[‘role’],
          ‘:members’=> $_GET[‘members’])
    );
$result = $sth->fetchAll(); // this should be enough to echo a result