I am trying to build a simple pagination in PHP. But for some reason it keeps outputting the error. I just can't seem to find the issue. Here is my code:
PHP :
include_once('connect.php'); // connect to db is successful
$count_query = mysql_query("SELECT NULL FROM users");
$count = mysql_num_rows($count_query);
// pagination starts here
if (isset($_GET['page'])) {
$page = preg_replace("#[^0-9]#", "", $_GET['page']);
} else {
$page = 1;
}
$perPage = 5;
$lastPage = cell($count / $perPage);
if ($page < 1) {
$page = 1;
} else if ($page > $lastPage) {
$page = $lastPage;
}
$limit = "LIMIT " . ($page - 1) * $perPage . ", $perPage";
$query = mysql_query('SELECT first_name FROM users ORDER BY user_id DESC $limit');
if ($lastPage != 1) {
if ($page != $lastPage) {
$next = $page + 1;
$pagination .= 'Next';
}
if ($page != $lastPage) {
$prev = $page - 1;
$pagination .= 'Previous';
}
}
while ($row = mysql_fetch_array($query)) {
$output .= $row['first_name'] . '<hr />';
}
HTML :
<!DOCTYPE html>
<html>
<head>
<title>Pagination</title>
</head>
<body>
<h1>My Pagination</a>
<?php echo $output; ?>
<?php echo $pagination; ?>
</body>
</html>
As said in the title, i'm getting a "Fatal error: Call to undefined function cell()".
I've spent hours of fixing and still no luck. I hope you guys figure it out and let me know what's the problem.
It is telling you that you are calling an undefined function. Your problem looks to be over here.
$lastPage = cell($count / $perPage);
You mean ceil(), not cell().
Simple spelling mistake, it is short for ceiling.
cell is a function call that PHP can't find...
You haven't shown it in your code so does it exist?
Related
This question already exists:
PHP's white screen of death [duplicate]
Closed 6 years ago.
Here is my code:
<?php
$sql = mysqli_query("SELECT thb_id FROM thumbnails LIMIT 10");
$result=mysqli_query($connection,$sql);
$count = mysqli_num_rows($sql);
if(isset($_GET['thb_id'])) {
$page = preg_replace('#[^0-9]#', '', $_GET['thb_id']);
} else {
$page = 1;
}
$lastPage = $count;
$total = 10;
if($page>1)
{
echo "<a href='project.php?project_id=".($page-1)."' class='button'>PREVIOUS</a>";
}
if($page!=$total)
{
echo "<a href='project.php?project_id=".($page+1)."' class='button'>NEXT</a>";
}
}
?>
My portfolio website is using PHP and MYSQL to call data from database,
each project has an assigned ID(thb_id), and i am showing 10 projects ($total = 10;). The code above is for my previous and next button, and they are on my "project.php" page. However, it has an internal server error, and i don't know how to fix it.
The updated version:
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$sql = ("SELECT thb_id FROM thumbnails LIMIT 10");
$result=mysqli_query($connection,$sql);
$count = mysqli_num_rows($result);
if(isset($_GET['thb_id'])) {
$page = preg_replace('#[^0-9]#', '', $_GET['thb_id']);
} else {
$page = 1;
}
$lastPage = $count;
if($page < 1) {
$page = 1;
} else if($page > $lastPage) {
$page = 1;
}
?>
The new problem is: both prev and next buttons only take users to project 1(ID = 1).
Looks like you have an extra } at the end
I was able to create a working pagination system for my application. But the problem I am having is when a user does a search, it can/will display over 100 pages in the pagination.
I am trying to figure out how to show only like 5 on each side of the current page. I would like to create a FIRST page button, and a LAST page button, but I'll deal with that later.
So here is the first part of code that counts the records in the database table:
<?php
function countRecords()
{
// The application takes a lot of user input, which then builds a query here.
// The user input goes into a session variable called $_SESSION['where']
// I'll start with the actual query code
$sql = "SELECT COUNT(DISTINCT CONTAINER_NUMBER) AS TOTAL
FROM 'contTable'" . " WHERE (" . $_SESSION['where'] . ");";
$sqlres = #mysql_query($sql) or die();
$row = mysql_fetch_row($sqlres);
$return $row[0];
}
So code above gets the count from the table depending on the search criteria.
Here is the next piece of code that prints the grid. I'll keep it as short as possible:
function displayrecords()
{
$rec_limit = 100;
$targetpage = "containers.php";
if(isset($_GET['page']))
{
$page = $_GET['page'];
$offset = $rec_limit * ($page - 1);
}
else
{
$page = 1;
$offset = $rec_limit * ($page - 1);
}
$left_rec = countRecords() - ($page * $rec_limit);
$select = "";
$_SESSION['where'] = "";
// user input variables are here that build a variable called $_SESSION['where']
// I'll skip the code down to the query
if ($_SESSION['where'] != "") $select = "SELECT * FROM 'contTable'" . " WHERE
(" . $_SESSION['where'] . ") GROUP BY container, bol LIMIT " . $offset . ",
" . $rec_limit . ";";
}
Please excuse any typos or missing brackets and whatnot. Just know the above code works.
Now here is the code for the rest of the pagination:
$total_records = countRecords();
$total_pages = ceil($total_records / $rec_limit);
$adjacents = 5; // I just added this variable. I don't know what to do with it
$previousPage = $page - 1;
$nextPage = $page + 1;
$querystring = "";
foreach ($_GET as $key => $value)
{
if ($key != "page") $querystring .= "$key=$value&";
}
echo '<ul style="border: 0px solid red; margin: 3px;" class="pager">';
if ($left_rec < $rec_limit)
{
$last = $page - 2;
echo #"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
Previous</a></li>";
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
}
else if ($page == 0)
{
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
echo #"<li>Next</li>";
}
else if ($page > 0)
{
$last = $page - 2;
echo #"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
Previous</a></li> ";
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
echo #"<li>Next</li>";
}
echo '</ul>';
}
So, with all of the code above, I can display a grid, and display the pages at the bottom of the application. But I don't want to show all 100 pages, only 5 on each side of the current page. I know I need to utilize the variable called $adjacents and plug it in to the paging portion of the code. But I'm not exactly sure how to do it.
I hope I am being clear on my request.
Please help.
Instead of looping through all of the pages:
for($i = 1; $i <= $total_pages; $i++)
Try doing something like this:
$start = ($page < $adjacents ? 1 : $page - $adjacents);
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
for($i= $start; $i <= $end; $i++)
//Here you can loop through the numbers within adjacents of the current page
I think i am very near to my correct answer. I found one code on this forum like i wrote below.but it didn't work for me.Whats wrong with this code???
<?php
include("config.php");
$start = 0;
$per_page = 10;
$targetpage = "manual.php?id=$id"; // This is my target page
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;
........................
.............................
.................?>
The answer which i saw contains id=42 in $targetpage = "manual.php?id=$id";But i don't understand why he used 42 there???
Then linking target page in next previous code as follows
<?php
if($page > 1){
$prev = $page - 1;
$prev = " <a href=\'$targetpage&page=$prev'>prev</a> ";
} else {
$prev = "";
}
if($page < $num_pages){
$next = $page + 1;
$next = " <a href=\'$targetpage&page=$next'>next</a> ";
}
else
{
$next = "";
}
echo $prev;
echo $next;
?>
But it gives me error like \'manual.php is not found..But i have this file.
just a guess, try to use:
$targetpage = "./manual.php?id=$id";
if it doesn't work then it means you are specifying the wrong URL to your manual.php page, try to find the correct URL
and id=42 it's an application specific parameter so it's related to that app so it's okay if you don't use it in your app.
Hi i am a new programmer,
i have a PHP & Mysql based pagination, i want to add Ajax functionality to it.
i have gone through many tutorials but i was not able to find any which tells about adding ajax onto existing pagination they all tell about making Ajax based pagination.
i want user be able to paginate even if javascript is turned off. so i want to add some Ajax to my code so that i can be able to paginate with Ajax and PHP.
i can use jquery .load() method to paginate.
please look at my code and suggest me how i can fetch page url for ajax to paginate
i guess something like this has to work. i cant figure out how, please help.
or tell me some tutorial i can learn from.
Jquery Code
$(document).ready(function(){
$('#pagination').click(function(){
$('pageurl').load('is-test2.php #PaginationDiv');});
});
PHP & MySQL Based Pagination
<?php
require_once('_ls-global/php/connection.php');
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 2;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$currentpage = (int) $_GET['page'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM internet_security ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage;
Code in html
h3>Results <?php echo ($startrow+1) ?> - <?php echo min($startrow + $rowsperpage, $row) ?> of <?php echo ($totalpages *$rowsperpage) ?></h3>
<ul><?php
if ($currentpage!=$totalpages) {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$totalpages'>$totalpages</a></li> ";
$nextpage = $currentpage + 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next»»</a></li> ";
}?></ul>
<ul><?php
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li>";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li> ";
}}}
}
}
?> </ul>
<ul><?php
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>««Prev</a></li> ";
echo "<li><a href='{$_SERVER['PHP_SELF']}?page=1'>1</a></li> ";
}?></ul>
since you are getting your page variable into the PHP script with GET method, you can pass the variable like :
$(document).ready(function(){
$('#pagination ul li a').click(function(){
e.preventDefault();
$('#divtoreplace').load($(this).attr("href"));
});
});
I have two tiny little problems;
1 . I want to add a previous / next button into this code
2 . I want it to only show like max 10 links between previous and next. So if i have 50 numbers/links it will only show 10 of them and not 50 links on the page.
I have searched on the clo
The code works, only need that two options in it.
Can someone help me out? Thank you !
<?php
include 'includes/connection.php';
$per_page = 8;
$pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)) {
echo '<p>', $query_row['name'] ,'</p>';
}
if ($pages >= 1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
//echo $x, ' ';
echo ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
}
}else{
header("location:index.php?page=1");
}
?>
First of all, you should, just for good practice, put an "exit;" after the header() call at the end. It doesn't make a difference in this particular script, but keep in mind that any code following a header("Location: ...") call WILL be executed before redirection.
Now to your question, try this (UPDATE: This code has been tested and works.)
<?php
include 'includes/connection.php';
$per_page = 8;
$pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query))
{
echo '<p>' . $query_row['name'] . '</p>';
}
// If the requested page is less than 1 or more than the total number of pages
// redirect to the first page
if($pages < 1 || $page > $pages)
{
header('Location: ?page=1');
// end execution of the rest of this script
// it will restart execution after redirection
exit;
}
// If more than one page, show pagination links
if($pages > 1)
{
$html = array();
$html[] = '<strong>';
// if you're on a page greater than 1, show a previous link
$html[] = (($page > 1) ? 'Previous ' : '');
// First page link
$pageFirst = '1';
$html[] = (($page == 1) ? "</strong>{$pageFirst}<strong>" : $pageFirst);
if ($pages > 6)
{
$start_cnt = min(max(1, $page - (6 - 1)), $pages - 6);
$end_cnt = max(min($pages, $page + 4), 8);
$html[] = ($start_cnt > 1) ? '...' : ' ';
for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
{
$html[] = ($i == $page) ? '</strong>' . $i . '<strong>' : '' . $i . '';
if ($i < $end_cnt - 1)
{
$html[] = ' ';
}
}
$html []= ($end_cnt < $pages) ? '...' : ' ';
}
else
{
$html[] = ' ';
for ($i = 2; $i < $pages; $i++)
{
$html[] = ($i == $page) ? '</strong>' . $i . '<strong>' : '' . $i . '';
if ($i < $pages)
{
$html[] = ' ';
}
}
}
// last page link
$pageLast = '' . $pages . '';
$html[] = (($page == $pages) ? "</strong>{$pageLast}<strong>" : $pageLast);
// Show next page link if you're on a page less than the total number of pages
$html[] = ($page < $pages) ? ' Next' : '';
// If you're not on the last page, show a next link
$html[] = '</strong>';
}
else
{
// show page number 1, no link.
$html[] = '<strong>1</strong>';
}
echo implode('', $html);
Also note that the final ?> is not required in PHP files that do not have HTML code following the PHP code, so I left it off.
Buddy refer this URL
http://www.codediesel.com/php/simple-pagination-in-php/
OR
http://www.phpfreaks.com/tutorial/basic-pagination
Surely will help you.
Thanks