Php 分页在排序中不起作用

Php 分页在排序中不起作用,php,mysql,wordpress,sorting,pagination,Php,Mysql,Wordpress,Sorting,Pagination,我有个问题。我对用户的工作可以输入他的信息,如,名称,图像等,然后所有的用户信息将显示在页面上的类别和排序也与分页 问题1。排序和分类单独运行良好,但在选择类别后排序不起作用。我想当用户选择一个类别,然后他也可以按所选类别排序 问题2。我的分页代码工作正常,但不正常,因为当我选择“类别”时,它没有分类工作,这与选择排序时的问题相同 谢谢 这是针对类别: <form action="" method="POST"> <select name="theme_category

我有个问题。我对用户的工作可以输入他的信息,如,名称,图像等,然后所有的用户信息将显示在页面上的类别和排序也与分页

问题1。排序和分类单独运行良好,但在选择类别后排序不起作用。我想当用户选择一个类别,然后他也可以按所选类别排序

问题2。我的分页代码工作正常,但不正常,因为当我选择“类别”时,它没有分类工作,这与选择排序时的问题相同

谢谢

这是针对类别:

<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" >Select Your Categories</option>
        <?php 

        global $wpdb;
        $table_name ="theme_category";
        $category = $wpdb->get_results( "SELECT * from $table_name" );
        foreach($category as $categories){
            ?>
            <option class="form-control" ><?php echo $categories->theme_category_name;?></option>
            <?php
        }
        ?>

    </select> 
    <input type="submit" class="btn btn-info" name="category_submit" value="GO"/>
</form>
<form action="" method="post">
    <input type="submit" class="btn btn-default" name="Newest" value="Newest ">
   <input type="submit" class="btn btn-default" name="high_price" value="High Price">
   <input type="submit" class="btn btn-default" name="low_price" value="Low Price">
