PlainSQLiteBlog/pages/search.php

52 lines
1.2 KiB
PHP

<?php
global $c;
$strPath = "/";
$strPath .= implode("/", Request::getPathParts());
$varPosts = [];
$strQuery = Request::getParam("q");
$intHasQuery = $strQuery !== null && strlen($strQuery) > 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("/\b({$strQuery})\b/i", "<mark>$1</mark>", $varOld["content"]);
$varPosts[$i] = $varOld;
}
}
?>
<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 if ($intHasQuery): ?>
<?php PostRender::rows($varPosts); ?>
<?php endif; ?>