You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

价格排序后URL跳转异常:商品分类参数错误变更求助

排序后分类参数异常变更的问题排查与解决

问题描述

已完成Google Sheet与网站的集成,商品数据(含价格)可正常显示,也实现了价格升降序排序功能,但执行排序操作后,URL中的分类参数会被错误替换,跳转到非当前分类页面。

示例:

  • 当前页面URL:
https://example.com/view-all-products.php?category=Baby+%26+Toddler+Toys
  • 排序后跳转URL:
https://example.com/view-all-products.php?category=stuffed+%26+plush+toys&sort=asc

项目无.htaccess文件,未做URL重写操作。

原因分析

问题核心是表单渲染时机错误:
排序表单的隐藏category字段,在PHP处理$_GET['category']参数之前就已输出。此时$category变量未被正确赋值,会被之前商品循环中最后一个商品的分类($row[2])覆盖,导致表单提交时携带错误的分类参数。

解决方案

  1. 提前处理分类参数:在页面最开始就获取并处理$_GET['category'],确保渲染表单时$category变量已持有当前页面的正确分类值。
  2. 修复排序逻辑漏洞:原代码中直接将带"PKR"前缀的价格转整数会得到0,导致排序失效,需提取价格中的数字部分再排序。

修改后的代码

第一步:在view-all-products.php顶部提前处理分类参数

<?php
// 提前获取并处理分类参数,确保表单渲染时变量已赋值
$category = '';
if (isset($_GET['category'])) {
    $category = trim(urldecode($_GET['category']));
}
?>

第二步:调整表单及后续代码

<div class="shop-top-bar-right mb-4">
    <h4 class="title me-2">Sort By: </h4>
    <div class="shop-short-by">
        <form method="GET" action="">
            <input type="hidden" name="category" value="<?php echo htmlspecialchars($category); ?>">
            <select class="nice-select" name="sort" aria-label=".form-select-sm example" onchange="this.form.submit()">
                <!-- 保留当前排序状态 -->
                <option value="asc" <?php echo (isset($_GET['sort']) && $_GET['sort'] == 'asc') ? 'selected' : ''; ?>>Price - Low To High</option>
                <option value="desc" <?php echo (isset($_GET['sort']) && $_GET['sort'] == 'desc') ? 'selected' : ''; ?>>Price - High To Low</option>
            </select>
        </form>
    </div>
</div>
<!-- Shop Top Bar Right End -->

</div>
<!--shop toolbar end-->

<!-- Shop Wrapper Start -->
<?php
if ($category) {
    // 过滤当前分类的商品
    $filteredProducts = [];
    foreach ($values as $row) {
        if (
            isset($row[0]) && isset($row[1]) && isset($row[2]) && isset($row[3]) && isset($row[4])
            && trim($row[2]) === $category
        ) {
            $productTitle = '<b>(' . $row[0] . ') ' . $row[1] . '</b>';
            $productPrice = 'PKR ' . $row[3];
            $productImage = isset($row[8]) ? $row[8] : '';
            
            $filteredProducts[] = [
                'image' => $productImage,
                'name' => $productTitle,
                'price' => $productPrice,
            ];
        }
    }
    
    $sort = isset($_GET['sort']) ? $_GET['sort'] : 'asc';

    // 修复排序逻辑:提取价格中的数字部分
    if ($sort === 'asc') {
        usort($filteredProducts, function ($a, $b) {
            $priceA = (int)preg_replace('/[^0-9]/', '', $a['price']);
            $priceB = (int)preg_replace('/[^0-9]/', '', $b['price']);
            return $priceA - $priceB;
        });
    } elseif ($sort === 'desc') {
        usort($filteredProducts, function ($a, $b) {
            $priceA = (int)preg_replace('/[^0-9]/', '', $a['price']);
            $priceB = (int)preg_replace('/[^0-9]/', '', $b['price']);
            return $priceB - $priceA;
        });
    }

    // 分页设置及商品展示代码保持不变
    $itemsPerPage = 60;
    $currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
    $totalItems = count($filteredProducts);
    $totalPages = ceil($totalItems / $itemsPerPage);
    
    $startIndex = ($currentPage - 1) * $itemsPerPage;
    $endIndex = min($startIndex + $itemsPerPage - 1, $totalItems - 1);
    
    echo '<div class="shop-top-bar-left mb-4">';
    echo '<div class="shop-top-show">';
    echo '<span style="font-size: 24px; font-weight:900">' . $category . '</span> <span style="font-size: 24px;"> - ' . $totalItems . ' items</span>';
    echo '</div>';
    echo '</div>';
    
    echo '<div class="row shop_wrapper grid_3">';
    for ($i = $startIndex; $i <= $endIndex; $i++) {
        $product = $filteredProducts[$i];
        echo '<div class="col-lg-4 col-md-4 col-sm-6 product">';
        echo '<div class="product-inner">';
        echo '<div class="thumb">';
        echo '<img class="first-image" src="' . $product['image'] . '" alt="Product" />';
        echo '</div>';
        echo '<div class="content">';
        echo '<h5 class="title">' . $product['name'] . '</h5>';
        echo '<span class="price">' . $product['price'] . '</span>';
        echo '<div class="add-cart-btn mt-3">';
        echo '<button class="btn btn-primary  text-capitalize add-to-cart">Add To Cart</button>';
        echo '</div>';
        echo '</div>';
        echo '</div>';
        echo '</div>';
    }
    echo '</div>';
    
    // 分页代码保持不变
    echo '<div class="shop-top-bar-right mb-4">';
    echo '<nav>';
    echo '<ul class="pagination">';
    if ($currentPage > 1) {
        echo '<li class="page-item">';
        echo '<a class="page-link rounded-0" href="view-all-products.php?category=' . urlencode($category) . '&page=' . ($currentPage - 1) . '&sort=' . $sort . '" aria-label="Previous">';
        echo '<span aria-hidden="true">&laquo;</span>';
        echo '</a>';
        echo '</li>';
    } else {
        echo '<li class="page-item disabled">';
        echo '<a class="page-link rounded-0" href="#" aria-label="Previous">';
        echo '<span aria-hidden="true">&laquo;</span>';
        echo '</a>';
        echo '</li>';
    }
    for ($page = 1; $page <= $totalPages; $page++) {
        if ($page == $currentPage) {
            echo '<li class="page-item active"><a class="page-link" href="#">' . $page . '</a></li>';
        } else {
            echo '<li class="page-item"><a class="page-link" href="view-all-products.php?category=' . urlencode($category) . '&page=' . $page . '&sort=' . $sort . '">' . $page . '</a></li>';
        }
    }
    if ($currentPage < $totalPages) {
        echo '<li class="page-item">';
        echo '<a class="page-link rounded-0" href="view-all-products.php?category=' . urlencode($category) . '&page=' . ($currentPage + 1) . '&sort=' . $sort . '" aria-label="Next">';
        echo '<span aria-hidden="true">&raquo;</span>';
        echo '</a>';
        echo '</li>';
    } else {
        echo '<li class="page-item disabled">';
        echo '<a class="page-link rounded-0" href="#" aria-label="Next">';
        echo '<span aria-hidden="true">&raquo;</span>';
        echo '</a>';
        echo '</li>';
    }
    echo '</ul>';
    echo '</nav>';
    echo '</div>';
} else {
    echo 'Category not specified.';
}
?>

内容的提问来源于stack exchange,提问作者SZ100

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 13:35:54