Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 3b343aea

Přidáno uživatelem Tomáš Pašek před více než 3 roky(ů)

Feature #8268 Ovládací metody databáze

Zobrazit rozdíly:

.idea/.gitignore
1
# Default ignored files
2
/shelf/
3
/workspace.xml
4
# Datasource local storage ignored files
5
/../../../../../:\wamp64\www\aswi\.idea/dataSources/
6
/dataSources.local.xml
7
# Editor-based HTTP Client requests
8
/httpRequests/
.idea/aswi.iml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<module type="JAVA_MODULE" version="4">
3
  <component name="NewModuleRootManager" inherit-compiler-output="true">
4
    <exclude-output />
5
    <content url="file://$MODULE_DIR$" />
6
    <orderEntry type="inheritedJdk" />
7
    <orderEntry type="sourceFolder" forTests="false" />
8
  </component>
9
</module>
.idea/misc.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project version="4">
3
  <component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
4
    <output url="file://$PROJECT_DIR$/out" />
5
  </component>
6
</project>
.idea/modules.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project version="4">
3
  <component name="ProjectModuleManager">
4
    <modules>
5
      <module fileurl="file://$PROJECT_DIR$/.idea/aswi.iml" filepath="$PROJECT_DIR$/.idea/aswi.iml" />
6
    </modules>
7
  </component>
8
</project>
.idea/vcs.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project version="4">
3
  <component name="VcsDirectoryMappings">
4
    <mapping directory="" vcs="Git" />
5
  </component>
6
</project>
application/controller/TableController.php
1
<?php
2
require "../model/DB.php";
3
$DB = new DB();
4

  
5
// (B) SEARCH FOR USERS
6
$results = $DB->select(
7
    "SELECT * FROM dd_wordform limit 10"
8
);
9

  
10
// (C) OUTPUT RESULTS
11
$result = json_encode(count($results)==0 ? null : $results);
12
echo $result;
application/model/DB.php
1
<?php
2
class DB {
3
    // (A) CONNECT TO DATABASE
4
    public $error = "";
5
    private $pdo = null;
6
    private $stmt = null;
7
    function __construct () {
8
        try {
9
            $this->pdo = new PDO(
10
                "pgsql:host=".DB_HOST.";dbname=".DB_NAME,
11
                DB_USER, DB_PASSWORD, [
12
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
13
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
14
                ]
15
            );
16
        } catch (Exception $ex) { die($ex->getMessage()); }
17
    }
18

  
19
    // (B) CLOSE CONNECTION
20
    function __destruct(){
21
        if ($this->stmt!==null) { $this->stmt = null; }
22
        if ($this->pdo!==null) { $this->pdo = null; }
23
    }
24

  
25
    // (C) RUN A SELECT QUERY
26
    function select($sql, $cond=null){
27
        $result = false;
28
        try {
29
            $this->stmt = $this->pdo->prepare($sql);
30
            $this->stmt->execute($cond);
31
            $result = $this->stmt->fetchAll();
32
            return $result;
33
        } catch (Exception $ex) {
34
            $this->error = $ex->getMessage();
35
            return false;
36
        }
37
    }
38
}
39

  
40
// (D) DATABASE SETTINGS - CHANGE TO YOUR OWN!
41
define('DB_HOST', 'localhost');
42
define('DB_NAME', 'dalimil1');
43
define('DB_CHARSET', 'utf8');
44
define('DB_USER', 'postgres');
45
define('DB_PASSWORD', 'a');
application/view/Index.php
1
<!-- TESTOVACI INDEX-->
2
<?
3
include "../controller/TableController.php"
4
?>
5
<script>
6
    function doSearch () {
7
        // (A1) GET SEARCH TERM
8
        var data = new FormData();
9

  
10
        // (A2) AJAX - USE HTTP:// NOT FILE://
11
        var xhr = new XMLHttpRequest();
12
        xhr.open("POST", "../controller/TableController.php");
13
        xhr.onload = function(){
14
            let results = document.getElementById("results"),
15
                search = this.response;
16
            results.innerHTML = "";
17
            console.log(search);
18
            let parsedJSON = JSON.parse(search);
19
            console.log(parsedJSON);
20
            if (parsedJSON != null) { for (let s of parsedJSON) {
21
                results.innerHTML += "<div>" + s.id + "</div>";
22
            }}
23
        };
24
        xhr.send(data);
25
        return false;
26
    }
27
</script>
28

  
29
<form onsubmit="return doSearch()">
30
    <input type="submit" value="Search"/>
31
</form>
32

  
33
<div id="results"></div>

Také k dispozici: Unified diff