-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
179 lines (148 loc) · 5.05 KB
/
index.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
CC BY SA 3.0 https://creativecommons.org/licenses/by-sa/3.0/
Contributors:
* Svante Svenson: https://stackoverflow.com/a/6298066/3161534
* Nicolas Form
*/
// Count the visitors
$host_name = '';
$database = '';
$user_name = '';
$password = '';
$dbConnection = new mysqli($host_name, $user_name, $password, $database)
or die('{ "error":"1", "message": "Impossible to connect : ' . $dbConnection->connect_error . '" }');
$stmt = $dbConnection->prepare("INSERT INTO `htmltable_visitors`(`date`) VALUES (CURRENT_TIMESTAMP)")
or die('Failed PREPARE request : ' . $dbConnection->error);
$stmt->execute()
or die('Failed EXECUTE request : ' . $stmt->error);
$stmt->close();
?><!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>HTML table swapper</title>
<style>
body {
font-family: "Source Code Pro";
text-align: center;
background-color: #d6eeee;
color: #ffffffcf;
}
#container {
background-color: #3c4057;
border-radius: 30px;
padding: 20px;
max-width: 700px;
margin: 10px auto;
}
#mainInput {
width: 90%;
height: 300px;
padding: 10px;
}
#result {
width: 90%;
height: 300px;
padding: 10px;
margin: auto;
text-align: left;
white-space: break-spaces;
font-weight: bold;
}
.preview {
width: 90%;
padding: 10px;
overflow: auto;
}
.preview td, .preview th {
border: solid 1px;
}
.error {
color: red;
}
#footer {
color: #444444;
font-size: 10px;
margin: auto;
padding: 10px;
max-width: 700px;
}
</style>
</head>
<body>
<div id="container">
<h1>HTML table swapper</h1>
<div>
<p>Copy/paste your HTML table below to instantly get its equivalent but with the row and columns swapped.</p>
<p><textarea type="text" id="mainInput" onkeyup="checkTable()" placeholder="Copy/paste your <table> here"></textarea></p>
<p id="mainInputPreview" class="preview"></p>
<p id="resultPreview" class="preview"></p>
<p><textarea type="text" id="result" placeholder="The inverted <table> will be here"></textarea></p>
</div>
</div>
<div id="container">
<h2>Why this website?</h2>
<p>When you write a table in HTML, sometimes you would like to try it with the row and columns inverted, like with the transpose of a matrix. There is various techniques to do it in CSS, but what if you want to do it in pure HTML? Well you have to go cell by cell and swap them manually. Pretty fastidious if you have a big table...</p>
<p>That's why I created this website! In a few clicks you can get an HTML table identical to your original table, but with the row and columns inverted.</p>
</div>
<p id="footer">Made by <a href="https://www.feelouttheform.net/" target="_blank">Nicolas Form</a> under <a href="https://creativecommons.org/licenses/by-sa/3.0/" target="_blank">CC BY SA 3.0</a> (<a href="https://github.com/nicolasform/htmltableswapper/" target="_blank">see source here</a>). This website does not put any tracker or cookie in your browser and does not collect what you put in the text box.</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function prettyTable(tableCode) {
return tableCode
.replace(/[ \t\n\r]*</gi,"<") // Remove all spaces and carriage return
.replace(/<tr>/gi,"\n <tr>")
.replace(/<\/tr>/gi,"\n </tr>")
.replace(/<th>/gi,"\n <th>")
.replace(/<td>/gi,"\n <td>")
;
}
// Built on: https://stackoverflow.com/a/6298066/3161534
function checkTable() {
if($("#mainInput").val() === "") {
$("#result").val("").removeClass("error");
$("#mainInputPreview").html("");
$("#resultPreview").html("");
} else {
try {
let table = $($("#mainInput").val());
if(table.prop("tagName") == "TABLE") {
table.each(function() {
var $this = $(this);
var newrows = [];
$this.find("tr").each(function(){
var i = 0;
$(this).find("td,th").each(function(){
i++;
if(newrows[i] === undefined) { newrows[i] = $("<tr></tr>"); }
newrows[i].append($(this));
});
});
$this.find("tr").remove();
$.each(newrows, function(){
$this.append(this);
});
});
// Display result
$("#mainInputPreview").html($("#mainInput").val());
$("#result").val(prettyTable(table.prop('outerHTML'))).removeClass("error");
$("#resultPreview").html($("#result").val());
} else {
$("#result").val("Your table should be a <table> element.").addClass("error");
$("#mainInputPreview").html("");
$("#resultPreview").html("");
}
} catch(e) {
$("#result").val("Your <table> is malformed.").addClass("error");
$("#mainInputPreview").html("");
$("#resultPreview").html("");
}
}
}
// Run once in case the input is pre-filled
checkTable();
</script>
</body>
</html>