【问题标题】:What is the best logic to create filtering system in CMS, and then to add option to choose how many filtered results will be shown?在 CMS 中创建过滤系统的最佳逻辑是什么,然后添加选项来选择显示多少过滤结果?
【发布时间】:2019-06-05 07:17:20
【问题描述】:

我正在为博客构建 CMS,并添加了过滤管理仪表板上帖子的选项。选项是已发布的帖子、草稿帖子、评论的帖子和查看次数最多的帖子。 这是代码

<?php
function get_options($select){

$filters = array('All posts' => 1, 'Published Posts' => 2, 'Drafted Posts' => 3, 'Commented Posts' => 4, 'Most Viewed Posts' => 5);

$options = '';

foreach($filters as $k => $v) {
    if($select == $v){
        $options.='<option value="'.$v.'" selected>'.$k.'</option>';
    } else {

        $options.='<option value="'.$v.'">'.$k.'</option>';
    }
}

return $options;

}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="filter">

    <div id="bulkOptionsContainer" class="col-xs-3">

        <select class="form-control" name="filter_posts" onchange="this.form.submit();">
            <option value="">Filter Posts</option>
            <?php echo get_options($selected); ?>
        </select><br>

    </div>

</form>
<?php
if(isset($_POST['filter_posts'])) {

            $filter_posts = $_POST['filter_posts'];

            switch ($filter_posts) {

                case '1';

                    include_once("all_posts.php"); //Here is query for all posts from database

                    break;

                case '2';

                   include_once("published_posts.php"); //Here is query for published posts from database

                    break;


                case '3';

                    include_once("draft_posts.php"); //Here is code for drafted posts from database

                    break;

                case '4';

                    include_once("commented_posts.php");//Here is query for commented posts from database

                    break;

                case '5';

                   include_once("most_viewed_posts.php");//Here is query for most viewed posts from database


                    break;
            }
        }
?>

这对我有用,我得到了所有想要的结果,但我想添加一个功能来选择在页面上显示多少结果。例如显示 20 个帖子、显示 40 个帖子、显示所有帖子。但它需要显示,例如,如果我将帖子过滤到已发布的帖子,并选择显示 20 个帖子,它需要为已发布的帖子显示 20 个帖子等。如何做到这一点? 这是用于选择选项的 HTML。

<form action="" method="post">
    <div id="bulkOptionsContainer" class="col-xs-2">
        <select name="select_num_posts" class="form-control">

            <option value="20" selected>Show 20</option>
            <option value="40">Show 40</option>
            <option value="all">Show All</option>

        </select>
    </div>

    <div class="col-xs-4">
        <input type="submit" name="submit" class="btn btn-success" value="Apply">
    </div>
</form>

