Paging in php mysql - php

I'm trying to make a website with movies in it, everything is fine but i have just 1 little problem,when ever i make a website, i do all work in my local computer test it then I upload the web, the code below is for paging with query it works fine in WAMP (locally). But when I upload the paging code to my web server it says NOT EXIST.
it shows the else part,whats the problem?
<?php
$per_page = 35;
$page = 1;
if (isset($_GET['page']))
{
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
$con= mysql_connect("localhost","sarya_asad","Thisisfor123");
mysql_select_db('saryaal_com_movies',$con);
$current_items = mysql_query( "SELECT * FROM `english` LIMIT $start_from, $per_page");
if( mysql_num_rows($current_items) > 0)
{
while($item = mysql_fetch_assoc($current_items))
{
?>
<tr>
<td> <strong><a href="english/english-preview.php?id=<?php echo$item['id']?>" ><?php echo $item['title'] ;?></a> </strong></td>
<td> <strong> <?php echo $item['year'] ;?> </strong></td>
<td> <strong> <?php echo $item['quality'] ;?> </strong> </td>
</tr>
<tr><td>
<?php
}
}
else
{
echo 'this page does not exists';
}
$total_rows = mysql_query("SELECT COUNT(*) FROM `english`");
$total_rows = mysql_fetch_row($total_rows);
$total_rows = $total_rows[0];
$total_pages = $total_rows / $per_page;
$total_pages = ceil($total_pages); # 19/5 = 3.8 ~=~ 4
for($i = 1; $i <= $total_pages; ++$i)
{
echo "<a href='temp2.php?page=$i' class='pagNumActive'>$i</a> ";
}
?>

<?php if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") {
$sql=mysql_query("select * from job where AND status='Active'");
}
$per_page = 5;
$page = 1;
if (isset($_GET['page'])) {
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="")
{
$current_items=mysql_query("select * from job LIMIT $start_from, $per_page"); } $start_from, $per_page");
if( mysql_num_rows($current_items)>0) { while($arr=mysql_fetch_array($current_items)) {
?>
<?php include("include/result.php") ?>// result u want to display
<?php
}
} else {
echo 'Data does not exists'; }
if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") {
$total_rows=mysql_query("select COUNT(*) from job where AND status='Active'");
}
$total_rows = mysql_query("SELECT COUNT(*) FROM job");
$total_rows = mysql_fetch_row($total_rows);
$total_rows = $total_rows[0];
$total_pages = $total_rows / $per_page;
$total_pages = ceil($total_pages);
# 19/5 = 3.8 ~=~ 4
echo "<div style='margin-left:280px;'>";
echo "Page : ";
for($i = 1; $i <= $total_pages; $i++) {
echo "[<a style='text-decoration:none' href='search_result.php?page=$i' class='pagNumActive'>$i</a> ]";
}
echo "</div>";
?>

This is your problem:
$con= mysql_connect("localhost","sarya_asad","Thisisfor123");
mysql_select_db('saryaal_com_movies',$con);
You need to change the host details. localhost is the local server on your machine.

change this
$start_from = ($page - 1) * $per_page;
to
$start_from = ($page) * $per_page;

you might want to use something like:
$per_page = 35;
$start_from = $page * $per_page - $per_page;

Try this logic for pagination:
Your goal is to offset by the number of images you want to display on each page.
So let's say you want to display 6 movie thumbnails on a page... You'd have:
$videos_per_page = 6
$pageNumber = (isset($_GET['page']) ? ($_GET['page']) : 1);
And your offset would be:
$offset = $videos_per_page * $pageNumber
(6 videos * page 0... so you're offsetting 0 videos. That's good because you want to display the first 6 videos in your data structure on page 0).
So now that you have your offset value... You need to set your array pointer to the correct spot... Loop through your database rows storing your movies and move your pointer by your offset value... Store that in $videos_to_offset...
while ($videos_per_page < $offset && ($row = $Query_Result->fetch_assoc())) {
$videos_to_offset++;
}
Now you can loop through your database rows, outputting your videos from wherever your offset array pointer left off:
$video_counter = 0;
while ($video_counter < $videos_per_page && ($row = $Query_Result->fetch_assoc())) {
echo $row['videopath'];
$video_counter++;
}
Something like that.

Related

PHP Pagination for MySQL posts

