/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
1 by Henrik G.
First seed of Lenasys ... Needs to be Organized Further
1
<?php
2
///In development...
3
4
$content = "search/search.html.php";
5
$pagetitle.=" - Sökresultat";
6
7
// function getColNames($tableName, $pdo) {
8
    // $columns = array();
9
    // foreach ($pdo->query("SHOW COLUMNS FROM " . $tableName . ";") as $columnInfo) {
10
        // array_push($columns, $columnInfo['Field']);
11
    // }
12
    // return $columns;
13
// }
14
15
function searchTable($tableToSearch, $searchString, $pdo) {
16
    $columns = getColNames($tableToSearch, $pdo);
17
    $searchWords = explode(" ", $searchString);
18
    $counter = 0;
19
    $querystring = "SELECT * FROM " . $tableToSearch . " WHERE ";
20
    foreach ($columns as $colName) {
21
        $querystring.="(";
22
        foreach ($searchWords as $word) {
23
            $querystring.=" " . $colName . " LIKE :SSTRING" . $counter . " OR";
24
            $counter++;
25
        }
26
        $querystring = substr($querystring, 0, strlen($querystring) - 2);
27
        $querystring.=") OR ";
28
    }
29
    $querystring = substr($querystring, 0, strlen($querystring) - 3);
30
    $querystring.=";";
31
    $stmt = $pdo->prepare($querystring);
32
33
    $j = 0;
34
    for ($i = 0; $i <= $counter - 1; $i++) {
35
36
        $sstring = "%" . $searchWords[$j] . "%";
37
        $stmt->bindValue(':SSTRING' . $i, $sstring);
38
        $j++;
39
        if ($j >= sizeof($searchWords)) {
40
            $j = 0;
41
        }
42
    }
43
44
    $stmt->execute();
45
46
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
47
    return $result;
48
}
49
50
function displaySearchResult($tableData, $tableCaption, $identifyingColumns, $hiddenFields, $searchString=null) {
51
    if (isset($searchString)) {
52
        $searchWords = explode(" ", $searchString);
53
        $replaceWords = array();
54
        foreach ($searchWords as $sWord) {
55
            array_push($replaceWords,"~".$sWord."^");
56
        }
57
    }
58
59
	$idString="";
60
	foreach($identifyingColumns as $id){
61
		$idString.=$id;
62
	}
63
	
64
    echo "<table class='dataTable'>";
65
    echo "<thead>";
66
    echo "<caption>" . $tableCaption . "</caption>";
67
    if (sizeof($tableData) > 0) {
68
        echo "<tr class='headerRow'>";
69
        foreach($studentList[0] as $columnName=>$data){
70
				echo "<th>".$columnName."</th>";
71
		}
72
		// foreach ($columnNames as $colName) {
73
            // echo "<th>" . $colName . "</th>";
74
        // }
75
        echo "</tr>";
76
        echo "</thead>";
77
        foreach ($tableData as $row) {
78
            echo "<tr class='dataRow'>";
79
            echo "<form name='" . $tableCaption . $idString . "' action='.' method='post' >";
80
            foreach($identifyingColumns as $name){
81
				echo "<input type='hidden' name='".$name."' value='" . $row[$name] . "' />";
82
			}
83
			foreach($hiddenFields as $name=>$value){
84
				echo "<input type='hidden' name='".$name."' value='" . $value . "' />";
85
			}
86
            foreach ($row as $value) {
87
                echo "<td onclick='document[\"" . $tableCaption . $idString . "\"].submit();return false;'>"; // . htmlsafe($value) . "</td>";
88
                if (isset($searchWords)) {
89
                   $value=str_replace($searchWords, $replaceWords, htmlsafe($value));
90
                   $value=str_replace("~","<strong>",$value);
91
                   echo str_replace("^","</strong>",$value);
92
                } else {
93
                    output($value);
94
                }
95
                echo "</td>";
96
            }
97
            echo "</form>";
98
            echo "</tr>";
99
        }
100
    } else {
101
        echo "</thead>";
102
        echo "<tr>";
103
        echo "<td>";
104
        echo "No matches for the search string";
105
        echo "</td>";
106
        echo "</tr>";
107
    }
108
    echo "</table>";
109
}
110
111
if (!empty($_POST['searchString'])) {
112
113
    // $queryString="SHOW TABLES;";
114
	// $stmt = $pdo->prepare($queryString);
115
	// $stmt->execute();
116
    // $tableNames = $stmt->fetchAll(PDO::FETCH_ASSOC);
117
	// $resultsArray=array();
118
	// foreach($tableNames as $table){
119
		// foreach($table as $tableName){
120
			// $result = searchTable($tableName, $_POST['searchString'], $pdo);
121
			// array_push($resultsArray,array($tableName=>$result));
122
		// }
123
	// }
124
	
125
	$AssignedQuizzesResult = searchTable("AssignedQuizzes", $_POST['searchString'], $pdo);
126
	
127
	// $aliensResult = searchTable("AllaAliens", $_POST['searchString'], $pdo);
128
    // $incidentsResult = searchTable("incident", $_POST['searchString'], $pdo);
129
    // $shipsResult = searchTable("skepp", $_POST['searchString'], $pdo);
130
    // $speciesResult = searchTable("AllaRaser", $_POST['searchString'], $pdo);
131
    // $weaponsResult = searchTable("vapen", $_POST['searchString'], $pdo);
132
133
    // if (isset($_SESSION['userType']) && $_SESSION['userType'] == "admin") {
134
        // $usersResult = searchTable("anvandare", $_POST['searchString'], $pdo);
135
    // }
136
} else {
137
    $errorMsg = "No search string entered";
138
}
139
?>