【问题讨论】:

    标签: javascript php jquery html mysql


    【解决方案1】:

    您可以创建更多的 PHP 文件,称为 draft_posts_20.phpdraft_posts_40.phpdraft_posts_all.php 等,但这会创建很多很多文件。显然这种方法是有缺陷的。

    我假设这些文件中的每一个,all_posts.phpdraft_posts.php 等都包含非常相似的代码。有一个数据库查询和一些输出代码。它们基本上是彼此的副本,略有不同。

    我认为您应该尝试将它们全部放入一个 PHP 文件中,然后再合并您的新过滤器。所有这些的输出部分都应该相同,只是您需要根据您的两个过滤器更改数据库查询。

    【讨论】:

    • 是的,实际上我在一个页面上将所有内容放在一起,而在另一页上则喜欢这样。我在想哪种方式更好。感谢您清理它。但我的问题是如何合并新过滤器,并且该过滤器需要与以前的过滤器相关。基本上,当我过滤帖子以仅显示已发布时,如何将选择选项显示 20 或仅显示 40 与已发布的帖子相关联。我需要使用什么逻辑?我猜我需要使用一些 javascript 代码。
    • 我认为你应该尝试在 PHP 中解决这个问题。但是,您的问题中缺少该代码,因此我无法提供任何详细建议。这个想法是:您有两个过滤器来形成查询以检索数据库的信息。
    【解决方案2】:

    这是来自单页版本的代码。这样我就可以过滤帖子,但我不知道如何过滤过滤后的帖子,以显示 20、40 等。

    <?php
        function get_options($select){
    
            $filters = array('All posts' => 1, 'Published Posts' => 2, 'Drafted Posts' => 3, 'Commented Posts' => 4, 'Most Viewed Posts' => 5);
    
            $options = '';
    
            foreach($filters as $k => $v) {
                if($select == $v){
                    $options.='<option value="'.$v.'" selected>'.$k.'</option>';
                } else {
    
                    $options.='<option value="'.$v.'">'.$k.'</option>';
                }
            }
    
            return $options;
    
        }
    
        function get_num_options($select){
    
            $filters = array('Show 20' => 20, 'Show 40' => 40, 'Show All' => 'all');
    
            $options = '';
    
            foreach($filters as $k => $v) {
                if($select == $v){
                    $options.='<option value="'.$v.'" selected>'.$k.'</option>';
                } else {
    
                    $options.='<option value="'.$v.'">'.$k.'</option>';
                }
            }
    
            return $options;
    
        }
    
    ?>    
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="filter">
    
            <div id="bulkOptionsContainer" class="col-xs-3">
    
                <select class="form-control" name="filter_posts" onchange="this.form.submit();">
                    <option value="">Filter Posts</option>
                    <?php echo get_options($selected); ?>
                </select><br>
    
            </div>
    
        </form>
    
    <?php    
          if (!isset($_POST['filter_posts'])) {
    
    
                        $stmt1 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id ORDER BY post_id DESC");
    
                        //Bind
                        //mysqli_stmt_bind_param();
                        //Execute
                        mysqli_stmt_execute($stmt1);
                        mysqli_stmt_store_result($stmt1);
                        //Result
                        mysqli_stmt_bind_result($stmt1, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                        confirmQuery($stmt1);
    
                        $stmt2 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.user_id = " . loggedInUserId() . " ORDER BY post_id DESC");
    
                        //Bind
                        //mysqli_stmt_bind_param();
                        //Execute
                        mysqli_stmt_execute($stmt2);
                        mysqli_stmt_store_result($stmt2);
                        //Result
                        mysqli_stmt_bind_result($stmt2, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                        confirmQuery($stmt2);
    
                        if (is_admin()) {
    
                            $stmt = $stmt1;
                        } else {
    
                            $stmt = $stmt2;
                        }
                    } else {
    
                        $filter_posts = $_POST['filter_posts'];
    
                        switch ($filter_posts) {
    
                            case '1';
    
                                $stmt1 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id ORDER BY post_id DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                //Execute
                                mysqli_stmt_execute($stmt1);
                                mysqli_stmt_store_result($stmt1);
                                //Result
                                mysqli_stmt_bind_result($stmt1, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt1);
    
                                $stmt2 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.user_id = " . loggedInUserId() . " ORDER BY post_id DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                //Execute
                                mysqli_stmt_execute($stmt2);
                                mysqli_stmt_store_result($stmt2);
                                //Result
                                mysqli_stmt_bind_result($stmt2, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt2);
    
                                if (is_admin()) {
    
                                    $stmt = $stmt1;
                                } else {
    
                                    $stmt = $stmt2;
                                }
    
                                break;
    
                            case '2';
    
                                $stmt1 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.comment_status = ? ORDER BY post_id DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                mysqli_stmt_bind_param($stmt1, "s", $comment_status);
    
                                $comment_status = 'published';
                                //Execute
                                mysqli_stmt_execute($stmt1);
                                mysqli_stmt_store_result($stmt1);
                                //Result
                                mysqli_stmt_bind_result($stmt1, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt1);
    
                                $stmt2 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.comment_status = ? AND posts.user_id = " . loggedInUserId() . " ORDER BY post_id DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                mysqli_stmt_bind_param($stmt2, "s", $comment_status);
    
                                $comment_status = 'published';
                                //Execute
                                mysqli_stmt_execute($stmt2);
                                mysqli_stmt_store_result($stmt2);
                                //Result
                                mysqli_stmt_bind_result($stmt2, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt2);
    
                                if (is_admin()) {
    
                                    $stmt = $stmt1;
                                } else {
    
                                    $stmt = $stmt2;
                                }
    
                                break;
    
    
                            case '3';
    
                                $stmt1 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.comment_status = ? ORDER BY post_id DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                mysqli_stmt_bind_param($stmt1, "s", $comment_status);
    
                                $comment_status = 'draft';
                                //Execute
                                mysqli_stmt_execute($stmt1);
                                mysqli_stmt_store_result($stmt1);
                                //Result
                                mysqli_stmt_bind_result($stmt1, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt1);
    
                                $stmt2 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.comment_status = ? AND posts.user_id = " . loggedInUserId() . " ORDER BY post_id DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                mysqli_stmt_bind_param($stmt2, "s", $comment_status);
    
                                $comment_status = 'draft';
                                //Execute
                                mysqli_stmt_execute($stmt2);
                                mysqli_stmt_store_result($stmt2);
                                //Result
                                mysqli_stmt_bind_result($stmt2, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt2);
    
                                if (is_admin()) {
    
                                    $stmt = $stmt1;
                                } else {
    
                                    $stmt = $stmt2;
                                }
    
                                break;
    
                            case '4';
    
                                $stmt1 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.post_comment_count > 0 ORDER BY post_id DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
    
                                //Execute
                                mysqli_stmt_execute($stmt1);
                                mysqli_stmt_store_result($stmt1);
                                //Result
                                mysqli_stmt_bind_result($stmt1, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt1);
    
                                $stmt2 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.post_comment_count > 0 AND posts.user_id = " . loggedInUserId() . " ORDER BY post_id DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                //Execute
                                mysqli_stmt_execute($stmt2);
                                mysqli_stmt_store_result($stmt2);
                                //Result
                                mysqli_stmt_bind_result($stmt2, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt2);
    
                                if (is_admin()) {
    
                                    $stmt = $stmt1;
                                } else {
    
                                    $stmt = $stmt2;
                                }
    
                                break;
    
                            case '5';
    
                                $stmt1 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id ORDER BY post_views_count DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                //Execute
                                mysqli_stmt_execute($stmt1);
                                mysqli_stmt_store_result($stmt1);
                                //Result
                                mysqli_stmt_bind_result($stmt1, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt1);
    
                                $stmt2 = mysqli_prepare($connection, "SELECT posts.post_id, posts.post_author, posts.post_user, posts.post_title, posts.post_category_id, posts.comment_status, posts.post_image, posts.post_tags, posts.post_comment_count, posts.post_date, posts.post_views_count, categories.cat_id, categories.cat_title FROM posts LEFT JOIN categories ON posts.post_category_id = categories.cat_id WHERE posts.user_id = " . loggedInUserId() . " ORDER BY post_views_count DESC");
    
                                //Bind
                                //mysqli_stmt_bind_param();
                                //Execute
                                mysqli_stmt_execute($stmt2);
                                mysqli_stmt_store_result($stmt2);
                                //Result
                                mysqli_stmt_bind_result($stmt2, $post_id, $post_author, $post_user, $post_title, $post_category_id, $comment_status, $post_image, $post_tags, $post_comment_count, $post_date, $post_views_count, $cat_id, $cat_title);
    
                                confirmQuery($stmt2);
    
                                if (is_admin()) {
    
                                    $stmt = $stmt1;
                                } else {
    
                                    $stmt = $stmt2;
                                }
    
    
    
                                break;
                        }
                    }
    
                    while (mysqli_stmt_fetch($stmt)) {
    
                       //Echo result
                    }
    
    
    
                    ?>
    
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="filter">
    
            <div id="bulkOptionsContainer" class="col-xs-3">
    
                <select class="form-control" name="select_num_posts" onchange="this.form.submit();">
                    <option value="">Show Posts</option>
                    <?php echo get_num_options($selected); ?>
                </select><br>
    
            </div>
    
        </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2019-10-26
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      相关资源
      最近更新 更多