gpt4 book ai didi

php - 分页在排序中不起作用

转载 作者:行者123 更新时间:2023-11-30 21:57:52 27 4
gpt4 key购买 nike

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

问题 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' => __( '&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 个查询。

此外,您应该对输入进行转义或进行某种检查传入的 $_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 //
?>

关于php - 分页在排序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44455330/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com