Сделал небольшой скрипт опроса
Протестируйте пожалуйста http://dkakharov.000webhostapp.com/
Иногда будет выдавать результат на +/- 1 процент от ста. Вот весь код db.php <?php error_reporting(E_ALL); $host = 'localhost'; $user = 'root'; $password = ''; $db_name = 'quiz'; $link = mysqli_connect($host, $user, $password, $db_name) or die(mysqli_error($link)); mysqli_query($link, "SET NAMES 'utf8'"); ?> index.php <?php require_once ('db.php'); $sql = "SELECT * FROM question"; $query = mysqli_query($link, $sql) or die(mysqli_error($link)); for ($res = []; $row = mysqli_fetch_assoc($query); $res[] = $row); echo '<pre>'; //print_r($res); echo '</pre>'; ?> <!DOCTYPE html> <html> <head> <title>Опрос</title> <meta charset="utf-8"> <link rel="stylesheet" href="style.css"> </head> <body> <div class="main"> <h1>Опрос</h1> <p class="result">Укажите Ваш возраст:</p> <form action="" method="get"> <?php foreach($res as $key) { echo '<p style="font-weight: bold;"><label><input type="radio" name="age" value="'.$key['variant'].'">'.$key['titile'].'</label></p>'; } ?> <input type="submit" class="btn-answer"> </form> <?php if(!empty($_GET['age'])) { $needAnswer = "SELECT * FROM answer WHERE question_id =" . $_GET['age']; $resultNeedAnswer = mysqli_query($link, $needAnswer) or die(mysqli_error($link)); $result = mysqli_fetch_assoc($resultNeedAnswer); $oldCount = $result['count_answer']; $newCount = $oldCount + 1; echo '<pre>'; //print_r($result); echo '</pre>'; $sqlUpdate = "UPDATE answer SET count_answer = '".$newCount."' WHERE question_id = " . $_GET['age']; mysqli_query($link, $sqlUpdate) or die(mysqli_error($link)); $allAnswer = "SELECT * FROM answer"; $resultAllAnswer = mysqli_query($link, $allAnswer) or die(mysqli_error($link)); for ($resAllAnswer = []; $rowAllAnswer = mysqli_fetch_assoc($resultAllAnswer); $resAllAnswer[] = $rowAllAnswer); echo '<pre>'; //print_r($resAllAnswer); echo '</pre>'; $allPeople = 0; foreach($resAllAnswer as $k) { $allPeople = $allPeople + $k['count_answer']; } $onePercent = $allPeople/100; //для погрешности $oneItem = round($resAllAnswer[0]['count_answer'] / $onePercent); $two = round($resAllAnswer[1]['count_answer'] / $onePercent); $three = round($resAllAnswer[2]['count_answer'] / $onePercent); echo '<p>Результат опроса</p>'; echo '<div class="result"> <p>Общее количество опрошенных: <span class="bold">'.$allPeople.'</span><p> <ol> <li>. Ответили "'.$res[0]['titile'].'": <span class="bold">'.$resAllAnswer[0]['count_answer'].'</span> человек, <span class="bold">'.$oneItem.'%</span> опрошенных</li> <li>. Ответили "'.$res[1]['titile'].'": <span class="bold">'.$resAllAnswer[1]['count_answer'].'</span> человек, <span class="bold">'.$two.'%</span> опрошенных</li> <li>. Ответили "'.$res[2]['titile'].'": <span class="bold">'.$resAllAnswer[2]['count_answer'].'</span> человек, <span class="bold">'.$three.'%</span> опрошенных</li> </ol> </div>'; echo '<br><br>'; $left1 = ($oneItem / 2) - 2; $left2 = ($two / 2) - 2; $left3 = ($three / 2) - 2; echo '<div> <p style="font-weight: bold;">Ответ "'.$res[0]['titile'].'"</p> <p class="gradient" style="position: relative; background: linear-gradient(to right, #1e5799 '.$oneItem.'%, #f5f5f5 '.$oneItem.'%);"><span style="position: absolute; top: 0; left:'.$left1.'%; color: white;">'.$oneItem.'%</span></p> </div>'; echo '<div> <p style="font-weight: bold;">Ответ "'.$res[1]['titile'].'"</p> <p class="gradient" style="position: relative; background: linear-gradient(to right, #1e5799 '.$two.'%, #f5f5f5 '.$two.'%);"><span style="position: absolute; top: 0; left:'.$left2.'%; color: white;">'.$two.'%</span></p> </div>'; echo '<div> <p style="font-weight: bold;">Ответ "'.$res[2]['titile'].'"</p> <p class="gradient" style="position: relative; background: linear-gradient(to right, #1e5799 '.$three.'%, #f5f5f5 '.$three.'%);"><span style="position: absolute; top: 0; left:'.$left3.'%; color: white;">'.$three.'%</span></p> </div>'; } /* 1. 130 человек = 100% 2. 130/100=1,3 (один процент) 3. 60/1,3 = 46% за первый пункт 4. 30/1,3 = 23% за второй пункт 5. 40/1,3 = 31% за третий пункт. 1. 34 человека = 100% 2. 34/100 = 0.34 3. 3/0.34 = 0 4. 2/0.34 = 100 5. 0/0.34 = 0 */ ?> </div> </body> </html> style.css .main { width: 620px; margin: 0 auto; padding: 10px; } ol { counter-reset: myCounter; padding: 0; } li { list-style: none; } li:before { counter-increment: myCounter; content:counter(myCounter); color: #31708f; font-weight: bold; } .result { background: #d9edf7; border-radius: 5px; padding: 15px; color: #31708f; } .btn-answer { color: white; background: #398439; border-radius: 5px; width: 100%; border: 1px solid #255625; padding: 6px 12px; } .bold { font-weight: bold; } .gradient { width: 100%; height: 20px; background: #337AB7; border-radius: 5px; box-shadow: inset 0 1px 2px rgba(0, 0, 0, .1); } база Код:
-- phpMyAdmin SQL Dump |
Цитата:
|
Часовой пояс GMT +3, время: 13:33. |