Witam
Jak zrobić żeby w jednej sondzie były 4 pytania i do tego odpowiedzi ?
(Aktualnie jest tak, że jak dodaje drugie pytanie i do tego odpowiedzi id_poll = 2. To wyświetla mi się nowa sonda (mam do wyboru dwie sondy, każda sonda ma 1 pytanie i kilka odpowiedzi).
Przepuszczam, że będzie trzeba zrobić od nowa dwie tabele (poll_questions & poll_answers) i do tego nowe zapytania ? Czy mógłby mi ktoś pomóc, dać kilka wskazówek ?
Proszę o pomoc i wyrozumiałość, jestem początkujący.
Aktualnie wygląda to tak:
Baza danych MySQLTworzę bazę danych.
CREATE DATABASE sonda;
USE sonda;
CREATE TABLE poll_questions (
id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
title varchar(255) NOT NULL,
date_add datetime NOT NULL,
date_begin datetime NOT NULL,
date_end datetime NOT NULL,
stop int NOT NULL DEFAULT 0
);
CREATE TABLE poll_answers (
id_answer int NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_poll int NOT NULL,
answer varchar(255) NOT NULL,
votes int NOT NULL DEFAULT 0
);
następnie dodaje do bazy danych pytanie i odpowiedzi:
INSERT INTO poll_questions VALUES(
'',
'Twój ulubiony język programowania?',
now(),
now(),
'2020-03-01',
0
);
INSERT INTO poll_answers VALUES('', 1, 'C/C++', 0);
INSERT INTO poll_answers VALUES('', 1, 'Java', 0);
INSERT INTO poll_answers VALUES('', 1, 'PHP', 0);
INSERT INTO poll_answers VALUES('', 1, 'Python', 0);
INSERT INTO poll_answers VALUES('', 1, 'Inny', 0);
Kod skryptu sondy:index.php
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Language" content="pl">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
</head>
<body>
<?php
class Database {
public function __construct() {
}
public function query($sql) {
}
public function numrows($sql) {
}
public function fetch($sql) {
}
}
$db = new Database;
$db->query('SET NAMES utf8');
class Poll {
public $db;
public $other = true; // czy pokazywac inne sondy
public $desc_sort = true; // sortowanie innych sond od najnowszych
public $id; // id sondy
public $new_fields = array(); // funkcje z nowy polami do formularza public $no_add = false; // nie dodawac (np. ktoras z funkcji z $new_fields mowi, ze dane niepoprawne)
public function __construct()
{
$this->db = new Database();
}
public function display()
{
$sql = $this->db->query('SELECT
q.id, q.title, q.date_begin, q.date_end, q.stop,
a.id_answer, a.answer, a.votes,
(SELECT sum(votes) FROM poll_answers WHERE id_poll = q.id GROUP BY id_poll) as sum
FROM
poll_questions as q, poll_answers as a
WHERE
q.id = a.id_poll AND q.id = ' .
(!isset($_GET['id']) ?
'(SELECT max(id) FROM poll_questions)' : (int
)$_GET['id']));
if($this->db->numrows($sql) > 0)
{
while($row = $this->db->fetch($sql))
{
if($_POST['vote'] && !$this->no_add)
{
$row['sum']++;
if($row['id_answer'] == $_POST['vote']) $row['votes']++;
}
if(!$b)
{
$this->id = $row['id'];
if($row['stop'] == 1 || $_POST['vote'] && !$this->no_add) $noform = true;
// podstawowe dane o ankiecie
$ret .= '<b>' . $row['title'] . '</b><p />Łącznie oddano głosów: ' . $row['sum'].
'<br />Data rozpoczęcia: ' . $row['date_begin'] .
'<br />Data zakończenia: ' . $row['date_end'];
if($row['date_end'] <= $now) $ret .= '<p />Ankieta się już zakończyła.';
elseif($row['stop'] == 1) $ret .= '<p />Glosowanie w ankiecie zostało wstrzymane.';
$ret .= '<p />';
// wyswietlenie formularza
if(!isset($_COOKIE['poll' . $this->id]) && $row['date_end'] > $now && !$noform) {
$ret .= '<form action="" method="post">';
foreach($this->new_fields as $v) $ret .= $v;
$form = true;
} elseif(isset($_COOKIE['poll' . $this->id]) && $row['date_end'] > $now && !$noform) {
$ret .= 'Głosowałeś już w tej sondzie.<p />';
}
// user zaglosowal
if($_POST['vote'] && !$this->no_add)
{
$ret .= 'Twój głos został dodany.<p />';
if(!isset($_COOKIE['poll' . $this->id])) {
$this->db->query('UPDATE poll_answers SET votes=votes+1 WHERE id_answer='.$_POST['vote']);
setcookie('poll' . $this->id, $this->id, time()+3600
* 3600
* 30
); // 22 lata }
$noform = true;
}
$b = true;
}
// wyswietlenie wariantow odpowiedzi
if($form)
$ret .= '<input type="radio" name="vote" value="' . $row['id_answer'] . '" /> ' .
$row['answer'] . '<br />';
else
{
$ret .= $row['answer'].', ' . $row['votes'] . ' glosow, ' .
($row['sum'] > 0 ?
round($row['votes']*100
/$row['sum']) : 0) . '% ' . '<div style="background: red; height: 10px; width: ' .
($row['votes'] == 0
|| $row['sum'] == 0 ? 5
: round($row['votes'] * 200
/ $row['sum'])) . 'px"></div><br />';
}
}
if($form) $ret .= '<br /><input type="submit" name="submit" value="Głosuj!" /></form>';
if($this->other) $ret .= '<p /><b>Inne sondy</b><p />' . $this->other($this->id);
}
else $ret = 'Nie ma takiej sondy w bazie.';
return $ret;
}
public function other($id)
{
$sql = 'SELECT id, title FROM poll_questions WHERE id <> ' . $id . ' ORDER BY id ' . ($this->desc_sort ? 'DESC' : 'ASC');
$sql = $this->db->query($sql);
if($this->db->numrows($sql) > 0)
{
$ret = '<ul>';
while($row = $this->db->fetch($sql))
$ret .= '<li><a href="' .$_SERVER['PHP_SELF'] . '?id=' . $row['id'] . '">' . $row['title'] . '</a></li>';
return $ret . '</ul>';
}
else return '(brak)';
}
}
$poll = new Poll;
?>
</body>
</html>