I've been trying for hours to implement a pagination for a blog that I am working on, but I just can't find it to work. In the URL, it gives me (myurl).php?pageno=
Without the page number requested.
This is my page to handle the database request:
<?php
require_once("db-connect.php");
$offset = 0;
$page_result = 5;
if($_GET['pageno'])
{
$page_value = $_GET['pageno'];
if($page_value > 1)
{
$offset = ($page_value - 1) * $page_result;
}
}
$results = mysqli_query($connection, "SELECT * FROM blog where $condition ORDER BY date DESC LIMIT $offset, $page_result")
or die(mysqli_error('No Records Found'));
?>
This is my blog page code:
<?php
if (mysqli_num_rows($results) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($results)) {
echo "<li class='background-white'><div class='column large-4'><a href='".$row['link']."' class='text-black1' title='".$row["arttitle"]."'>"."<img src='http://tmggeotech.com/img/blog/".$row['thumbnail']."' alt='".$row['thumbalt']."'/>"."</a></div>"."<div class='column large-8 article-data'>"."<h4><a href='".$row['link']."' class='text-black1' title='".$row["arttitle"]."'>".$row["arttitle"]."</a></h4>"."<small class='text-black2'>Publicado el <span class='text-orange2'>".date('m/d/Y', strtotime($row['date']))."</span> | "."Categoría: <span class='text-orange2'>".$row["category"]."</span></small>"."<p>".$row["excerpt"]."</p>"."</div>"."</li>";
} }else {
echo "0 results";
}
$pagecount = 50; // Total number of rows
$num = $pagecount / $page_result;
if($_GET['pageno'] > 1) {
echo "Prev";
}
for($i = 1 ; $i <= $num ; $i++) {
echo "".$i."";
}
if($num != 1) {
echo "Next";
}
?>
So my question is:
Is it there something I am missing here to make it work?
Thanks to #ccKep for point out some clues on how to get what I needed. I just made it work.
In page to handle the database request, I added a second request but without LIMIT, so that I could use num_rows with it:
<?php
require_once("db-connect.php");
$offset = 0;
$page_result = 5;
if($_GET['pageno'])
{
$page_value = $_GET['pageno'];
if($page_value > 1)
{
$offset = ($page_value - 1) * $page_result;
}
}
$results = mysqli_query($connection, "SELECT * FROM blog where $condition ORDER BY date DESC LIMIT $offset, $page_result")
or die(mysqli_error('No Records Found'));
$npages = mysqli_query($connection, "SELECT * FROM blog where $condition ORDER BY date DESC")
or die(mysqli_error('No Records Found'));
?>
Later, on my blog page I round up my posts to the next multiple of 5 number.
And also changed my last if statement so that the NEXT button disappears after getting the last page.
<?php
if (mysqli_num_rows($results) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($results)) {
echo "<li class='background-white'><div class='column large-4'><a href='".$row['link']."' class='text-black1' title='".$row["arttitle"]."'>"."<img src='http://tmggeotech.com/img/blog/".$row['thumbnail']."' alt='".$row['thumbalt']."'/>"."</a></div>"."<div class='column large-8 article-data'>"."<h4><a href='".$row['link']."' class='text-black1' title='".$row["arttitle"]."'>".$row["arttitle"]."</a></h4>"."<small class='text-black2'>Publicado el <span class='text-orange2'>".date('m/d/Y', strtotime($row['date']))."</span> | "."Categoría: <span class='text-orange2'>".$row["category"]."</span></small>"."<p>".$row["excerpt"]."</p>"."</div>"."</li>";
} }else {
echo "0 results";
}
$x = ceil($npages->num_rows/5) * 5;
$pagecount = $x; // Total number of rows
$num = $pagecount / $page_result;
if($_GET['pageno'] > 1) {
echo "<a href='noticias-perforacion-suelos-articulos-maquinas-blog.php?pageno=".($_GET["pageno"] - 1)."'>Prev</a>";
}
for($i = 1 ; $i <= $num ; $i++) {
echo "<a href='noticias-perforacion-suelos-articulos-maquinas-blog.php?pageno=".$i."'>".$i."</a>";
}
if($_GET['pageno'] == $num) {
echo "";
} else {
echo "<a href='noticias-perforacion-suelos-articulos-maquinas-blog.php?pageno=".($_GET["pageno"] + 1)."'>Next</a>";
}
?>
You might notice that I am not a very good coder, so if you find a less messy way to do this please let me know.
Cheers!

php pagination on second page not working

$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') ;
}

Next/Previous Page bug

