Get Messages
This commit is contained in:
parent
6538ccd525
commit
6c0a6bc500
|
@ -1,9 +1,16 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
createDB();
|
$db = createDB();
|
||||||
|
$test = getMessages($db, ['id_user' => 1]);
|
||||||
|
var_dump($test);
|
||||||
|
|
||||||
function createDB(): SQLite3 {
|
function createDB($db_name = 'guestbook.db'): SQLite3 {
|
||||||
$db = new SQLite3('guestbook.db');
|
// Can't create table if it already exists
|
||||||
|
$exists = file_exists($db_name);
|
||||||
|
|
||||||
|
$db = new SQLite3($db_name);
|
||||||
|
|
||||||
|
if(!$exists) {
|
||||||
$db->exec('CREATE TABLE user
|
$db->exec('CREATE TABLE user
|
||||||
(id_user INTEGER PRIMARY KEY,
|
(id_user INTEGER PRIMARY KEY,
|
||||||
username TEXT NOT NULL UNIQUE,
|
username TEXT NOT NULL UNIQUE,
|
||||||
|
@ -16,33 +23,37 @@ function createDB(): SQLite3 {
|
||||||
date INT,
|
date INT,
|
||||||
FOREIGN KEY (id_user) REFERENCES user (id_user))'
|
FOREIGN KEY (id_user) REFERENCES user (id_user))'
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return $db;
|
return $db;
|
||||||
}
|
}
|
||||||
|
|
||||||
// getMessages(['id' => 34, 'since' => 383489289424])
|
function getMessages(SQLite3 $db, array $filter = [], int $limit = 25) {
|
||||||
|
|
||||||
function getMessages(SQLite3 $db, int $limit = 25, array $filter = []) {
|
|
||||||
// Apply filters
|
// Apply filters
|
||||||
$append_string = '';
|
$append_string = '';
|
||||||
$verb = 'WHERE';
|
$verb = 'WHERE';
|
||||||
|
|
||||||
foreach ($filter as $key => $value) {
|
foreach($filter as $key => $value) {
|
||||||
$to_append = match($key) {
|
$to_append = match($key) {
|
||||||
'id_msg' => "$verb $key IN (:$key)",
|
'id_user' => "$verb $key = :$key",
|
||||||
// TODO
|
// TODO
|
||||||
};
|
};
|
||||||
if ($to_append) {
|
if($to_append) {
|
||||||
$append_string .= " $to_append";
|
$append_string .= " $to_append";
|
||||||
$verb = 'AND';
|
$verb = 'AND';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare statement
|
||||||
$query = 'SELECT * FROM message' . $append_string;
|
$query = 'SELECT * FROM message' . $append_string;
|
||||||
|
var_dump($query);
|
||||||
|
|
||||||
$statement = $db->prepare($query);
|
$statement = $db->prepare($query);
|
||||||
|
|
||||||
foreach ($filter as $key => $value) {
|
foreach($filter as $key => $value) {
|
||||||
$statement->bindValue($key, $value);
|
$statement->bindParam($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $statement->execute();
|
var_dump($statement);
|
||||||
|
return $statement->execute()->fetchArray(SQLITE3_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue