【发布时间】:2017-11-11 07:49:51
【问题描述】:
我有问题。我正在研究用户可以输入他的信息,如姓名、图像等。然后所有用户信息将显示在页面上,并带有分类和分页排序。
问题 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代码:
<?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' => __( '«', 'text-domain' ),
'next_text' => __( '»', '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 mysql wordpress sorting pagination