</form>
<?php
        global $wpdb;
        $table_name ="theme_upload";
            //Pegination
            $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
            $limit = 2; // number of rows in page
            $offset = ( $pagenum - 1 ) * $limit;
            $total = $wpdb->get_var( "select count(*) as total from $table_name" );
            $num_of_pages = ceil( $total / $limit );
            $rowcount = $wpdb->num_rows;

        $category_s = $_POST['theme_category_name'];
        //Category Select
        if(isset($_POST['category_submit']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name WHERE theme_cat_name = '$category_s' limit  $offset, $limit");
        }
        //Newest
        elseif(isset($_POST['Newest']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY id DESC limit  $offset, $limit");
        }
        //Sorting for High Price
        elseif (isset ($_POST['high_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price DESC limit  $offset, $limit");
            }
        //Sorting for low Price
        elseif (isset ($_POST['low_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price ASC limit  $offset, $limit");
            }
        // Default Order
        else {
                $result = $wpdb->get_results( "SELECT * from $table_name limit  $offset, $limit" );
        }


        if($rowcount>0){    
            foreach($result as $results){
                //var_dump($results);

            ?>

            <?php

            }
        }
        else{
            echo "<tr><td cols=an='5'>No records found</td></tr>";
        } 
            ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //

?>
<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" value="all" >Select Your Categories</option>
            <?php 

            global $wpdb;
            $table_name ="theme_category";
            $category = $wpdb->get_results( "SELECT * from $table_name" );
            foreach($category as $categories){
                ?>
                <option class="form-control" value="<?=$categories->theme_category_name?>" ><?php echo $categories->theme_category_name;?></option>
                <?php
            }
            ?>

        </select> 
        <!-- Change these to buttons, share the name=submit -->
        <button type="submit" class="btn btn-info" name="submit" value="category_submit">GO</button>

        <!-- Move these into the same form tag and make them buttons -->
        <button type="submit" class="btn btn-default" name="submit" value="Newest">Newest</button>
        <button type="submit" class="btn btn-default" name="submit" value="high_price">High Price</button>
        <button type="submit" class="btn btn-default" name="submit" value="low_price">Low Price</button>

</form>
<?php
    global $wpdb;
    $table_name ="theme_upload";
        //Pegination
        $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
        $limit = 2; // number of rows in page
        $offset = ( $pagenum - 1 ) * $limit;
        $total = $wpdb->get_var( "select count(*) as total from $table_name" );
        $num_of_pages = ceil( $total / $limit );
        $rowcount = $wpdb->num_rows;

    $category_s = $_POST['theme_category_name'];

    // Get a list of valid categories
    $categories = $wpdb->get_results( "SELECT theme_category_name from theme_category", ARRAY_A );

    // Sort them into an array of values
    $allowed_categories = array_column($categories, 'theme_category_name');

    // Add in the default value (defined on your page)
    $allowed_categories[] = 'all';

    // Check the incoming value is valid / allowed
    if(in_array($category_s, $allowed_categories) === false) {
        // Set up your own error handling here
        echo "Invalid category";
        exit;
    }

    // define defaults
    $where = $order_by = "";

    // If a category is specified, define the WHERE clause
    if($category_s !== 'all') {
        $where = " WHERE theme_cat_name = '$category_s' ";
    }

    // Toggle your order_by values
    switch($_POST['submit']) {
        case 'Newest':
            $order_by = " ORDER BY id DESC ";
            break;

        case 'high_price':
            $order_by = " ORDER BY theme_price DESC ";
            break;

        case 'low_price':
            $order_by = " ORDER BY theme_price ASC ";
            break;
    }

    // Run only 1 SQL query to process all of the incoming information
    $result = $wpdb->get_results( "SELECT * from $table_name $where $order_by LIMIT $offset, $limit" );

    if($rowcount>0){    
        foreach($result as $results){
            //var_dump($results);

        ?>

        <?php

        }
    }
    else{
        echo "<tr><td cols=an='5'>No records found</td></tr>";
    } 
        ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //
?> 

选择您的类别
此if用于排序:

<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" >Select Your Categories</option>
        <?php 

        global $wpdb;
        $table_name ="theme_category";
        $category = $wpdb->get_results( "SELECT * from $table_name" );
        foreach($category as $categories){
            ?>
            <option class="form-control" ><?php echo $categories->theme_category_name;?></option>
            <?php
        }
        ?>

    </select> 
    <input type="submit" class="btn btn-info" name="category_submit" value="GO"/>
</form>
<form action="" method="post">
    <input type="submit" class="btn btn-default" name="Newest" value="Newest ">
   <input type="submit" class="btn btn-default" name="high_price" value="High Price">
   <input type="submit" class="btn btn-default" name="low_price" value="Low Price">
</form>
<?php
        global $wpdb;
        $table_name ="theme_upload";
            //Pegination
            $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
            $limit = 2; // number of rows in page
            $offset = ( $pagenum - 1 ) * $limit;
            $total = $wpdb->get_var( "select count(*) as total from $table_name" );
            $num_of_pages = ceil( $total / $limit );
            $rowcount = $wpdb->num_rows;

        $category_s = $_POST['theme_category_name'];
        //Category Select
        if(isset($_POST['category_submit']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name WHERE theme_cat_name = '$category_s' limit  $offset, $limit");
        }
        //Newest
        elseif(isset($_POST['Newest']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY id DESC limit  $offset, $limit");
        }
        //Sorting for High Price
        elseif (isset ($_POST['high_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price DESC limit  $offset, $limit");
            }
        //Sorting for low Price
        elseif (isset ($_POST['low_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price ASC limit  $offset, $limit");
            }
        // Default Order
        else {
                $result = $wpdb->get_results( "SELECT * from $table_name limit  $offset, $limit" );
        }


        if($rowcount>0){    
            foreach($result as $results){
                //var_dump($results);

            ?>

            <?php

            }
        }
        else{
            echo "<tr><td cols=an='5'>No records found</td></tr>";
        } 
            ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //

?>
<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" value="all" >Select Your Categories</option>
            <?php 

            global $wpdb;
            $table_name ="theme_category";
            $category = $wpdb->get_results( "SELECT * from $table_name" );
            foreach($category as $categories){
                ?>
                <option class="form-control" value="<?=$categories->theme_category_name?>" ><?php echo $categories->theme_category_name;?></option>
                <?php
            }
            ?>

        </select> 
        <!-- Change these to buttons, share the name=submit -->
        <button type="submit" class="btn btn-info" name="submit" value="category_submit">GO</button>

        <!-- Move these into the same form tag and make them buttons -->
        <button type="submit" class="btn btn-default" name="submit" value="Newest">Newest</button>
        <button type="submit" class="btn btn-default" name="submit" value="high_price">High Price</button>
        <button type="submit" class="btn btn-default" name="submit" value="low_price">Low Price</button>

</form>
<?php
    global $wpdb;
    $table_name ="theme_upload";
        //Pegination
        $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
        $limit = 2; // number of rows in page
        $offset = ( $pagenum - 1 ) * $limit;
        $total = $wpdb->get_var( "select count(*) as total from $table_name" );
        $num_of_pages = ceil( $total / $limit );
        $rowcount = $wpdb->num_rows;

    $category_s = $_POST['theme_category_name'];

    // Get a list of valid categories
    $categories = $wpdb->get_results( "SELECT theme_category_name from theme_category", ARRAY_A );

    // Sort them into an array of values
    $allowed_categories = array_column($categories, 'theme_category_name');

    // Add in the default value (defined on your page)
    $allowed_categories[] = 'all';

    // Check the incoming value is valid / allowed
    if(in_array($category_s, $allowed_categories) === false) {
        // Set up your own error handling here
        echo "Invalid category";
        exit;
    }

    // define defaults
    $where = $order_by = "";

    // If a category is specified, define the WHERE clause
    if($category_s !== 'all') {
        $where = " WHERE theme_cat_name = '$category_s' ";
    }

    // Toggle your order_by values
    switch($_POST['submit']) {
        case 'Newest':
            $order_by = " ORDER BY id DESC ";
            break;

        case 'high_price':
            $order_by = " ORDER BY theme_price DESC ";
            break;

        case 'low_price':
            $order_by = " ORDER BY theme_price ASC ";
            break;
    }

    // Run only 1 SQL query to process all of the incoming information
    $result = $wpdb->get_results( "SELECT * from $table_name $where $order_by LIMIT $offset, $limit" );

    if($rowcount>0){    
        foreach($result as $results){
            //var_dump($results);

        ?>

        <?php

        }
    }
    else{
        echo "<tr><td cols=an='5'>No records found</td></tr>";
    } 
        ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //
?> 

所有php代码:

<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" >Select Your Categories</option>
        <?php 

        global $wpdb;
        $table_name ="theme_category";
        $category = $wpdb->get_results( "SELECT * from $table_name" );
        foreach($category as $categories){
            ?>
            <option class="form-control" ><?php echo $categories->theme_category_name;?></option>
            <?php
        }
        ?>

    </select> 
    <input type="submit" class="btn btn-info" name="category_submit" value="GO"/>
</form>
<form action="" method="post">
    <input type="submit" class="btn btn-default" name="Newest" value="Newest ">
   <input type="submit" class="btn btn-default" name="high_price" value="High Price">
   <input type="submit" class="btn btn-default" name="low_price" value="Low Price">
</form>
<?php
        global $wpdb;
        $table_name ="theme_upload";
            //Pegination
            $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
            $limit = 2; // number of rows in page
            $offset = ( $pagenum - 1 ) * $limit;
            $total = $wpdb->get_var( "select count(*) as total from $table_name" );
            $num_of_pages = ceil( $total / $limit );
            $rowcount = $wpdb->num_rows;

        $category_s = $_POST['theme_category_name'];
        //Category Select
        if(isset($_POST['category_submit']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name WHERE theme_cat_name = '$category_s' limit  $offset, $limit");
        }
        //Newest
        elseif(isset($_POST['Newest']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY id DESC limit  $offset, $limit");
        }
        //Sorting for High Price
        elseif (isset ($_POST['high_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price DESC limit  $offset, $limit");
            }
        //Sorting for low Price
        elseif (isset ($_POST['low_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price ASC limit  $offset, $limit");
            }
        // Default Order
        else {
                $result = $wpdb->get_results( "SELECT * from $table_name limit  $offset, $limit" );
        }


        if($rowcount>0){    
            foreach($result as $results){
                //var_dump($results);

            ?>

            <?php

            }
        }
        else{
            echo "<tr><td cols=an='5'>No records found</td></tr>";
        } 
            ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //

?>
<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" value="all" >Select Your Categories</option>
            <?php 

            global $wpdb;
            $table_name ="theme_category";
            $category = $wpdb->get_results( "SELECT * from $table_name" );
            foreach($category as $categories){
                ?>
                <option class="form-control" value="<?=$categories->theme_category_name?>" ><?php echo $categories->theme_category_name;?></option>
                <?php
            }
            ?>

        </select> 
        <!-- Change these to buttons, share the name=submit -->
        <button type="submit" class="btn btn-info" name="submit" value="category_submit">GO</button>

        <!-- Move these into the same form tag and make them buttons -->
        <button type="submit" class="btn btn-default" name="submit" value="Newest">Newest</button>
        <button type="submit" class="btn btn-default" name="submit" value="high_price">High Price</button>
        <button type="submit" class="btn btn-default" name="submit" value="low_price">Low Price</button>

</form>
<?php
    global $wpdb;
    $table_name ="theme_upload";
        //Pegination
        $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
        $limit = 2; // number of rows in page
        $offset = ( $pagenum - 1 ) * $limit;
        $total = $wpdb->get_var( "select count(*) as total from $table_name" );
        $num_of_pages = ceil( $total / $limit );
        $rowcount = $wpdb->num_rows;

    $category_s = $_POST['theme_category_name'];

    // Get a list of valid categories
    $categories = $wpdb->get_results( "SELECT theme_category_name from theme_category", ARRAY_A );

    // Sort them into an array of values
    $allowed_categories = array_column($categories, 'theme_category_name');

    // Add in the default value (defined on your page)
    $allowed_categories[] = 'all';

    // Check the incoming value is valid / allowed
    if(in_array($category_s, $allowed_categories) === false) {
        // Set up your own error handling here
        echo "Invalid category";
        exit;
    }

    // define defaults
    $where = $order_by = "";

    // If a category is specified, define the WHERE clause
    if($category_s !== 'all') {
        $where = " WHERE theme_cat_name = '$category_s' ";
    }

    // Toggle your order_by values
    switch($_POST['submit']) {
        case 'Newest':
            $order_by = " ORDER BY id DESC ";
            break;

        case 'high_price':
            $order_by = " ORDER BY theme_price DESC ";
            break;

        case 'low_price':
            $order_by = " ORDER BY theme_price ASC ";
            break;
    }

    // Run only 1 SQL query to process all of the incoming information
    $result = $wpdb->get_results( "SELECT * from $table_name $where $order_by LIMIT $offset, $limit" );

    if($rowcount>0){    
        foreach($result as $results){
            //var_dump($results);

        ?>

        <?php

        }
    }
    else{
        echo "<tr><td cols=an='5'>No records found</td></tr>";
    } 
        ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //
?> 

主要问题是脚本运行SQL查询时相互隔离

我建议对代码进行一些修改,以基于可用的表单数据构建1个查询

此外,您还应该转义输入或执行某种检查传入的$\u POST值,而不仅仅是将类别名称的$\u POST值传递到SQL查询中。我已经添加了一个建议的解决方案,通过首先加载类别并检查是否有匹配项来确保$\u POST值有效

您可以在此处阅读有关在wordpress数据库类中使用预制语句的更多信息:

对于您的HTML部分:

<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" >Select Your Categories</option>
        <?php 

        global $wpdb;
        $table_name ="theme_category";
        $category = $wpdb->get_results( "SELECT * from $table_name" );
        foreach($category as $categories){
            ?>
            <option class="form-control" ><?php echo $categories->theme_category_name;?></option>
            <?php
        }
        ?>

    </select> 
    <input type="submit" class="btn btn-info" name="category_submit" value="GO"/>
</form>
<form action="" method="post">
    <input type="submit" class="btn btn-default" name="Newest" value="Newest ">
   <input type="submit" class="btn btn-default" name="high_price" value="High Price">
   <input type="submit" class="btn btn-default" name="low_price" value="Low Price">
</form>
<?php
        global $wpdb;
        $table_name ="theme_upload";
            //Pegination
            $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
            $limit = 2; // number of rows in page
            $offset = ( $pagenum - 1 ) * $limit;
            $total = $wpdb->get_var( "select count(*) as total from $table_name" );
            $num_of_pages = ceil( $total / $limit );
            $rowcount = $wpdb->num_rows;

        $category_s = $_POST['theme_category_name'];
        //Category Select
        if(isset($_POST['category_submit']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name WHERE theme_cat_name = '$category_s' limit  $offset, $limit");
        }
        //Newest
        elseif(isset($_POST['Newest']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY id DESC limit  $offset, $limit");
        }
        //Sorting for High Price
        elseif (isset ($_POST['high_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price DESC limit  $offset, $limit");
            }
        //Sorting for low Price
        elseif (isset ($_POST['low_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price ASC limit  $offset, $limit");
            }
        // Default Order
        else {
                $result = $wpdb->get_results( "SELECT * from $table_name limit  $offset, $limit" );
        }


        if($rowcount>0){    
            foreach($result as $results){
                //var_dump($results);

            ?>

            <?php

            }
        }
        else{
            echo "<tr><td cols=an='5'>No records found</td></tr>";
        } 
            ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //

?>
<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" value="all" >Select Your Categories</option>
            <?php 

            global $wpdb;
            $table_name ="theme_category";
            $category = $wpdb->get_results( "SELECT * from $table_name" );
            foreach($category as $categories){
                ?>
                <option class="form-control" value="<?=$categories->theme_category_name?>" ><?php echo $categories->theme_category_name;?></option>
                <?php
            }
            ?>

        </select> 
        <!-- Change these to buttons, share the name=submit -->
        <button type="submit" class="btn btn-info" name="submit" value="category_submit">GO</button>

        <!-- Move these into the same form tag and make them buttons -->
        <button type="submit" class="btn btn-default" name="submit" value="Newest">Newest</button>
        <button type="submit" class="btn btn-default" name="submit" value="high_price">High Price</button>
        <button type="submit" class="btn btn-default" name="submit" value="low_price">Low Price</button>

</form>
<?php
    global $wpdb;
    $table_name ="theme_upload";
        //Pegination
        $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
        $limit = 2; // number of rows in page
        $offset = ( $pagenum - 1 ) * $limit;
        $total = $wpdb->get_var( "select count(*) as total from $table_name" );
        $num_of_pages = ceil( $total / $limit );
        $rowcount = $wpdb->num_rows;

    $category_s = $_POST['theme_category_name'];

    // Get a list of valid categories
    $categories = $wpdb->get_results( "SELECT theme_category_name from theme_category", ARRAY_A );

    // Sort them into an array of values
    $allowed_categories = array_column($categories, 'theme_category_name');

    // Add in the default value (defined on your page)
    $allowed_categories[] = 'all';

    // Check the incoming value is valid / allowed
    if(in_array($category_s, $allowed_categories) === false) {
        // Set up your own error handling here
        echo "Invalid category";
        exit;
    }

    // define defaults
    $where = $order_by = "";

    // If a category is specified, define the WHERE clause
    if($category_s !== 'all') {
        $where = " WHERE theme_cat_name = '$category_s' ";
    }

    // Toggle your order_by values
    switch($_POST['submit']) {
        case 'Newest':
            $order_by = " ORDER BY id DESC ";
            break;

        case 'high_price':
            $order_by = " ORDER BY theme_price DESC ";
            break;

        case 'low_price':
            $order_by = " ORDER BY theme_price ASC ";
            break;
    }

    // Run only 1 SQL query to process all of the incoming information
    $result = $wpdb->get_results( "SELECT * from $table_name $where $order_by LIMIT $offset, $limit" );

    if($rowcount>0){    
        foreach($result as $results){
            //var_dump($results);

        ?>

        <?php

        }
    }
    else{
        echo "<tr><td cols=an='5'>No records found</td></tr>";
    } 
        ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //
?> 

选择您的类别

主要的问题是,您的脚本正在彼此隔离地运行SQL查询

我建议对代码进行一些修改,以基于可用的表单数据构建1个查询

此外,您还应该转义输入或执行某种检查传入的$\u POST值,而不仅仅是将类别名称的$\u POST值传递到SQL查询中。我已经添加了一个建议的解决方案,通过首先加载类别并检查是否有匹配项来确保$\u POST值有效

您可以在此处阅读有关在wordpress数据库类中使用预制语句的更多信息:

对于您的HTML部分:

<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" >Select Your Categories</option>
        <?php 

        global $wpdb;
        $table_name ="theme_category";
        $category = $wpdb->get_results( "SELECT * from $table_name" );
        foreach($category as $categories){
            ?>
            <option class="form-control" ><?php echo $categories->theme_category_name;?></option>
            <?php
        }
        ?>

    </select> 
    <input type="submit" class="btn btn-info" name="category_submit" value="GO"/>
</form>
<form action="" method="post">
    <input type="submit" class="btn btn-default" name="Newest" value="Newest ">
   <input type="submit" class="btn btn-default" name="high_price" value="High Price">
   <input type="submit" class="btn btn-default" name="low_price" value="Low Price">
</form>
<?php
        global $wpdb;
        $table_name ="theme_upload";
            //Pegination
            $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
            $limit = 2; // number of rows in page
            $offset = ( $pagenum - 1 ) * $limit;
            $total = $wpdb->get_var( "select count(*) as total from $table_name" );
            $num_of_pages = ceil( $total / $limit );
            $rowcount = $wpdb->num_rows;

        $category_s = $_POST['theme_category_name'];
        //Category Select
        if(isset($_POST['category_submit']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name WHERE theme_cat_name = '$category_s' limit  $offset, $limit");
        }
        //Newest
        elseif(isset($_POST['Newest']))
        {
            $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY id DESC limit  $offset, $limit");
        }
        //Sorting for High Price
        elseif (isset ($_POST['high_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price DESC limit  $offset, $limit");
            }
        //Sorting for low Price
        elseif (isset ($_POST['low_price'])) 
            {
                $result = $wpdb->get_results( "SELECT * from $table_name ORDER BY theme_price ASC limit  $offset, $limit");
            }
        // Default Order
        else {
                $result = $wpdb->get_results( "SELECT * from $table_name limit  $offset, $limit" );
        }


        if($rowcount>0){    
            foreach($result as $results){
                //var_dump($results);

            ?>

            <?php

            }
        }
        else{
            echo "<tr><td cols=an='5'>No records found</td></tr>";
        } 
            ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //

?>
<form action="" method="POST">
    <select name="theme_category_name" >
        <option class="form-control" value="all" >Select Your Categories</option>
            <?php 

            global $wpdb;
            $table_name ="theme_category";
            $category = $wpdb->get_results( "SELECT * from $table_name" );
            foreach($category as $categories){
                ?>
                <option class="form-control" value="<?=$categories->theme_category_name?>" ><?php echo $categories->theme_category_name;?></option>
                <?php
            }
            ?>

        </select> 
        <!-- Change these to buttons, share the name=submit -->
        <button type="submit" class="btn btn-info" name="submit" value="category_submit">GO</button>

        <!-- Move these into the same form tag and make them buttons -->
        <button type="submit" class="btn btn-default" name="submit" value="Newest">Newest</button>
        <button type="submit" class="btn btn-default" name="submit" value="high_price">High Price</button>
        <button type="submit" class="btn btn-default" name="submit" value="low_price">Low Price</button>

</form>
<?php
    global $wpdb;
    $table_name ="theme_upload";
        //Pegination
        $pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;      
        $limit = 2; // number of rows in page
        $offset = ( $pagenum - 1 ) * $limit;
        $total = $wpdb->get_var( "select count(*) as total from $table_name" );
        $num_of_pages = ceil( $total / $limit );
        $rowcount = $wpdb->num_rows;

    $category_s = $_POST['theme_category_name'];

    // Get a list of valid categories
    $categories = $wpdb->get_results( "SELECT theme_category_name from theme_category", ARRAY_A );

    // Sort them into an array of values
    $allowed_categories = array_column($categories, 'theme_category_name');

    // Add in the default value (defined on your page)
    $allowed_categories[] = 'all';

    // Check the incoming value is valid / allowed
    if(in_array($category_s, $allowed_categories) === false) {
        // Set up your own error handling here
        echo "Invalid category";
        exit;
    }

    // define defaults
    $where = $order_by = "";

    // If a category is specified, define the WHERE clause
    if($category_s !== 'all') {
        $where = " WHERE theme_cat_name = '$category_s' ";
    }

    // Toggle your order_by values
    switch($_POST['submit']) {
        case 'Newest':
            $order_by = " ORDER BY id DESC ";
            break;

        case 'high_price':
            $order_by = " ORDER BY theme_price DESC ";
            break;

        case 'low_price':
            $order_by = " ORDER BY theme_price ASC ";
            break;
    }

    // Run only 1 SQL query to process all of the incoming information
    $result = $wpdb->get_results( "SELECT * from $table_name $where $order_by LIMIT $offset, $limit" );

    if($rowcount>0){    
        foreach($result as $results){
            //var_dump($results);

        ?>

        <?php

        }
    }
    else{
        echo "<tr><td cols=an='5'>No records found</td></tr>";
    } 
        ?>


<?php
//Pagination Strat  //
$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'text-domain' ),
    'next_text' => __( '&raquo;', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
//Pagination END //
?> 

选择您的类别