View sourcecode

The following files exists in this folder. Click to view.

old_functions.php

164 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
// -------------------------------------------------------------------------------------------
//
// createCodeFromFile
// description:    Makes <code>-tags around every line of the given file
// inparam:        $filename, the name of the file (including directory)
// inparam:        $specialCharacter, true if htmlspecialchars() will be used. Used with php and html
// return:         String of code-tags  
//
function createCodeFromFile($fileName$specialCharacter){
    
$returnString "";
    
$file fopen($fileName"r");
    while(!
feof($file)){
        
$returnString  .= $specialCharacter "<code>".htmlspecialchars(fgets($file))."</code>" "<code>".fgets($file)."</code>";
    }
    
fclose($file);
    return 
$returnString;
}


// -------------------------------------------------------------------------------------------
//
// createCodeFromArray
// description:    Makes <code>-tags around every component of the given array
// inparam:        $arr, the array
// return:         String of code-tags  
//
function createCodeFromArray($arr){
    
$returnString "";
    for(
$i 0$i <  count($arr); $i++) {
        
$returnString .= "<code>".$arr[$i]."</code>\n";
    }
    return 
$returnString;
}


// -------------------------------------------------------------------------------------------
//
// createTableFromSql
// description:    Makes a table of result from a sql script
// inparam:        $arr, array of attribute to print head of table
// inparam:        $sql, the sql script
// return:         String of table content  
//
function createTableFromSql($arr$sql){
    
// Läser in databaskopplingen, require eftersom det måste göras igen!
    
require('dbConn.php');

    
$returnString "<table class=\"utskrift\">
      <tr>"
;

    
# creates head of table
    
for($i 0$i <  count($arr); $i++) {
        
$returnString .= "<th>".$arr[$i]."</th>\n";
    }
    
$returnString .= "</tr>";

    
# gets result set an makes body of the table
    
$stmt $pdo->query($sql);
    
$result $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach (
$result as $row) {
        
$returnString .= "<tr>";
        foreach (
$arr as $i){
            
$returnString  .= $row[$i]!="" "<td>{$row[$i]}</td>\n" "<td><i>null</i></td>";
        }
        
$returnString .= "</tr>";
    }

    
$returnString .= "</table>";
    return 
$returnString;
}


// -------------------------------------------------------------------------------------------
//
// createTableFromSqlEdit
// description:    Makes a table of result from a sql script
// inparam:        $arr, array of attribute to print head of table
// inparam:        $sql, the sql script
// inparam:        $table, name of table to link edit and delete
// return:         String of table content  
//
function createTableFromSqlEdit($arr$sql$table){
    
// Läser in databaskopplingen, require eftersom det måste göras igen!
    
require('dbConn.php');

    
$returnString "<table class=\"utskrift\">
      <tr>"
;

    
# Skapar tabellhuvudet
    
for($i 0$i <  count($arr); $i++) {
        
$returnString .= "<th>".$arr[$i]."</th>\n";
    }
    
$returnString .= "<th>delete</th>";    
    
$returnString .= "</tr>";

    
# Hämtar resultset och placerar ut det i tabellen
    
$stmt $pdo->query($sql);
    
$result $stmt->fetchAll(PDO::FETCH_ASSOC);

    
# Special, hämta id och skriv ut det i första kolumnen och sista som länkar.
    
foreach ($result as $row) {
        
$id = -1;
        
$returnString .= "<tr>";
        foreach (
$arr as $i){
            if(
$id == -1){
                
$id $row[$i];
                
$returnString  .= "<td><a href='edit.php?table={$table}&id={$id}'>{$row[$i]}</a></td>";
            } else {
                
$returnString  .= $row[$i]!="" "<td>{$row[$i]}</td>\n" "<td><i>null</i></td>";
            }
        }
        
$returnString .= "<td><a href='edit.php?table={$table}&action=delete&id={$id}'>X</a></td>";        
        
$returnString .= "</tr>";
    }

    
$returnString .= "</table>";
    return 
$returnString;
}

// -------------------------------------------------------------------------------------------
//
// executeSQLFile
// description:    Run all sql-scripts from a file to the database
// inparam:        $fileName, name of sql-file to run
// inparam:        $urlRedirect, the page to redirect
// return:         nothing  
//
function executeSQLFile($fileName$urlRedirect){
    
// Läser in databaskopplingen, require eftersom det måste göras igen!
    
require('dbConn.php');
    
$mess "";

       try
       {
        
$sql file_get_contents($fileName);
        
$qr $pdo->exec($sql);                # 2 är bra lokalt, 0 är bra på Binero
        
$mess "Databasen återställd!";
        
# Kolla över detta, http://php.net/manual/en/pdo.exec.php
        # $qr skall ge värdet för antal rader, kan man jobba med $db->errorInfo() för 


        // Errorinfo
        // ok: Array ( [0] => 00000 [1] => [2] => )

        # print_r($pdo->errorInfo());
        #$mess = $pdo->errorInfo();
/*
         if($qr == 2){
             $mess = "Databasen återställd!";
         } else {
             $mess = "Något gick fel, databasen är ej återställd";
         }*/
    
}
    catch (
PDOException $e
    {
        
$mess =  "Connection failed: $e->getMessage()"    ;
    }
    
$urlRedirect .= "?mess=".$mess
    
header("location: $urlRedirect");
}
?>