Browse Source

Fixes 'n bugs (in that order)

Brendan Abolivier 9 years ago
parent
commit
73f8996336
7 changed files with 91 additions and 40 deletions
  1. 11
    4
      web/index.php
  2. 32
    12
      web/model/class.connector.php
  3. 26
    8
      web/model/class.score.php
  4. 1
    0
      web/model/classes.php
  5. 9
    0
      web/tests.php
  6. 11
    14
      web/view/palmares.php
  7. 1
    2
      web/view/questions.php

+ 11
- 4
web/index.php View File

@@ -2,7 +2,14 @@
2 2
 
3 3
 require_once('model/classes.php');
4 4
 
5
-header("Content-Type:application/json");
6
-
7
-echo json_encode(array("Message" => "Hello world;"));
8
-
5
+//header("Content-Type:application/json");
6
+
7
+if(!empty($_GET['page'])) {
8
+  if($_GET['page'] == "palmares") {
9
+    include("view/palmares.php");
10
+    echo json_encode($scArray);
11
+  }
12
+} else {
13
+  include("view/questions.php");
14
+	echo json_encode($categories);
15
+}

+ 32
- 12
web/model/class.connector.php View File

@@ -41,11 +41,7 @@ class Connector {
41 41
 					$whereClause .= $array[0]." ".$array[1]." :".$array[0]." AND ";
42 42
 					$arrayVerif[":".$array[0]] = $array[2];
43 43
 				}
44
-				if($substring = substr($whereClause, 0, -5)) {
45
-					$request .= $substring;
46
-				} else {
47
-					throw new Exception('Problème lors de la création du substring');
48
-				}
44
+				$request .= substr($whereClause, 0, -5);
49 45
 			} else if(($upName = strtoupper($name)) == "ORDER BY") {
50 46
 				if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
51 47
 					throw new Exception('Nombre de paramètres incorrects (ORDER BY). Les paramètres passés sont : '
@@ -80,14 +76,38 @@ class Connector {
80 76
 
81 77
 	function Insert($table, $values) {
82 78
 		$request = "INSERT INTO $table(";
83
-		$values = "VALUES(";
84
-		$params = array();
85
-		foreach($values as $name=>$values) {
86
-			$request += $name.",";
87
-			$values += ":".$name.",";
79
+		$valeurs = "VALUES(";
80
+		$arrayVerif = array();
81
+		foreach($values as $name=>$value) {
82
+			$request .= $name.",";
83
+			$valeurs .= "?,";
84
+				array_push($arrayVerif, $value);
85
+		}
86
+
87
+		$request = substr($request, 0, -1).") ".substr($valeurs, 0, -1).")";
88
+
89
+		$stmt = $this->bdd->prepare($request);
90
+
91
+		$stmt->execute($arrayVerif);
92
+	}
93
+
94
+	function Update($table, $update) {
95
+		$request = "UPDATE $table SET ";
96
+		$arrayVerif = array();
97
+		foreach($update['set'] as $name=>$value) {
98
+			$request .= $name."=?,";
99
+			array_push($arrayVerif, $value);
88 100
 		}
89
-		$request = substr($request, 0, -1).") ".substr($values, 0, -1).")";
90
-		echo $request;
101
+		$request = substr($request, 0, -1)." WHERE ";
102
+		var_dump($request);
103
+		foreach($update['where'] as $value) {
104
+			$request .= $value[0].$value[1]."? AND ";
105
+			array_push($arrayVerif, $value[2]);			
106
+		}
107
+		$request = substr($request, 0, -5);
108
+
109
+		$stmt = $this->bdd->prepare($request);
110
+		$stmt->execute($arrayVerif);
91 111
 	}
92 112
 
93 113
 	function beginTransaction() {

+ 26
- 8
web/model/class.score.php View File

@@ -10,13 +10,13 @@ class Score {
10 10
 
11 11
     $options = array(
12 12
       "where" => array(
13
-        array("login", "=", $login);
13
+        array("login", "=", $login)
14 14
       )
15 15
     );
16 16
 
17
-    $data = $bdd->Select("*", "scores", $options);
18
-    $this->login = $data['login'];
19
-    $this->score = $data['score'];
17
+    $data = $this->bdd->Select("*", "scores", $options);
18
+    $this->login = $data[0]['login'];
19
+    $this->score = $data[0]['score'];
20 20
   }
21 21
 
22 22
   function getLogin() {
@@ -47,11 +47,29 @@ class Score {
47 47
   public static function add($login, $score) {
48 48
     $bdd = new Connector();
49 49
 
50
-    $values = array(
51
-      "login" => $login,
52
-      "score" => $score
50
+    $options = array(
51
+      "where" => array(
52
+        array("login", "=", $login)
53
+      )
53 54
     );
54 55
 
55
-    $bdd->Insert("score", $values);
56
+    if(!$scores = $bdd->Select("*", "scores", $options)) {
57
+      $values = array(
58
+        "login" => $login,
59
+        "score" => $score
60
+      );
61
+
62
+      $bdd->Insert("scores", $values);
63
+    } else {
64
+      if($score > $scores[0]['score']) {
65
+        $update = array(
66
+          "where" => array(
67
+            array("login", "=", $login)
68
+          ),
69
+          "set" => array("score" => $score)
70
+        );
71
+        $bdd->Update("scores", $update);
72
+      }
73
+    }
56 74
   }
57 75
 }

+ 1
- 0
web/model/classes.php View File

@@ -5,3 +5,4 @@ require_once('model/class.connector.php');
5 5
 require_once('model/class.question.php');
6 6
 require_once('model/class.questset.php');
7 7
 require_once('model/class.categorie.php');
8
+require_once('model/class.score.php');

+ 9
- 0
web/tests.php View File

@@ -0,0 +1,9 @@
1
+<?php
2
+
3
+require_once('model/classes.php');
4
+
5
+try {
6
+	Score::add("Yolo", 200);
7
+}catch(Exception $e) {
8
+	echo("Erreur : ".$e->getMessage());
9
+}

+ 11
- 14
web/view/palmares.php View File

@@ -1,18 +1,15 @@
1 1
 <?php
2 2
 
3
-require_once('../model/classes.php');
4
-
5
-$bdd = new Connector();
6
-
7
-$options = array(
8
-	"order by" => array("score", "desc"),
9
-	"limit" => array(10)
10
-);
11
-
12 3
 try {
13
-	$res = $bdd->Select("*", "scores", $options);
14
-} catch(Exception $e) {
15
-	echo("Erreur : ".$e->getMessage());
4
+	$scores = Score::getScores(10);
5
+	$scArray = array();
6
+	for($i = 0; $i < sizeof($scores); $i++) {
7
+		array_push($scArray, array(
8
+			"login" => $scores[$i]->getLogin(),
9
+			"score" => $scores[$i]->getScore()
10
+		));
11
+		echo json_encode($scArray);
12
+	}
13
+}catch(Exception $e) {
14
+	echo json_encode(array("erreur" => $e->getMessage()));
16 15
 }
17
-
18
-// $res contient les résultats

web/questions.php → web/view/questions.php View File

@@ -11,7 +11,6 @@ try{
11 11
 	foreach($categoriesObj as $categorie) {
12 12
 		array_push($categories, $categorie->getArray());
13 13
 	}
14
-	echo json_encode($categories);
15 14
 }catch(Exception $e) {
16
-	echo("<strong>Erreur : </strong>".$e->getMessage());
15
+	echo json_encode(array("erreur" => $e->getMessage()));
17 16
 }