这是来自单页版本的代码。这样我就可以过滤帖子,但我不知道如何过滤过滤后的帖子,以显示 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>