I am doing my first pagination implementation to my site. I feel like I am very close and just confusing my logic or mistaking the placement or actual value of some variables.
Here is the code:
$pgsize_wave = 40;
$pg_wave = (is_numeric($_GET["p"]) ? $_GET["p"] : 1);
$start = ($pg_wave-1)*$pgsize_wave;
$waves = mysql_query("SELECT * FROM `CysticAirwaves` LIMIT $start, $pgsize_wave");
$waves_total = mysql_query("SELECT COUNT(1) FROM `CysticAirwaves`");
$waves_total = mysql_fetch_row($waves_total);
$waves_total = $waves_total[0];
$max_pages = $waves_total / $pgsize_wave;
$max_pages = ceil($waves_total/$pgsize_waves);
?>
<div id="all_page_turn">
<ul>
<?php if($waves_total > 40 && $pg_wave >= 40) { ?>
<li class="PreviousPageBlog round_10px">
Previous Page
</li>
<?php } ?>
<?php if($waves_total > 40 && $pg_wave < ($waves_total-40)) { ?>
<li class="NextPageBlog round_10px">
Next Page
</li>
<?php } ?>
</ul>
</div>
To clarify any confusion, an airwave is a post from a user and I want to limit 40 per page. If it helps here is the query that pulls an airwave on the same page:
$query = "SELECT * FROM `CysticAirwaves` WHERE `FromUserID` = `ToUserID` AND `status` = 'active' ORDER BY `date` DESC, `time` DESC";
$request = mysql_query($query,$connection);
$counter = 0;
while($result = mysql_fetch_array($request)) {
$replies_q = "SELECT COUNT(`id`) FROM `CysticAirwaves_replies` WHERE `AirwaveID` = '" . $result['id'] . "' && `status` = 'active'";
$request2 = mysql_query($replies_q,$connection);
$replies = mysql_fetch_array($request2);
$replies_num = $replies['COUNT(`id`)'];
$counter++;
$waver = new User($result['FromUserID']);
Thanks so much in advance.
This is pseudo code but hopefully explains the logic.
$total = SELECT COUNT(*) FROM `waves`
$currentPage = 1 // changes through page parameter in URL
$wavesPerPage = 40
$totalPages = ceil($total / $wavesPerPage)
$offset = ($wavesPerPage * ($currentPage - 1)) + 1
$waves = mysql_query("SELECT * FROM `waves` LIMIT $offset, $wavesPerPage");
while ($row = mysql_fetch_assoc($waves)) {
echo $row['wave']
…
}
To output the pagination links:
if ($totalPages > 1) {
if ($currentPage > 1) {
printf('Previous', $currentPage - 1);
}
for ($i = 1; $i <= $totalPages; $i++) {
$class = ($i == $currentPage) ? 'selected' : null;
printf('Page %1$u', $i, $class);
}
if ($currentPage < $totalPages) {
printf('Next', $currentPage + 1);
}
}
Related
I have a bit of code that pulls all the results from a database and displays the ones that are relevant to the users search. I have some more code that counts the amount of items and generates a certain amount of pages based on how many items are relevant to the users search. The problem is as follows. If I do a search all, my code displays everything in the database on 11 pages. If I search for car, it will still display 11 pages but only 2 results that have the word car in the title. The problem is that these results display on the eighth page and all other pages are blank. During the search all the two results with car in the title displayed on the eighth page as well. The search all is based on the order the items are in, in the database. Here is my current code:
$pagesQuery = mysql_query("SELECT count(id) FROM(`posts`)");
$pageNum = ceil(mysql_result($pagesQuery, 0)/5);
$start = (($page-1)*5);
$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5");
while ($row = mysql_fetch_array($currentname)) {
//recieve relevant data.
$title = $row[0];
$desc = $row[13];
$ID = $row[6];
$views = $row[3];
$user = $row[7];
//fetch the last id from accounts table.
$fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
$lastrow1 = mysql_fetch_row($fetchlast1);
$lastid1 = $lastrow1[6];
//acquire the username of postee.
for ($i1=1; $i1 <= $lastid1; $i1++) {
$currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user");
while ($row1 = mysql_fetch_array($currentname1)) {
$username1 = $row1[0];
}
}
//Format Title, description and view count.
$title2 = rtrim($title);
$donetitle = str_replace(" ", "-", $title2);
$url = "articles/".$ID."/".$donetitle."";
$donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title;
$donedesc = '';
if(strlen($desc) > 150) {
$donedesc = explode( "\n", wordwrap( $desc, 150));
$donedesc1 = $donedesc[0] . '...';
}else{
$donedesc1 = $desc;
}
$finviews = number_format($views, 0, '.', ',');
//Give relevant results
if(stripos($title, $terms) !== false || stripos($desc, $terms) !== false || stripos($username1, $terms) !== false){
if($row[10] == null){
$SRC = "img/tempsmall.jpg";
}else{
$SRC ="generateThumbnailSmall.php?id=$ID";
}
echo "<div id = \"feature\">
<img src=\"$SRC\" alt = \"article thumbnail\" />
</div>
<div id = \"feature2\">
$donetitle
<p id=\"resultuser\" >$username1</p>
<p id=\"resultp\">$donedesc1</p>
<img src=\"img/icons/flag.png\"/><b id=\"resultview\">$finviews views</b>
</div>
<div id = \"border\"></div>";
}
}
$totalPages = $pageNum;
$currentPage = $page;
$numPagesToShow = 10;
if($currentPage > $totalPages) {
$currentPage = $totalPages;
}
if($numPagesToShow >= $totalPages) {
$numMaxPageLeft = 1;
$numMaxPageRight = $totalPages;
} else {
$pagesToShow = ceil($numPagesToShow/2);
$numMaxPageLeft = $currentPage - $pagesToShow;
$numMaxPageRight = $currentPage + $pagesToShow;
if($numMaxPageLeft <= 0) {
$numMaxPageRight = $numMaxPageRight - $numMaxPageLeft +1;
$numMaxPageLeft = 1;
} elseif($numMaxPageRight >= $totalPages) {
$numMaxPageLeft -= ($numMaxPageRight - $totalPages);
$numMaxPageRight = $totalPages;
}
}
for ($i=$numMaxPageLeft; $i<=$numMaxPageRight; $i++) {
echo "<a id =\"pagenationlink\" href=\"searchresults.php?search=".$terms."&page=".$i."\">".$i."</a>";
}
How can I only display one page with the two results on it instead of 11 pages with the two relevant results on the eighth page? Thanks
Please update your code as below.
But try to use mysqli_() as mysql() are depricted
$cond = "";
if(!empty($_POST["search"]))
{
$cond = " write your search condition " ;
}
$start = (($page-1)*5);
$query = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM posts where $cond LIMIT $start, 5");
$TotalDataQuery = mysql_query("SELECT FOUND_ROWS() tot;");
$rsVal = mysql_fetch_array($pagesQuery);
$pagesQuery = $rsVal['tot'];
$pageNum = ceil($pagesQuery/5);
while ($row = mysql_fetch_array($query)) {
//continue your code
}
I have some PHP and html code that loads in results from my database. It shows five results per page. Let's pretend I have 1000 pages. The links for all those pages would go off the screen. Google had this problem but they fixed it by only displaying the current link as well as 5 links back and 5 links forward. I want to do something like this. I don't want to display 100 links to the various pages. Pretend the user is on page 100. I want to display the links for page 100 as well as link 95 to 105. How can I do this? Here is my code so far:
$page = $_GET["page"];
$pagesQuery = mysql_query("SELECT count(id) FROM(`posts`)");
$pageNum = ceil(mysql_result($pagesQuery, 0)/5);
$start = (($page-1)*5);
$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5");
while ($row = mysql_fetch_array($currentname)) {
//recieve relevant data.
$title = $row[0];
$desc = $row[13];
$ID = $row[6];
$views = $row[3];
$user = $row[7];
//fetch the last id from accounts table.
$fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
$lastrow1 = mysql_fetch_row($fetchlast1);
$lastid1 = $lastrow1[6];
//acquire the username of postee.
for ($i1=1; $i1 <= $lastid1; $i1++) {
$currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user");
while ($row1 = mysql_fetch_array($currentname1)) {
$username1 = $row1[0];
}
}
//Format Title, description and view count.
$title2 = rtrim($title);
$donetitle = str_replace(" ", "-", $title2);
$url = "articles/".$ID."/".$donetitle."";
$donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title;
$donedesc = '';
if(strlen($desc) > 150) {
$donedesc = explode( "\n", wordwrap( $desc, 150));
$donedesc1 = $donedesc[0] . '...';
}else{
$donedesc1 = $desc;
}
$finviews = number_format($views, 0, '.', ',');
//Give relevant results
if(stripos($title, $terms) !== false || stripos($desc, $terms) !== false || stripos($username1, $terms) !== false){
if($row[10] == null){
$SRC = "img/tempsmall.jpg";
}else{
$SRC ="generateThumbnailSmall.php?id=$ID";
}
echo "<div id = \"feature\">
<img src=\"$SRC\" alt = \"article thumbnail\" />
</div>
<div id = \"feature2\">
$donetitle
<p id=\"resultuser\" >$username1</p>
<p id=\"resultp\">$donedesc1</p>
<img src=\"img/icons/flag.png\"/><b id=\"resultview\">$finviews views</b>
</div>
<div id = \"border\"></div>";
}
}
for ($j=1; $j < $pageNum; $j++) {
echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>";
}
What you want is to change this:
for ($j=1; $j < $pageNum; $j++) {
echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>";
}
Here you list all links $j < $pageNum and you want to list just 10: $j <= 10 starting $j = $currentPage or $j = $currentPage - 5 if $currentPage is > 5
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> ";
}
I am using the php script below to generate comments from mysql, and also paginating it at the same time. I have 19 rows in the database, and I have set 5 comments per page in the variable. so I should have 4 links i.e. [1] [2] [3] [4] . but i am only getting 3 links
. I do not get any error. even when I set per page to 2, I still get 3 links.
<?php
$per_page = 5;
$total_query = $query = mysql_query("SELECT COUNT(*) FROM comments ") or die (mysql_error());
$pages = ceil(mysql_result($total_query, 0) / $total_query);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page ;
$query = mysql_query("SELECT * FROM comments LIMIT $start, $per_page") or die (mysql_error());
while ($comment = mysql_fetch_assoc($query)) {
<?php echo $comment['owner'] ; ?>
<?php echo htmlspecialchars($comment['body']) ;?>
<?php $date = date_create($comment['created']);
echo date_format($date, 'F j, Y g:i a'); ?>
if ($pages >= 1 && $page <= $pages) {
for ($x = 1; $x<=$pages; $x++) {
echo ($x == $page) ? '<a href="http://127.0.0.1/page.php?page='.$x.'">' . $x
.'</a> </span>' : ' [' . $x .' ] ';
}
}
?>
Your line:
$pages = ceil(mysql_result($total_query, 0) / $total_query);
is getting the result and dividing by... $total_query, which should be $per_page.
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.