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 MySQL

Tworzę bazę danych.

  1. CREATE DATABASE sonda;
  2. USE sonda;
  3.  
  4. CREATE TABLE poll_questions (
  5. id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
  6. title varchar(255) NOT NULL,
  7. date_add datetime NOT NULL,
  8. date_begin datetime NOT NULL,
  9. date_end datetime NOT NULL,
  10. stop int NOT NULL DEFAULT 0
  11. );
  12.  
  13. CREATE TABLE poll_answers (
  14. id_answer int NOT NULL PRIMARY KEY AUTO_INCREMENT,
  15. id_poll int NOT NULL,
  16. answer varchar(255) NOT NULL,
  17. votes int NOT NULL DEFAULT 0
  18. );


następnie dodaje do bazy danych pytanie i odpowiedzi:

  1. INSERT INTO poll_questions VALUES(
  2. '',
  3. 'Twój ulubiony język programowania?',
  4. now(),
  5. now(),
  6. '2020-03-01',
  7. 0
  8. );
  9. INSERT INTO poll_answers VALUES('', 1, 'C/C++', 0);
  10. INSERT INTO poll_answers VALUES('', 1, 'Java', 0);
  11. INSERT INTO poll_answers VALUES('', 1, 'PHP', 0);
  12. INSERT INTO poll_answers VALUES('', 1, 'Python', 0);
  13. INSERT INTO poll_answers VALUES('', 1, 'Inny', 0);


Kod skryptu sondy:

