php - 单击按钮时如何遍历来自数据库的数据
问题描述
我正在制作一个网站来提交问卷的答案。
每个问题都是由仪表板的管理员创建的,目标是通过从数据库中获取这些问题的问题和答案来使页面动态化。
基本上是这样的:
所以目标是当我单击其中一个按钮的答案时,显示下一个问题(从数据库中获取第二个问题以及与该问题相关的三个答案等等)。
对于后端开发,我使用的是纯 PHP,因为我是编程新手,并且PDO
用于处理数据库中的数据。
为了管理我正在使用控制器的数据,为了连接数据库,我已经建立了连接/core/Database.php
任何帮助表示赞赏..感谢您的时间
这是我到目前为止所尝试的。
<?php
session_start();
require './controllers/UserController.php';
$user = new UserController;
$arr = $users=$user->getQuestions();
$index = 0;
$answ1 = $user->getAnswers();
$index = 0;
$_SESSION['answerOne'] = $_POST['question1'];
// $_SESSION['answerTwo'] = $_POST['question2'];
// $_SESSION['answerThree'] = $_POST['question3'];
// $_SESSION['answerFour'] = $_POST['question4'];
echo $_SESSION['answerOne'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./styling/questionstyle.css">
<title>Question</title>
</head>
<body>
<div class="hero">
<div class="questionContainer">
<div class="header">
<img src="./assets/back.png" height="35%" class="backImage">
<h2 class="questionNumber">01/04</h2>
</div>
<progress max="100" value="10"></progress>
<h1 class="question">
<?php echo $arr[$index]['description'] ?>
</h1>
</div>
<div class="answerContainer">
<img src="./assets/shadow.png" width="100%">
<form action="" method="POST">
<button class="button" onclick="document.getElementById('q1').value ='a'"><h4 class="choice">a</h4><h4 class="answer"><?php echo $answ1[$index]['choice1']?></h4></button> <br>
<button class="button2" onclick="document.getElementById('q1').value ='b'"><h4 class="choice">b</h4><h4 class="answer"><?php echo $answ1[$index]['choice2']?></h4></button> <br>
<button class="button2"onclick="document.getElementById('q1').value ='c'" ><h4 class="choice">c</h4><h4 class="answer"><?php echo $answ1[$index]['choice3']?></h4></button>
<input type="hidden" id="q1" name="question1">
<input type="hidden" id="q2" name="question2">
<input type="hidden" id="q3" name="question3">
<input type="hidden" id="q4" name="question4">
<br>
</form>
</div>
</div>
</body>
</html>
<?php
include './core/Database.php';
?>
class UserController
{
protected $db;
public function __construct()
{
$this->db = new Database;
}
public function all()
{
$query = $this->db->pdo->query('SELECT * FROM user');
return $query->fetchAll();
}
public function store($request)
{
isset($request['is_admin']) ? $isAdmin = 1 : $isAdmin = 0;
$password = password_hash($request['password'], PASSWORD_DEFAULT);
$query = $this->db->pdo->prepare('INSERT INTO users (name, email, password, is_admin) VALUES (:name, :email, :password, :is_admin)');
$query->bindParam(':name', $request['fullName']);
$query->bindParam(':email', $request['email']);
$query->bindParam(':password', $password);
$query->bindParam(':is_admin', $isAdmin);
$query->execute();
return header('Location: ./index.php');
}
public function edit($id)
{
$query = $this->db->pdo->prepare('SELECT * FROM users WHERE id = :id');
$query->execute(['id' => $id]);
return $query->fetch();
}
public function update($id, $request)
{
isset($request['is_admin']) ? $isAdmin = 1 : $isAdmin = 0;
$query = $this->db->pdo->prepare('UPDATE users SET name = :name, email = :email, is_admin = :is_admin WHERE id = :id');
$query->execute([
'name' => $request['fullName'],
'email' => $request['email'],
'is_admin' => $isAdmin,
'id' => $id
]);
return header('Location: ./index.php');
}
public function destroy($id)
{
$query = $this->db->pdo->prepare('DELETE FROM users WHERE id = :id');
$query->execute(['id' => $id]);
return header('Location: ./index.php');
}
public function totalpa(){
$query= $this->db->pdo->query('SELECT count(*) FROM user');
$query->execute();
return $query->fetchColumn();
}
public function totalfemra(){
$query= $this->db->pdo->query("SELECT count(*) FROM user WHERE gender='F'");
$query->execute();
return $query->fetchColumn();
}
public function totalmeshkuj(){
$query=$this->db->pdo->query("SELECT count(*) from user WHERE gender ='M'");
$query->execute();
return $query->fetchColumn();
}
public function storeUser($req){
$query = $this->db->pdo->prepare('INSERT INTO user (city, gender, age) VALUES (:city, :gender, :age)');
$query->bindParam(':city', $_SESSION['cityID']);
$query->bindParam(':gender', $_SESSION['gender']);
$query->bindParam(':age', $_SESSION['age']);
$query->execute();
}
public function storeQuestion($req){
$query=$this->db->pdo->prepare('INSERT INTO question(description) VALUES(:description)');
$query->bindParam(':description',$_POST['pyetja']);
$query->execute();
}
public function storeAnswer($param){
$query=$this->db->pdo->prepare('INSERT into add_answer (q_id,choice1,choice2,choice3) VALUES(:q_id,:ch1,:ch2,:ch3)');
$query->bindParam(':q_id',$param);
$query->bindParam(':ch1', $_POST['choice1']);
$query->bindParam(':ch2',$_POST['choice2']);
$query->bindParam(':ch3',$_POST['choice3']);
$query->execute();
}
public function getLastInsertedId(){
$query=$this->db->pdo->prepare('SELECT MAX(qid) from question');
$query->execute();
return $query->fetchColumn();
}
public function getQuestions(){
$query= $this->db->pdo->query('SELECT * FROM question');
$query->execute();
return $query->fetchAll();
}
public function getAnswers(){
try{
$query =$this->db->pdo->query('SELECT * FROM add_answer a INNER JOIN question q ON a.q_id = q.qid');
$query -> execute();
return $query->fetchAll();
}catch(Exception $ex){
echo 'error';
}
}
}
解决方案
推荐阅读
- sql - 哪些云存储提供商允许您逐行查询文本文件?
- android - 有人在颤动中使用 blue_thermal_printer 成功地将图像打印到斑马蓝牙打印机吗?
- python - 在函数中输入列表有什么问题?
- flutter - 哪种方式在专业环境中更常见?
- java - 在休眠中使用onetoone共享pk
- javascript - 如何使用 Javascript/Jquery 重定向到特定窗口宽度的页面?
- java - 如何将变量传递给 SQL 查询
- javascript - 清除 input type='date' 时如何捕获?
- php - 必填字段变为红色
- c# - 带有 Hangfire 的 CancellationToken