Store Message
This commit is contained in:
parent
b0a41b7966
commit
7f02744efe
18
index.php
18
index.php
|
@ -1,10 +1,26 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
<?php
|
||||||
|
require_once('persistence.php');
|
||||||
|
$db = createDB();
|
||||||
|
?>
|
||||||
<html lang="ca">
|
<html lang="ca">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<title>Guestbook</title>
|
<title>Guestbook</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
Test
|
<table border="1">
|
||||||
|
<?php
|
||||||
|
//storeMessage($db, 'Hola Dendy');
|
||||||
|
$test = getMessages($db);
|
||||||
|
foreach($test as $msg) {
|
||||||
|
echo('<tr>');
|
||||||
|
foreach($msg as $key => $value) {
|
||||||
|
echo("<td>$key: $value</td>");
|
||||||
|
}
|
||||||
|
echo('</tr>');
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</table>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
$db = createDB();
|
|
||||||
$test = getMessages($db, ['id_user' => 1]);
|
|
||||||
var_dump($test);
|
|
||||||
|
|
||||||
function createDB($db_name = 'guestbook.db'): SQLite3 {
|
function createDB($db_name = 'guestbook.db'): SQLite3 {
|
||||||
// Can't create table if it already exists
|
// Can't create table if it already exists
|
||||||
$exists = file_exists($db_name);
|
$exists = file_exists($db_name);
|
||||||
|
@ -46,7 +42,7 @@ function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
|
||||||
|
|
||||||
// Prepare statement
|
// Prepare statement
|
||||||
$query = 'SELECT * FROM message' . $append_string;
|
$query = 'SELECT * FROM message' . $append_string;
|
||||||
var_dump($query);
|
//var_dump($query);
|
||||||
|
|
||||||
$statement = $db->prepare($query);
|
$statement = $db->prepare($query);
|
||||||
|
|
||||||
|
@ -54,6 +50,28 @@ function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
|
||||||
$statement->bindParam($key, $value);
|
$statement->bindParam($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
var_dump($statement);
|
$result = $statement->execute();
|
||||||
return $statement->execute()->fetchArray(SQLITE3_ASSOC);
|
|
||||||
|
$notnull = true;
|
||||||
|
$ret = [];
|
||||||
|
while($notnull) {
|
||||||
|
$arr = $result->fetchArray(SQLITE3_ASSOC);
|
||||||
|
//var_dump($arr);
|
||||||
|
if($arr !== false) {
|
||||||
|
$ret[] = $arr;
|
||||||
|
}
|
||||||
|
else $notnull = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
function storeMessage(SQLite3 $db, string $content, ?int $user_id = null) {
|
||||||
|
$query = 'INSERT INTO message(id_user, content, date) VALUES (:id_user, :content, :date)';
|
||||||
|
$date = (new DateTime('now'))->getTimestamp();
|
||||||
|
$statement = $db->prepare($query);
|
||||||
|
$statement->bindParam(':id_user', $user_id);
|
||||||
|
$statement->bindParam(':content', $content);
|
||||||
|
$statement->bindParam(':date', $date);
|
||||||
|
$statement->execute();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue