PlainSQLiteBlog/pages/find.php

83 lines
2.0 KiB
PHP

<?php
global $c;
$strPath = "/";
$strPath .= implode("/", Request::getPathParts());
$varPosts = [];
$strQuery = Request::getParam("q");
$strAuthor = Request::getParam("username");
$strId = Request::getParam("id");
$intHasQuery = $strQuery !== null && strlen($strQuery) > 0;
$intHasAuthor = $strAuthor !== null && strlen($strAuthor) > 0;
$intHasId = $strId !== null && strlen($strId) > 0;
if ($intHasQuery)
{
$strQuery = preg_replace("/[^A-Za-z0-9]/", "", $strQuery);
$varPosts = $c->query(
"SELECT *
from posts as p
where
content like concat('%', ?, '%')
order by
created desc",
$strQuery);
$i = 0;
for ($i = 0; $i < count($varPosts); $i++)
{
$varOld = $varPosts[$i];
$varOld["content"] = preg_replace("/({$strQuery})/i", "<mark>$1</mark>", $varOld["content"]);
$varPosts[$i] = $varOld;
}
}
if ($intHasAuthor)
{
$varPosts = $c->query(
"SELECT *
from posts as p
where
username like ?
order by
created desc",
$strAuthor);
}
if ($intHasId)
{
$varPosts = $c->query(
"SELECT *
from posts as p
where
id = ?
order by
created desc",
$strId);
}
?>
<?php if (!$intHasAuthor && !$intHasId): ?>
<form method="get">
<table>
<tbody>
<tr>
<td><label>Search</label></td>
<td><input type="text" name="q" value="<?= $strQuery; ?>" /></td>
<td>
<input type="submit" value="Go" />
</td>
</tr>
</tbody>
</table>
</form>
<?php endif; ?>
<?php if ($intHasQuery || $intHasAuthor || $intHasId): ?>
<?php PostRender::rows($varPosts); ?>
<?php endif; ?>