I need some help, I want to make an next / previous page but I have some problems...
This is the PHP Code, how I try to make it. The problem is that it doesn't show 3 data on the first page and on the next page it puts the same data again an not the other one.
Code:
$statement = $connect->prepare("SELECT COUNT(*) AS anzahl FROM `accounts`");
$statement->execute();
$row = $statement->fetch();
$max_data = $row['anzahl'];
$page = 1;
if(isset($_GET['page'])) {
$page = intval($_GET['page']);
}
$max_info = 3;
$start = ($page - 1) * $max_info;
$number_page = ceil($max_data / floatval($max_info));
for($a = 1; $a <= $anzahl_seiten; $a++) {
if($page == $a){
echo " <b>$a</b> ";
}
else {
echo " <a href='acp.php?page=list_all_player&seite=$a'>$a</a> ";
}
}

show page numbering in PHP

I am using this coed in PHP to show next and previous buttons for records in a mysql database:
$sql="SELECT * from customer";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$MaxRowsPerPage = 25;
$total_records = mysql_num_rows($rs);
$total_pages = ceil($total_records / $MaxRowsPerPage);
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
}
$start_from = ($page-1) * $MaxRowsPerPage;
$sql.=" LIMIT $start_from, $MaxRowsPerPage";
I am echoing $total_records to show the total amount, how can i show the number from and to on the current page. for example, on page 1 it will be showing records 1 to 25 (because max rows per page is 25) and then page 2 will be showing records 26 to 50 and so on...
There a are many ways of doing this, but here's a simple pagination example I made. It will also show 1-25, 26-50 etc. It's heavily commented so it should be easy to understand.
<?php
// Connect to database
include 'includes/db_connect.php';
// Find total number of rows in table
$result = mysql_query("SELECT COUNT(*) FROM example_table");
$row = mysql_fetch_array($result);
$total_rows = $row[0];
// Set rows per page
$rows_per_page = 25;
// Calculate total number of pages
$total_pages = ceil($total_rows / $rows_per_page);
// Get current page
$current_page = (isset($_GET['p']) && $_GET['p'] > 0) ? (int) $_GET['p'] : 1;
// If current page is greater than last page, set it to last.
if ($current_page > $total_pages)
$current_page = $total_pages;
// Set starting post
$offset = ($current_page - 1) * $rows_per_page;
// Get rows from database
$result = mysql_query("SELECT * FROM example_table LIMIT $offset, $rows_per_page");
// Print rows
echo '<hr>';
while($row = mysql_fetch_array($result))
{
echo $row['id'].'<br />';
echo $row['text'];
echo '<hr>';
}
// Build navigation
// Range of pages on each side of current page in navigation
$range = 4;
// Create navigation link for previous and first page.
if ($current_page > 1)
{
$back = $current_page - 1;
echo ' PREV ';
if ($current_page > ($range + 1))
echo ' 1... ';
}
else
echo ' PREV ';
// Create page links, based on chosen range.
for ($i = $current_page - $range; $i < ($current_page + $range) + 1; $i++)
{
if ($i > 0 && $i <= $total_pages)
if ($i == $current_page)
echo ' [<strong>'.$i.'</strong>] ';
else
echo ' '.$i.' ';
}
// Create navigation link for next and last page.
if ($current_page != $total_pages)
{
$next = $current_page + 1;
if (($current_page + $range) < $total_pages)
echo ' ...'.$total_pages.' ';
echo ' NEXT ';
}
else
echo ' NEXT ';
?>

set maximum pagination links

I have a large database which can yield thousands of results and I need to limit the amount of pagination links of say 9 links, with previous 4 pages on one side then the current page then the 4 next pages.
for example: 14|15|16|17|18|19|20|21|22
anyone know how I could achieve this? Thanks
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
};
$start_from = ($page-1) * 10;
$message = "SELECT * FROM document WHERE email='$_SESSION[email]' ORDER BY id DESC LIMIT $start_from , 10";
// echo results
// make page links for results
$sql = "SELECT id FROM document WHERE email = '$_SESSION[email]'";
$rs_result = $db->query($sql);
$total_records = $rs_result->num_rows;
$total_pages = ceil($total_records / 10);
if($rs_result->num_rows >10) {
$page = "<p class = 'page'>";
for ($i=1; $i<=$total_pages; $i++)
{
$page .="<a href='results.html?page=".$i."'>".$i."</a> ";
}
$page .="</p>";
echo $page;
}
Just use basic math to calculate the start and end pages for use in your loop:
$page_range_offset = 4;
$page_start = $page - $page_range_offset;
if ($page_start < 1) {
$page_start = 1;
}
$page_end = $page + $page_range_offset;
if ($page_end > $total_pages) {
$page_end = $total_pages;
}
for ($i=$page_start; $i<=$page_end; $i++) {
$page .="<a href='results.html?page=".$i."'>".$i."</a> ";
}

Categories