Bouton de tri avec PHP sql dans un simple-MVC
Bonjour à tous cela fait plusieurs jours que je galère sur un fonction de tri par prix je travail avec un MVC voici mon code :
je suis en PHP 8.2
Dans mon productManager :
Code:
1 2 3 4 5 6 7 8
| public function sortByPrice(string $price): array
{
$sql = "SELECT * FROM WS_product ORDER BY price=:price";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':price', $price, \PDO::PARAM_STR);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} |
Dans mon productController:
Code:
1 2 3 4 5 6 7 8 9 10
| public function sortPrice(): string
{
if ($_SERVER['REQUEST_METHOD'] === 'get') {
$price = $_GET['price'];
$productManager = new ProductManager();
$productManager->sortByPrice($price);
header('Location:/product/sort?price=' . $price);
}
return $this->twig->render('Product/index.html.twig');
} |
Et voici mon formulaire en html:
Code:
1 2 3 4
| <form action="/product/sort" method="get">
<input type="hidden" name="price" id="price" value="{{ product.price }}">
<button class="btn btn-success m-3">Trier</button>
</form> |
Mon but est de pouvoir cliquer sur mon bouton du formulaire et qu'il trie mes produits par ordre de prix grâce à la valeur du bouton car j'ai d'autre bouton qui vont par la suite utilisé la même fonction.
Mais cela ne fonctionne pas et je ne sais plus quoi faire après 4 jours de recherche.
En vous remerciant par avance,