使用单选按钮 mvc 设置带有单选按钮的表的显示



好吧,我在每个网站上搜索,但我没有找到我想要的东西,所以: 我会更容易让你看到我有什么: 我的页面;)

因此,对于每个单选按钮,我想设置表格的显示。

例如:如果我选择"Alphabétique",则值按字母顺序排序。

我知道我需要使用Ajax,但我绝对不舒服。除此之外,我还在MVC中编程我的项目。 我对Google,Stack,OpenClassroom等进行了一些研究。 而且我不明白我需要在 Ajax 中做什么,或者我需要在哪里添加说明。 所以这是我的代码:

控制器精品.php(控制器):

<?php 
require('../Views/codes/boutique.php'); 
require('../Models/vehicule.php');
$query = getVehiculesByAlpha();
require('../Views/codes/tabAffich.php');
?>

精品.php : (查看)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rentcar - Boutique</title>
<link rel="stylesheet" href="../Views/styles/style2.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
</head>
<body>
<?php require("menu.php");?>
<section id="section" class="main-section cleanbg">
<div class="container">
<label>Trier par :</label>
<div class="single-col">
<div class="styled-input-container">
<div class="styled-input-single">
<input type="radio" name="fieldset-1" id="radioAlpha" value="Alpha" checked="checked"/>
<label for="radioAlpha">Alphabétique</label>
</div>
<div class="styled-input-single">
<input type="radio" name="fieldset-1" id="radioModel" value ="Model"/>
<label for="radioModel">Modèle</label>
</div>
<div class="styled-input-single">
<input type="radio" name="fieldset-1" id="radioKm" value ="Km"/>
<label for="radioKm">Kilométrage</label>
</div>
<div class="styled-input-single">
<input type="radio" name="fieldset-1" id="radioDispo" value ="Dispo"/>
<label for ="radioDispo"> Disponible </label>
</div>
</div>
</div>
</div>
<div class="search">
<label class="lblSearch"> Rechercher :</label>
<input type="text" id="search-box" class="search-box">
<button class="btnSearch">Chercher</button>
</div>
</section>
<section id="section" class="main-section cleanbg">
<div class="container">
<hr>

tabAffich.php : (查看表)

<div class="wrapper">
<div class="table">
<div class="row header">
<div class = "cell">Marque</div>
<div class = "cell">Modèle</div>
<div class= "cell">Kilométrage</div>
</div>
<?php 
while($res = $query->fetch()){
?> 
<div class="row">
<div class ="cell">
<?php echo $res['nom'];?>
</div>
<div class="cell">
<?php echo $res['modele'];?>
</div>
<div class="cell">
<?php echo $res['km'];?>
</div>
</div>
<?php 
}
$query->closeCursor();
?> 
</div>
</div>
</div>
</section>
</body>
</html>

车辆.php(型号):

<?php 
function getVehiculesByAlpha(){
try{
$db = new PDO('mysql:host=localhost;dbname=ProjectRentcar;charset=utf8','root','root');
}catch(Exception $e){
die('Erreur : '.$e->getMessage());
}
$query = $db->query('
SELECT nom, modele, km 
FROM Vehicule V, Marque Ma 
WHERE V.numMarque = Ma.numMarque
ORDER BY nom, modele');
return $query;
}
function getVehiculesByModel(){
//Same code with order differently
}
function getVehiculesByKm(){
//Same here
}

所以我会知道如何在单击特定单选按钮时更改表格的显示,以及在 Ajax 😉 中的一些帮助 提前❤️谢谢你

你可以只使用PHP来实现这一点,但可以肯定的是,UX最好用Ajax来实现这一点(对你来说,你也会学到更多)。

1)所以首先你需要一个javascript函数,当你点击input[type=radio]时会触发,例如,如果你点击单选按钮id == radioAlpha它将运行一个使用Ajax请求到你的vehicule.php?sort=alphabetique的函数。如果你点击单选按钮id == radioModel它应该运行一个使用 Ajax 请求到你的vehicule.php?sort=model的函数 - 你可以使用 POST 或 GET 请求使用 Ajax 发送数据,你喜欢什么。

2)然后你应该改变你的vehicule.php文件,它还应该包含这样的内容:

if ($_GET['sort'] == 'alphabetique') {
// run function with sort for alphabetique
// echo data with json format for example or even with html
}
if ($_GET['sort'] == 'models') {
// run function with sort for model
// echo data with json format for example or even with html
}
...
...

3) 文件tabAffich.php

<?php 
while($res = $query->fetch()){
?>
<div id="sorttable"> // Add this line
<div class="row">
<div class ="cell">
<?php echo $res['nom'];?>
</div>
<div class="cell">
<?php echo $res['modele'];?>
</div>
<div class="cell">
<?php echo $res['km'];?>
</div>
</div>
<?php 
}
$query->closeCursor();
?> 
</div>

回到 Ajax 请求,当它成功调用并带有响应时,您可以在<div id="sorttable">容器中生成新数据。你需要一些javascript的知识,如何清除数据并从ajax生成新的响应,但它并不难学习。

我希望这能帮助你理解在这种情况下如何使用ajax。

有很多方法可以达到你想要达到的效果,我认为我介绍的方法很简单。

最新更新