I'm doing a paging, but it's generating a memory error.
Is there any way to optimize or fix the crash? Or is it on the same server?
Note : php.ini
is set to 512 in memory.
if (!(isset($_GET['pagenum']))) {
$pagenum = 1;
} else {
$pagenum = $_GET['pagenum'];
}
$page_limit = ($_GET["show"] <> "" && is_numeric($_GET["show"]) ) ? $_GET["show"] : 30;
try {
$keyword = trim($_GET["keyword"]);
if (!empty($keyword)) {
$sql = "SELECT * FROM noticias WHERE conteudo LIKE :keyword OR titulo LIKE :keyword ORDER BY Nid DESC";
$stmt = $DB->prepare($sql);
$likekeyword = "%".$keyword."%";
$stmt->bindParam(':keyword', $likekeyword, PDO::PARAM_STR);
} else {
$sql = "SELECT * FROM noticias WHERE 1 ORDER BY Nid DESC";
$stmt = $DB->prepare($sql);
}
$stmt->execute();
$total_count = count($stmt->fetchAll());
$last = ceil($total_count / $page_limit);
if ($pagenum < 1) {
$pagenum = 1;
} elseif ($pagenum > $last) {
$pagenum = $last;
}
$lower_limit = ($pagenum - 1) * $page_limit;
$lower_limit = ($lower_limit < 0) ? 0 : $lower_limit;
$sql2 = $sql . " limit " . ($lower_limit) . " , " . ($page_limit) . " ";
$stmt = $DB->prepare($sql2);
if ($keyword <> "" ) {
$stmt->bindParam(':keyword', $likekeyword, PDO::PARAM_STR);
}
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
code displays counters
<div class="col-lg-12 center">
<ul class="pagination pagination-sm">
<?php
for ($i = 1; $i <= $last; $i++) {
if ($i == $pagenum) {
?>
<li class="active"><a href="javascript:void(0);" ><?php echo $i ?></a></li>
<?php
} else {
?>
<li><a href="noticias_listar.php?pagenum=<?php echo $i; ?>&keyword=<?php echo $_GET["keyword"]; ?>" class="links" onclick="displayRecords('<?php echo $page_limit; ?>', '<?php echo $i; ?>');" ><?php echo $i ?></a></li>
<?php
}
}
?>
</ul>
</div>