From 99c59cf977e563a17c962970a7c7d86fe8b316f3 Mon Sep 17 00:00:00 2001 From: Peter Edmond Date: Sat, 31 May 2025 01:53:14 +0100 Subject: [PATCH] Ensuring that bar graphs are cleared before redrawing --- drawbar.js | 3 +++ getQuestionsData.php | 61 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/drawbar.js b/drawbar.js index 2d584ac..cdb9443 100644 --- a/drawbar.js +++ b/drawbar.js @@ -37,6 +37,9 @@ const width = 600; //+svg.attr("width"); const height = 200; //+svg.attr("height"); const margin = { top: 40, right: 30, bottom: 60, left: 30 }; +//First clean the svg: +svg.selectAll("*").remove(); + // Draw the white background // Draw the rounded rectangle svg.append("rect") diff --git a/getQuestionsData.php b/getQuestionsData.php index 7fed309..1bdcf94 100644 --- a/getQuestionsData.php +++ b/getQuestionsData.php @@ -1,4 +1,21 @@ prepare("INSERT INTO Filters (surveyId, FilterQuestion, FilterOption, Text) VALUES (:survey, :question, :option, :text)"); + + $stmt->execute([ + ':survey' => $surveyId, + ':question' => $question, + ':option' => $option, + ':text' => $text + ]); + +} + + + // Load config $config = require 'config.php'; $apiToken = $config['api_token']; @@ -42,26 +59,54 @@ curl_close($ch); $data = json_decode($response, true); +// Database connection (PDO) +$pdo = new PDO("mysql:host=localhost;dbname={$config['db_name']};charset=utf8mb4", $config['db_user'], $config['db_pass']); + +//Check if the survey data has previously been inserted: +$checkSql = "SELECT 1 FROM Filters WHERE surveyId = :surveyId LIMIT 1"; + +$stmt = $pdo->prepare($checkSql); +$stmt->execute([ + ':surveyId' => $surveyId +]); + +if ($stmt->fetch()) { + echo "Site already exists. Skipping insert.\n"; + return; +} + + if (isset($data['result']['elements']) && is_array($data['result']['elements'])) { foreach ($data['result']['elements'] as $element) { if (isset($element['QuestionID'])) { - echo "QuestionID: " . $element['QuestionID'] . "
". PHP_EOL; - + if (isset($element['QuestionID']) && $element['QuestionID'] === "QID70") { //echo json_encode($element, JSON_PRETTY_PRINT) . PHP_EOL; } if (isset($element['QuestionID']) && $element['QuestionID'] === "QID68") { - echo json_encode($element['QuestionDescription'], JSON_PRETTY_PRINT) . PHP_EOL; - echo json_encode($element['Choices'], JSON_PRETTY_PRINT) . PHP_EOL; + echo "Survey: {$surveyId}, QuestionID: {$element['QuestionID']} : Description: {$element['QuestionDescription']} : 0 ". PHP_EOL; + insertOption($surveyId, $element['QuestionID'], 0, $element['QuestionDescription'], $pdo); + foreach ($element['Choices'] as $index => $choice) { + insertOption($surveyId, $element['QuestionID'], $index, $choice['Display'], $pdo); + echo "ID: $index, Display: " . $choice['Display'] . "
"; + } } if (isset($element['QuestionID']) && $element['QuestionID'] === "QID69") { - echo json_encode($element['QuestionDescription'], JSON_PRETTY_PRINT) . PHP_EOL; - echo json_encode($element['Choices'], JSON_PRETTY_PRINT) . PHP_EOL; + echo "Survey: {$surveyId}, QuestionID: {$element['QuestionID']} : Description: {$element['QuestionDescription']} : 0 ". PHP_EOL; + insertOption($surveyId, $element['QuestionID'], 0, $element['QuestionDescription'], $pdo); + foreach ($element['Choices'] as $index => $choice) { + insertOption($surveyId, $element['QuestionID'], $index, $choice['Display'], $pdo); + echo "ID: $index, Display: " . $choice['Display'] . "
"; + } } if (isset($element['QuestionID']) && $element['QuestionID'] === "QID70") { - echo json_encode($element['QuestionDescription'], JSON_PRETTY_PRINT) . PHP_EOL; - echo json_encode($element['Choices'], JSON_PRETTY_PRINT) . PHP_EOL; + echo "Survey: {$surveyId}, QuestionID: {$element['QuestionID']} : Description: {$element['QuestionDescription']} : 0 ". PHP_EOL; + insertOption($surveyId, $element['QuestionID'], 0, $element['QuestionDescription'], $pdo); + foreach ($element['Choices'] as $index => $choice) { + insertOption($surveyId, $element['QuestionID'], $index, $choice['Display'], $pdo); + echo "ID: $index, Display: " . $choice['Display'] . "
"; + } } } }