index.php

  1. <!DOCTYPE html
  2. PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html>
  6.  
  7. <meta http-equiv="Content-Language" content="pl">
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  9.  
  10. <head>
  11. </head>
  12. <body>
  13.  
  14.  
  15.  
  16. <?php
  17.  
  18. error_reporting(E_ALL ~ E_NOTICE);
  19.  
  20. class Database {
  21. public function __construct() {
  22. mysql_connect('localhost', 'root', '123456');
  23. mysql_select_db('sonda');
  24. }
  25.  
  26. public function query($sql) {
  27. return mysql_query($sql);
  28. }
  29.  
  30. public function numrows($sql) {
  31. return mysql_num_rows($sql);
  32. }
  33.  
  34. public function fetch($sql) {
  35. return mysql_fetch_array($sql);
  36. }
  37. }
  38.  
  39. $db = new Database;
  40. $db->query('SET NAMES utf8');
  41.  
  42. class Poll {
  43. public $db;
  44. public $other = true; // czy pokazywac inne sondy
  45. public $desc_sort = true; // sortowanie innych sond od najnowszych
  46. public $id; // id sondy
  47. public $new_fields = array(); // funkcje z nowy polami do formularza
  48. public $no_add = false; // nie dodawac (np. ktoras z funkcji z $new_fields mowi, ze dane niepoprawne)
  49.  
  50. public function __construct()
  51. {
  52. $this->db = new Database();
  53. }
  54.  
  55. public function display()
  56. {
  57. $sql = $this->db->query('SELECT
  58. q.id, q.title, q.date_begin, q.date_end, q.stop,
  59. a.id_answer, a.answer, a.votes,
  60. (SELECT sum(votes) FROM poll_answers WHERE id_poll = q.id GROUP BY id_poll) as sum
  61. FROM
  62. poll_questions as q, poll_answers as a
  63. WHERE
  64. q.id = a.id_poll AND q.id = ' .
  65. (!isset($_GET['id']) ? '(SELECT max(id) FROM poll_questions)' : (int)$_GET['id']));
  66.  
  67. if($this->db->numrows($sql) > 0)
  68. {
  69. $now = date('Y-m-d');
  70. while($row = $this->db->fetch($sql))
  71. {
  72. if($_POST['vote'] && !$this->no_add)
  73. {
  74. $row['sum']++;
  75. if($row['id_answer'] == $_POST['vote']) $row['votes']++;
  76. }
  77.  
  78. if(!$b)
  79. {
  80. $this->id = $row['id'];
  81. if($row['stop'] == 1 || $_POST['vote'] && !$this->no_add) $noform = true;
  82.  
  83. // podstawowe dane o ankiecie
  84. $ret .= '<b>' . $row['title'] . '</b><p />Łącznie oddano głosów: ' . $row['sum'].
  85. '<br />Data rozpoczęcia: ' . $row['date_begin'] .
  86. '<br />Data zakończenia: ' . $row['date_end'];
  87.  
  88. if($row['date_end'] <= $now) $ret .= '<p />Ankieta się już zakończyła.';
  89. elseif($row['stop'] == 1) $ret .= '<p />Glosowanie w ankiecie zostało wstrzymane.';
  90.  
  91. $ret .= '<p />';
  92.  
  93. // wyswietlenie formularza
  94. if(!isset($_COOKIE['poll' . $this->id]) && $row['date_end'] > $now && !$noform)
  95. {
  96. $ret .= '<form action="" method="post">';
  97. foreach($this->new_fields as $v) $ret .= $v;
  98. $form = true;
  99. } elseif(isset($_COOKIE['poll' . $this->id]) && $row['date_end'] > $now && !$noform)
  100. {
  101. $ret .= 'Głosowałeś już w tej sondzie.<p />';
  102. }
  103.  
  104. // user zaglosowal
  105. if($_POST['vote'] && !$this->no_add)
  106. {
  107. $ret .= 'Twój głos został dodany.<p />';
  108. if(!isset($_COOKIE['poll' . $this->id]))
  109. {
  110. $this->db->query('UPDATE poll_answers SET votes=votes+1 WHERE id_answer='.$_POST['vote']);
  111. setcookie('poll' . $this->id, $this->id, time()+3600 * 3600 * 30); // 22 lata
  112. }
  113. $noform = true;
  114. }
  115. $b = true;
  116. }
  117.  
  118. // wyswietlenie wariantow odpowiedzi
  119. if($form)
  120. $ret .= '<input type="radio" name="vote" value="' . $row['id_answer'] . '" /> ' .
  121. $row['answer'] . '<br />';
  122. else
  123. {
  124. $ret .= $row['answer'].', ' . $row['votes'] . ' glosow, ' .
  125. ($row['sum'] > 0 ? round($row['votes']*100/$row['sum']) : 0) . '% ' .
  126. '<div style="background: red; height: 10px; width: ' .
  127. ($row['votes'] == 0 || $row['sum'] == 0 ? 5 : round($row['votes'] * 200 / $row['sum'])) .
  128. 'px"></div><br />';
  129. }
  130. }
  131. if($form) $ret .= '<br /><input type="submit" name="submit" value="Głosuj!" /></form>';
  132. if($this->other) $ret .= '<p /><b>Inne sondy</b><p />' . $this->other($this->id);
  133. }
  134. else $ret = 'Nie ma takiej sondy w bazie.';
  135. return $ret;
  136. }
  137.  
  138. public function other($id)
  139. {
  140. $sql = 'SELECT id, title FROM poll_questions WHERE id <> ' . $id . ' ORDER BY id ' . ($this->desc_sort ? 'DESC' : 'ASC');
  141. $sql = $this->db->query($sql);
  142. if($this->db->numrows($sql) > 0)
  143. {
  144. $ret = '<ul>';
  145. while($row = $this->db->fetch($sql))
  146. $ret .= '<li><a href="' .$_SERVER['PHP_SELF'] . '?id=' . $row['id'] . '">' . $row['title'] . '</a></li>';
  147. return $ret . '</ul>';
  148. }
  149. else return '(brak)';
  150. }
  151. }
  152.  
  153. $poll = new Poll;
  154. echo $poll->display();
  155.  
  156. ?>
  157.  
  158. </body>
  159. </html>