我试图从数据库中显示我的产品的详细信息,但我只想使用按钮显示我点击的产品" afficher le detail"现在它立刻显示所有应用程序。 任何提示都会受到赞赏,因为我真的迷失了这个提示。 我不知道是否需要修改索引,class.app或产品页面。
这就是我对index.php的看法
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Applications</title>
</head>
<body>
<?php
include ("class.app.php");
$obj_app = new app();
include("class.client.php");
$obj_client = new client();
$btnajouterpanier=false;
if(isset($_SESSION['utilisateur'])){
if($obj_client->validate_login($_SESSION['utilisateur'],$_SESSION['motdepasse'])){
echo $obj_client->get_welcome_message($_SESSION['utilisateur']);
$btnajouterpanier=true;
}
}
if($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER["CONTENT_TYPE"] === 'application/json'){
$data = json_decode(file_get_contents('php://input'),true);
$resultat = array('ajout' => FALSE);
$_SESSION['panier'][$_POST($data['id'])]+= 1;
$resultat['ajout'] = TRUE;
}
echo '<form name="frm_cat" action="index.php" method="POST">';
echo '<select name="lst_cat">';
$obj_app->get_cat_options();
echo '</select>';
echo '<input type="submit" name="btn_cat" value="Filtrer" />';
echo '</form>';
if(isset($_POST['lst_cat'])){
$obj_app->get_app_list($_POST['lst_cat'],$btnajouterpanier);
}else{
$obj_app->get_app_list('',$btnajouterpanier);
}
?>
<script src="jquery-3.2.1.min.js"></script>
<script src="panier.js"></script>
</body>
</html>
这是我的class.app.php
<?php
class app{
private $dbh;
public function __construct(){
$this->dbh = new PDO('mysql:host=localhost;dbname=appstore','root','',array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ));
}
public function get_cat_options(){
$req = $this->dbh->prepare('SELECT * FROM cat ORDER BY titre');
$req->execute();
while($cat = $req->fetch()){
echo '<option value="'.$cat->id.'">'.$cat->titre.'</option>';
}
}
public function get_app_list($idcat="",$btnajouterpanier=false){
if ($idcat!=""){
$reqapp = $this->dbh->prepare("SELECT app.* FROM app JOIN appcat ON app.id=appcat.idapp WHERE appcat.idcat='".$idcat."'");
}else{
$reqapp = $this->dbh->prepare("SELECT app.* FROM app");
}
$reqapp->execute();
while($app = $reqapp->fetch()){
echo '<h3>'.$app->nom.'</h3><br/>';
echo '<img src="image/'.$app->image.'" /><br/>';
echo '<p>'.$app->description.'</p><br/>';
echo $app->prix.' $<br/>';
echo "<form name='btn_det' method='POST' action='produit.php'><input type='hidden' name='app_detail' value='".$app->id."' /><input type='submit' name='iddetail' value='Afficher le detail' data-id='".$app->id."'/></form>";
if($btnajouterpanier){
echo '<button type="button" class="btnajouterpanier" data-id="$app->id">Ajouter au panier</button>';
}
echo '<hr/>';
}
}
public function detail($iddetail){
$reqdet = $this->dbh->prepare('SELECT * FROM app where id = ?');
$reqdet->execute(array($iddetail));
while($app = $reqdet->fetch()){
echo '<h3>'.$app->nom.'</h3><br/>';
echo '<img src="image/'.$app->image.'" /><br/>';
echo '<p>'.$app->description.'</p><br/>';
echo $app->prix.' $<br/>';
}
}
}
?>
<script src="jquery-3.2.1.min.js"></script>
<script src="panier.js"></script>
这是我的produit.php页面
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Document sans nom</title>
</head>
<body>
<?php
include ("class.app.php");
$obj_app = new app();
$obj_app->detail($_POST['iddetail'])
?>
</body>
</html>