php URL在提交后获得更改

jljoyd4f  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(109)

我试图做价格从低到高,从高到低的谷歌表,是成功地在我的网站集成排序。所有数据,包括价格,都显示正确。我做了排序功能,但每当我做低到高或高到低排序,网址得到改变,把我错误的类别。
例如,我在第页:

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重写
这是index.php页面上的代码,它将我带到我选择的所选类别的view-all-products.php页面

<a href="view-all-products.php?category=' . urlencode($category) . '" class="btn btn-whited btn-hover-primary text-capitalize view-all-button">View All</a>

这是view-all-products.php代码

<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">Price - Low To High</option>
                <option value="desc">Price - High To Low</option>
            </select>
        </form>
    </div>
</div>
<!-- Shop Top Bar Right End -->

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

<!-- Shop Wrapper Start -->
<?php
    // Check if the 'category' query parameter is set
    if (isset($_GET['category'])) {
        $category = $_GET['category'];
        $category = trim(urldecode($category));
    
        // Filter products for the specified 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 // Check if the category matches
            ) {
                $productTitle = '<b>(' . $row[0] . ') ' . $row[1] . '</b>';
                $productPrice = 'PKR ' . $row[3];
                $productImage = '';
                if (isset($row[8])) {
                    $productImage = $row[8];
                }
                $filteredProducts[] = [
                    'image' => $productImage,
                    'name' => $productTitle,
                    'price' => $productPrice,
                ];
            }
        }
        
        $sort = isset($_GET['sort']) ? $_GET['sort'] : 'asc';

        if ($sort === 'asc') {
            // Sort products from low to high based on price
            usort($filteredProducts, function ($a, $b) {
                $priceA = (int) $a['price'];
                $priceB = (int) $b['price'];
                return $priceA - $priceB;
            });
        } elseif ($sort === 'desc') {
            // Sort products from high to low based on price
            usort($filteredProducts, function ($a, $b) {
                $priceA = (int) $a['price'];
                $priceB = (int) $b['price'];
                return $priceB - $priceA;
            });
        }
    
        // Pagination settings
        $itemsPerPage = 60;
        $currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
        $totalItems = count($filteredProducts);
        $totalPages = ceil($totalItems / $itemsPerPage);
    
        // Calculate the start and end indexes for the current page
        $startIndex = ($currentPage - 1) * $itemsPerPage;
        $endIndex = min($startIndex + $itemsPerPage - 1, $totalItems - 1);
    
        // Display the category name and total product count
        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>';
    
        // Display the products for the current page
        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>';
            // Add To Cart button
            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>';
    
        // Display pagination
        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.';
    }
?>
jmo0nnb3

jmo0nnb31#

我重写了你的代码,希望它能像你需要的那样工作

<?php

if (isset($_GET['category'])) {
    $category = htmlspecialchars($_GET['category']);
    $category = urldecode(trim($category));
} else {
    echo 'ERROR: category not exist !';
}

?>
<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="<?= urlencode($category); ?>">
            <select class="nice-select" name="sort" aria-label=".form-select-sm example" onchange="this.form.submit()">
                <option value="asc">Price - Low To High</option>
                <option value="desc">Price - High To Low</option>
            </select>
        </form>
    </div>
</div>
<!-- Shop Top Bar Right End -->

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

<!-- Shop Wrapper Start -->
<?php

// Check if the 'category' query parameter is set
    if ($category != '') {
    
        // Filter products for the specified 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 // Check if the category matches
            ) {
                $productTitle = '<b>(' . $row[0] . ') ' . $row[1] . '</b>';
                $productPrice = 'PKR ' . $row[3];
                $productImage = '';
                if (isset($row[8])) {
                    $productImage = $row[8];
                }
                $filteredProducts[] = [
                    'image' => $productImage,
                    'name' => $productTitle,
                    'price' => $productPrice,
                ];
            }
        }
        
        $sort = isset($_GET['sort']) ? $_GET['sort'] : 'asc';

        if ($sort === 'asc') {
            // Sort products from low to high based on price
            usort($filteredProducts, function ($a, $b) {
                $priceA = (int) $a['price'];
                $priceB = (int) $b['price'];
                return $priceA - $priceB;
            });
        } elseif ($sort === 'desc') {
            // Sort products from high to low based on price
            usort($filteredProducts, function ($a, $b) {
                $priceA = (int) $a['price'];
                $priceB = (int) $b['price'];
                return $priceB - $priceA;
            });
        }
    
        // Pagination settings
        $itemsPerPage = 60;
        $currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
        $totalItems = count($filteredProducts);
        $totalPages = ceil($totalItems / $itemsPerPage);
    
        // Calculate the start and end indexes for the current page
        $startIndex = ($currentPage - 1) * $itemsPerPage;
        $endIndex = min($startIndex + $itemsPerPage - 1, $totalItems - 1);
    
        // Display the category name and total product count
        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>';
    
        // Display the products for the current page
        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>';
            // Add To Cart button
            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>';
    
        // Display pagination
        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.';
    }
?>

相关问题