浏览代码

Hämta ut kategorier + antal frågor

axel 5 年之前
父节点
当前提交
e71d2d82e4
共有 2 个文件被更改,包括 21 次插入6 次删除
  1. 10 2
      dbFiles/NewOnlineGame.php
  2. 11 4
      dbFiles/ReadQuestionsFromFile.php

+ 10 - 2
dbFiles/NewOnlineGame.php

@@ -73,8 +73,16 @@
 			echo $error . " from sql " . $playerSql;
 		}
 	} else if ($functionName === "GetCategories") {
-		$sql = "SELECT category.name, count(*) FROM questions INNER JOIN questionToCategory ON questions.id = questionToCategory.questionId INNER JOIN category ON questionToCategory.categoryId = category.id GROUP BY category.name"
-		
+		$sql = "SELECT category.name as name, count(*) as num FROM questions INNER JOIN questionToCategory ON questions.id = questionToCategory.questionId INNER JOIN category ON questionToCategory.categoryId = category.id GROUP BY category.name"
+		$result = $conn->query($sql);
+		$json = array();
+		while ($row = $result->fetch_assoc()) {
+			$json = array('name' => $row['name'],
+						'value' => $row['value']);
+			$jsonString = json_encode($json);
+
+			echo $jsonString;
+		}
 	}
 
 ?>

+ 11 - 4
dbFiles/ReadQuestionsFromFile.php

@@ -23,7 +23,7 @@ if (file_exists($argv[1])) {
 	
 	while (($data = fgetcsv($file, 1000, ",")) !== false) {
 		$answer = intval($data[0]);
-		$question = utf8_encode($data[1]);
+		$question = $data[1];
 	 
 		if ($answer == null || $question == null || $answer == "" || $question == "") {
 			echo "Failed to get answer and/or question from " . print_r($data,true) .  "\r\n";
@@ -35,8 +35,9 @@ if (file_exists($argv[1])) {
 		if ($result === FALSE) {
 			echo "Insert new question $question \r\n";
 		} else {
-			$questionId = $conn->query($sql)->fetch_object()->id;			
+			$questionId = $result->fetch_object()->id;
 		}
+$result->free_result();
 		
 		if($questionId == null) {
 			$sql = "INSERT INTO questions (answer, question) 
@@ -56,7 +57,13 @@ if (file_exists($argv[1])) {
 		}
 		
 		$sql = "SELECT id FROM category WHERE name = '$categoryName'";
-		$categoryId = $conn->query($sql)->fetch_object()->id;
+		$result = $conn->query($sql);
+		if ($result === FALSE) {
+			echo "Failed to get category, add New with name $categoryName";
+		} else {
+			$categoryId = $result->fetch_object()->id;
+		}
+$result->free_result();
 		
 		if ($categoryId == null) {
 			$sql = "INSERT INTO category (name, r,g,b,a) VALUES ($categoryName, 255,255,255,255)";
@@ -73,4 +80,4 @@ if (file_exists($argv[1])) {
  fclose($file);
 } else {
 	echo "Run with file as first argument";
-}
+}