Store Message
This commit is contained in:
parent
b0a41b7966
commit
7f02744efe
18
index.php
18
index.php
|
@ -1,10 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<?php
|
||||
require_once('persistence.php');
|
||||
$db = createDB();
|
||||
?>
|
||||
<html lang="ca">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Guestbook</title>
|
||||
</head>
|
||||
<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>
|
||||
</html>
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
<?php
|
||||
|
||||
$db = createDB();
|
||||
$test = getMessages($db, ['id_user' => 1]);
|
||||
var_dump($test);
|
||||
|
||||
function createDB($db_name = 'guestbook.db'): SQLite3 {
|
||||
// Can't create table if it already exists
|
||||
$exists = file_exists($db_name);
|
||||
|
@ -46,7 +42,7 @@ function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
|
|||
|
||||
// Prepare statement
|
||||
$query = 'SELECT * FROM message' . $append_string;
|
||||
var_dump($query);
|
||||
//var_dump($query);
|
||||
|
||||
$statement = $db->prepare($query);
|
||||
|
||||
|
@ -54,6 +50,28 @@ function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
|
|||
$statement->bindParam($key, $value);
|
||||
}
|
||||
|
||||
var_dump($statement);
|
||||
return $statement->execute()->fetchArray(SQLITE3_ASSOC);
|
||||
$result = $statement->execute();
|
||||
|
||||
$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