如何使用PHP開發簡單的線上問捲和資料分析功能
#引言:
隨著網路的普及,越來越多的企業和個人開始關注問捲和數據分析。而使用PHP作為開發工具,可以快速實現線上問捲和資料分析功能。本篇文章將向大家介紹如何使用PHP開發簡單的線上問捲和資料分析功能,並給出具體的程式碼範例。
一、需求分析
在開始開發之前,我們需要先明確需求,確定我們的系統功能應該具備哪些面向的功能。
具體程式碼實作:
建立問卷
程式碼範例:
<form action="create_survey.php" method="POST"> <label for="title">问卷标题:</label> <input type="text" name="title"><br> <label for="question1">问题1:</label> <input type="text" name="question1"><br> <input type="submit" value="创建问卷"> </form>
create_survey.php檔案:
<?php $title = $_POST['title']; $question1 = $_POST['question1']; // 连接数据库 $dsn = 'mysql:host=localhost;dbname=survey'; $username = 'root'; $password = ''; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 插入数据 $sql = "INSERT INTO surveys (title, question1) VALUES (:title, :question1)"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':title', $title); $stmt->bindParam(':question1', $question1); $stmt->execute(); echo "调查问卷创建成功!"; } catch (PDOException $e) { die("连接数据库失败: " . $e->getMessage()); } ?>
發布問卷
程式碼範例:
<a href="survey.php?id=1">点击参与问卷调查</a>
survey.php檔案:
<?php // 获取调查问卷ID $id = $_GET['id']; // 根据ID查询调查问卷标题和问题 $dsn = 'mysql:host=localhost;dbname=survey'; $username = 'root'; $password = ''; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 查询数据 $sql = "SELECT * FROM surveys WHERE id = :id"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':id', $id); $stmt->execute(); $survey = $stmt->fetch(); if ($survey) { echo "<h1>{$survey['title']}</h1>"; echo "<h2>问题1:{$survey['question1']}</h2>"; echo "<form action='submit_survey.php' method='POST'>"; echo "<input type='hidden' name='id' value='$id'>"; echo "<label for='answer1'>您的回答:</label>"; echo "<input type='text' name='answer1'><br>"; echo "<input type='submit' value='提交'>"; echo "</form>"; } else { echo "调查问卷不存在!"; } } catch (PDOException $e) { die("连接数据库失败: " . $e->getMessage()); } ?>
submit_survey.php檔案:
<?php $id = $_POST['id']; $answer1 = $_POST['answer1']; $dsn = 'mysql:host=localhost;dbname=survey'; $username = 'root'; $password = ''; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO survey_responses (survey_id, answer1) VALUES (:survey_id, :answer1)"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':survey_id', $id); $stmt->bindParam(':answer1', $answer1); $stmt->execute(); echo "问卷提交成功!"; } catch (PDOException $e) { die("连接数据库失败: " . $e->getMessage()); } ?>
analysis.php檔案:
<?php $dsn = 'mysql:host=localhost;dbname=survey'; $username = 'root'; $password = ''; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 查询所有问卷的数据 $sql = "SELECT * FROM survey_responses"; $stmt = $pdo->query($sql); $responses = $stmt->fetchAll(); // 统计回答结果 $answer1_counts = array(); foreach ($responses as $response) { $answer1 = $response['answer1']; if (!isset($answer1_counts[$answer1])) { $answer1_counts[$answer1] = 0; } $answer1_counts[$answer1]++; } // 打印统计结果 echo "<h1>问卷数据分析结果</h1>"; echo "<h2>问题1:</h2>"; foreach ($answer1_counts as $answer1 => $count) { echo "<p>$answer1: $count</p>"; } } catch (PDOException $e) { die("连接数据库失败: " . $e->getMessage()); } ?>
使用PHP開發簡單的線上問捲和資料分析功能是十分容易的。本文提供了創建問卷、發布問卷、收集數據、數據分析和結果展示的程式碼範例,讀者可以根據這些範例程式碼進行二次開發,實現更完善的功能和效果。
以上是如何使用PHP開發簡單的線上問捲和數據分析功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!