$dwdb=mysqli_connect("localhost","root","","dw");
//this is my page number
$page=(isset($_GET['page']) && $_GET['page']>0)?$_GET['page']:1 ;
//this is my cat_id
$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;
$perpage=2;
$limit=($page > 1)?($page*$perpage)-$perpage:0;
$query=mysqli_query($dwdb,"select *from movies where y_id='$new' limit {$limit},{$perpage}");
while($result=mysqli_fetch_array($query)){
$id=$result['m_id'];
$name=$result['title'];
$img=$result['image'];
echo"<div><a href='downloadpage.php?yc=$id'>$id.....$name<br><img src='i/image/$img' style='height:200px;width:200px;'/></a></div>";
}
$query1=mysqli_query($dwdb,"select *from movies where y_id='$new'");
$total=mysqli_num_rows($query1);
$pages=ceil($total/$perpage);
echo "<a href='index1.php?page=1'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
echo "<a href='index1.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='index1.php?page=$pages'>Last page</a>";
That is my code. The problem is that on my second page i have result of first $new variable .......................................................................................................................................................................
... i have a problem that on my second page i have result of first $new variable
That's because you're not including $new variable in the pagination links. So every time you go to 2nd, 3rd, 4th, ... page, you'll get the same $new value as 1, and that's because of this statement,
$new=(isset($_GET['year']) && $_GET['year']>0)?$_GET['year']:1 ;
Include this variable in the pagination links so that you could get it's value in the subsequent pages. So your pagination links section would be like this:
// your code
echo "<a href='index1.php?page=1&year=".$new."'>".'First Page'."</a>";
for ($i=1; $i<=$pages;$i++){
echo "<a href='index1.php?page=".$i."&year=".$new."'>".$i."</a> ";
}
echo "<a href='index1.php?page=".$pages."&year=".$new."'>Last page</a>";
for pagination use this code in file with name: view-paginated.php
$per_page = 10;
$result = mysql_query("SELECT * FROM table");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
$start = 0;
$end = $per_page;
}
}
else
{
$start = 0;
$end = $per_page;
}
echo "<p><a href='view.php'>show all</a> | <b>page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results) { break; }
echo mysql_result($result, $i, 'YOUR COLUMN') ;
}
Related
I have the following code:
$max = 4;
$page = isset($_GET['page']) ? ($_GET['page']) : '1';
$init = $page - 1;
$init= $max * $init;
$strCount = "SELECT COUNT(*) AS 'total_mytable' FROM mytable";
$varstrCount = $crud->viewdatas($strCount);
$total = 0;
if(count($varstrCount)){
foreach ($varstrCount as $row) {
$total = $row["total_mytable"];
}
}
$result = "SELECT * FROM mytable ORDER BY id_mytable LIMIT $init,$max";
$varresult = $crud->viewdatas($result);
content of page:
<?php
if(count($varresult)){
foreach ($varresult as $res) {
?>
<h5><?php echo $res['title'] ?></h5>
<?php
}
}
?>
<?php
$max_links = 10;
$previous = $page - 1;
$next = $page + 1;
$pgs = ceil($total / $max);
if($pgs > 1 ){
if($previous > 0){
echo "<li><a href='".BASE_URL."/category/$previous' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
} else{
}
for($i=$page-$max_links; $i <= $pgs-1; $i++) {
if ($i <= 0){
}else{
if($i != $page{
if($i == $pgs){ //if end insert 3 dots
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li> ...";
}else{
echo "<li><a href='".BASE_URL."/category/".($i)."'>".$i."</a></li>";
}
} else{
if($i == $pgs){ //if end insert 3 dots
echo "<li>".$i."</li> ...";
}else{
echo "<li>".$i."</li>";
}
}
}
}
if($next <= $pgs){
echo "<li><a href='".BASE_URL."/category/$next' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}else{
}
}
?>
The result:
And I not understand the reason for the active number stay right off the paging menu
In the code I defined max-links for 10, but no show number 5 and if I define max links for 3 changes nothing , displays the same result as the image.
Thanks for any help
echo "<li>".$i."</li>";
on the above line add up a href, seems like you have css formatting for li>a because of which you are not getting the required formatting on only li. so for getting better formatting for all paging links current, prev, next. you need to add up a tags.
echo "<li><a>".$i."</a></li>"; //in your else part
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'm having trouble with the follow PHP which paginates the results of a MySQL query.
When I go to webpagename.php with the first page of the results and click Previous, the browser changes to webpagename.php?page=-1 and shows the first page of results again. If I click Previous again, it changes to webpagename.php?page=-2 and shows Page 1 of the results again, etc.
When I go to webpagename.php with the first page of the results and click Next, the browser changes to webpagename.php?page=1 and shows the first page of results again. I then have to hit Next a second time to move to Page 2.
When I go to the last page of the results - Page 8 - and click Next, the browser changes to webpagename.php?page=9 and shows Page 1 of the results. If I click Next again, it shows webpagename.php?page=10 and shows Page 1 of the results again, etc.
Expected Results:
When on Page 1 and a user hits Previous, I would like the code to do nothing/not decrement. When on Page 8 - the last page of results, I would like the code to do nothing/not increment. Of course, I would also expect that if you hit Next from Page 1 that it doesn't display Page 1 a second time but rather goes to Page 2.
Your exact changes to this code to make it work properly are very much appreciated. Thank you for time.
<?php
mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
// number of results to show per page
$per_page = 10;
// figure out the total pages in the database
$result = mysql_query("SELECT * FROM uc_users LEFT JOIN ent_dancers ON uc_users.id = ent_dancers.id WHERE ent_dancers.DancerYesNo = '1' AND ent_dancers.DancerEnabledYesNo = '1' ORDER BY uc_users.display_name ASC");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: webpagename.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
// display data in table
echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
$rowid = mysql_result($result, $i, 'id');
echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";
}
// close table>
echo "<div class='ugen-1'></div>";
echo "</div>";
$prev = $_GET['page'] - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a> ";
}
$next = $_GET['page'] + 1;
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
// pagination
?>
replace this:
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
by this:
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
elseif ($show_page > $total_pages)
{
$show_page=$total_pages;
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else {
$show_page=1;
$start = 0;
$end = $per_page;
}
}
else {
$show_page=1;
$start = 0;
$end = $per_page;
}
then :
$prev=$show_page-1;
$next=$show_page+1;
if($show_page>1){//this way previsous won't appear if you are at page 1 already
//show previous div
}
if($show_page<$total_pages){ //this way next won't appear unless you are not at the last page
//show next div
}
Make a variable $page set it equal to $_GET['page'].
if(isset($_GET['page'])){
$page = $_GET['page'];
}
else{
$page = 1;
}
you need to put a condition before echoing previous link to check whether $_GET['page'] is set or not and is greater than 1.
Like this:
if($page!=($start+1)){
$prev = $page - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
}
Add another condition for next
if($page!=$total_pages)
{
$next = $page+1
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
}
I hope your issue is solved.
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
$show_page=1
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
$show_page=1;
}
// display pagination
// display data in table
echo "<div class='dancerbio'>";
echo "<div class='uts-1'>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
$rowid = mysql_result($result, $i, 'id');
echo "<div class='uts-1-1'><a class='bodytxt5' href='webpagename-details.php?userid=$rowid'>" . mysql_result($result, $i, 'display_name') . "</a></div>";
}
// close table>
echo "<div class='ugen-1'></div>";
echo "</div>";
if($show_page!=($start+1)){
$prev = $page - 1;
echo "<div style='clear:both;height:1px;overflow: hidden;'></div>";
echo "<br /><a class='bodytxt5' href='webpagename.php?page=" . $prev . "'>Prev</a> ";
}
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a class='bodytxt5' href='webpagename.php?page=$i'>$i</a> ";
}
if($show_page!=$total_pages)
{
$next = $page+1
echo " <a class='bodytxt5' href='webpagename.php?page=" . $next . "'>Next</a> ";
echo "</div>";
}
// pagination
?>
if( isset($_GET['page'] ) )
{
$page = $_GET['page']-1;
$offset = $recLimit * $page;$page = $_GET['page']+1;
}
else
{
$page=2;
$offset = 0;
}
$phpself = $_SERVER['PHP_SELF'];
if( $totalPages <= $noofpages )
{
echo "Pages if less than or equal 5 : ";
for($i=1; $i <= $totalPages; $i++)
{
echo "$i |";
}
}
else
{
$i=1;
for($i; $i <= $totalPages; $i++)
{
echo "$i |";
}
}
?><table border="1" cellpadding="8"><tr> <th>ID</th><th>Name</th><th>Age</th><th>E-mail ID</th><th>Verified</th>
I'm goin to make a PHP pagination but i've some problem, in this i want to show 5 pages and 4 records per page. and if we click on next button then it shows the remaining page. And there are total 7 pages.
Since I couldn't really understand your code, here is something to get you started:
$items_per_page = 4;
/* Get the total number of records */
$result = mysqli_query("select count(*) as n from ...");
$row = mysqli_fetch_assoc($result);
$total_count = $row["n"];
/* Calculate the total number of pages */
$total_pages = ceil($total_count / $items_per_page);
/* Determine the current page */
$current_page = intval(#$_GET["page"]);
if(!$current_page || $current_page < 1) { $current_page = 1; }
if($current_page > $total_pages) { $current_page = $total_pages; }
/* Get the records for the current page */
$limit = $items_per_page;
$offset = ($current_page - 1) * $items_per_page;
$result = mysqli_query("select ... limit " . $offset . ", " . $limit);
To display the pagination links, use this:
for($i = 1; $i <= $total_pages; $i++)
{
if($i > 1) { print(" | "); }
if($current_page == $i) { print($i); }
else { print('' . $i . ''; }
}
Pagination problem in search result
I am trying to paginate the search results, Here is the code that i'm using to look up for the search queries from user's form. I am getting search results paginated, but when i click 2 nd page of results it shows undefined index for those item posted...While gone through tutorials i have seen to use $_GET instead of $_POST ,after doing that chane also no improvement in results
As i am new to this php code, can you guys help or guide me in the right direction?
/////////////test.php//////////////////
mysqli_report(MYSQLI_REPORT_ERROR);
// number of results to show per page
$per_page = 2;
// figure out the total pages in the database
if ($result = $mysqli->query("SELECT * FROM bridegroom where Gender like '%$Name%' and Castest like'%$caste%' and Location like '%$location%' order by AGE"))
{
if ($result->num_rows != 0){
$total_results = $result->num_rows;
// ceil() returns the next highest integer value by rounding up value if necessary
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
$start = 0;
$end = $per_page;
}
}
else
{
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> <b>View search results:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo $i . " ";
}
else
{
echo "<a href='test.php?page=$i'>$i</a> ";
}
}
echo "</p>";
// display data in table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// find specific row
$result->data_seek($i);
$row = $result->fetch_row();
// echo out the contents of each row into a table
echo ("Name:$row[1]<br><span class=\"old\"> Age:$row[4]</span><br><span class=\"sold\">Caste:$row[5]</span><br><span class=\"old\">Location:</span><span class=\"sold\">$row[6]</span><br><br>");
}
// close table>
}
else
{
echo "No results to display!";
}
}
// error with the query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
// display pagination
echo "<p> <b>Your search results:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo $i . " ";
}
else
{
echo "<a href='test.php?page=$i'>$i</a> ";
}
}
echo "</p>";
?>