分页不适用于分类

时间:2017-06-09 10:23:07

标签: php mysql wordpress sorting pagination

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

问题1.排序和类别单独运行良好,但排序不适用于选择类别后。我想当用户选择一个类别时,他也可以按所选类别进行排序。

问题2.我的分页代码有效,但不正确,因为当我选择类别时,它没有被分类,与选择排序时相同的问题。

谢谢!

这是针对类别:

=OR(RIGHT(B2)="_",A1="Total",A2="Total")

这个用于排序:

<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>

所有php代码:

<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>

1 个答案:

答案 0 :(得分:0)

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

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

此外,您应该转义输入或进行某种检查传入的$ _POST值,绝对不仅仅是将类别名称的$ _POST值传递给SQL查询。我添加了一个建议的解决方案,通过首先加载类别并检查是否匹配来确保$ _POST值有效。

您可以在此处阅读有关在wordpress数据库类中使用预准备语句的更多信息:https://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks

对于您的HTML部分:

<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部分:

<?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 //
?>