4
$content = "search/search.html.php";
5
$pagetitle.=" - Sökresultat";
7
// function getColNames($tableName, $pdo) {
9
// foreach ($pdo->query("SHOW COLUMNS FROM " . $tableName . ";") as $columnInfo) {
10
// array_push($columns, $columnInfo['Field']);
15
function searchTable($tableToSearch, $searchString, $pdo) {
16
$columns = getColNames($tableToSearch, $pdo);
17
$searchWords = explode(" ", $searchString);
19
$querystring = "SELECT * FROM " . $tableToSearch . " WHERE ";
20
foreach ($columns as $colName) {
22
foreach ($searchWords as $word) {
23
$querystring.=" " . $colName . " LIKE :SSTRING" . $counter . " OR";
26
$querystring = substr($querystring, 0, strlen($querystring) - 2);
27
$querystring.=") OR ";
29
$querystring = substr($querystring, 0, strlen($querystring) - 3);
31
$stmt = $pdo->prepare($querystring);
34
for ($i = 0; $i <= $counter - 1; $i++) {
36
$sstring = "%" . $searchWords[$j] . "%";
37
$stmt->bindValue(':SSTRING' . $i, $sstring);
39
if ($j >= sizeof($searchWords)) {
46
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
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."^");
60
foreach($identifyingColumns as $id){
64
echo "<table class='dataTable'>";
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>";
72
// foreach ($columnNames as $colName) {
73
// echo "<th>" . $colName . "</th>";
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] . "' />";
83
foreach($hiddenFields as $name=>$value){
84
echo "<input type='hidden' name='".$name."' value='" . $value . "' />";
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);
104
echo "No matches for the search string";
111
if (!empty($_POST['searchString'])) {
113
// $queryString="SHOW TABLES;";
114
// $stmt = $pdo->prepare($queryString);
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));
125
$AssignedQuizzesResult = searchTable("AssignedQuizzes", $_POST['searchString'], $pdo);
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);
133
// if (isset($_SESSION['userType']) && $_SESSION['userType'] == "admin") {
134
// $usersResult = searchTable("anvandare", $_POST['searchString'], $pdo);
137
$errorMsg = "No search string entered";
b'\\ No newline at end of file'