i want to make the pages not go beyond 10
this the code that shows the number of pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}`
this is the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
and i want to make it go like this
1 2 3 4 5 6 7 8 9 10 ...
and if im on page 10 it will show like this
1 .. 10 11 12 13 14 15 16 17 18 19 20 ... 25
i don't have any idea how to make this work
this is the source
<?php
if(isset($_POST['search']))
{
unset($_SESSION['keyword']);
$KeyWord = $_POST['keyword'];
$_SESSION['keyword'] = $KeyWord;
}
$KW = $_SESSION['keyword'];
$results_per_page = 1;
$sql="SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR Book_Author
LIKE '%$KW%' ORDER BY Book_Title";
$result = mysqli_query($connect, $sql);
$number_of_results = mysqli_num_rows($result);
$number_of_pages = ceil($number_of_results/$results_per_page);
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
$this_page_first_result = ($page-1)*$results_per_page;
$sql="SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR Book_Author LIKE '%$KW%' ORDER BY Book_Title LIMIT $this_page_first_result, $results_per_page";
$result = mysqli_query($connect, $sql);
$query = "SELECT * FROM books_data WHERE Book_Title LIKE '%$KW%' OR Book_Author LIKE '%$KW%' ORDER BY Book_Title ASC";
$sql = mysqli_query($connect, $query);
$nurows = mysqli_num_rows($sql);
$numrows = $connect->query($query);
if($nurows == 0 OR $nurows == 1)
{
$restext = "result";
}
else
{
$restext = "results";
}
if(isset($_POST['search']))
{
$num = 0;
$desc = "result. Keyword is blank... Please try again.";
}
else
{
$num = '';
$desc = '';
}
?>
<p><strong><?php $num ?> </strong><?php $desc ?> </p>
<?php
?> <p><strong><?php echo $nurows; ?> </strong> <?php echo $restext;?> for '<?php echo $KW; ?>'</p>
<?php
while($row = mysqli_fetch_array($result)) {
$bookid = $row['Book_ID'];
$booktitle = $row['Book_Title'];
$bookauthor = $row['Book_Author'];
$bookimg = $row['Book_IMG_Name'];
echo "<hr><img src='../../images/bookimg/$bookimg' class='bookimg'><h3>$booktitle</hr><p><font style='font-size: 15px;'><font style='font-style: italic;'>by " . $bookauthor . '</p></font><br />';
}
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}
try the following:
function pageList($pages, $current_page)
{
$output = '';
$previous_page = 0;
foreach($pages as $page)
{
if ($page - $previous_page > 2)
{
$output.= '... ';
}
else
if ($page - $previous_page === 2)
{
$output.= '... ';
}
$output.= "$page ";
$previous_page = $page;
}
return $output;
}
function createPagination($current_page, $number_of_pages)
{
$page_span = 5;
$middle_min = max($current_page - $page_span, 1);
$middle_max = min($current_page + $page_span, $number_of_pages);
$pages = range($middle_min, $middle_max);
if ($middle_min !== 1)
{
array_unshift($pages, 1);
}
if ($middle_max !== $number_of_pages)
{
$pages[] = $number_of_pages;
}
return pageList($pages, $current_page);
}
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$number_of_pages = 25;
echo createPagination($current_page, $number_of_pages);
This will dispaly the 5 pages before and after of the selected page. Just edit the $page_span to determine how many of the pages you want to show. This uses a simple loop to loop over range of values from the minimum to maximum range.
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
<?php
if (isset($_GET['page'])) {
$pageno = $_GET['page'];
} else {
$pageno = 1;
}
$getPages = "SELECT COUNT(steamid) FROM player_time";
$rs_result = mysql_query($getPages, $sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
$printed_ellipsis = false;
for ($i=1; $i<=$total_pages; $i++) {
if($i > 4 && !$printed_ellipsis)
{
echo "…"; //printing the ellipsis
$i = max(0, $total_pages - 3);
$printed_ellipsis = true;
}
if($i == $pageno)
{
echo "<span class='current-page'>$pageno</span>";
}
else
{
echo "<a class='paginations' href='index.php?page=".$i."'>".$i."</a> ";
}
};
?>
Output : 1 2 3 4 … 76 77 78 79
When I reach the page 4, the 5,6,7,8.... don't appear. So basically I am stuck at page 4, without possibility of going to the fifth sixth and so on.
What's wrong here?
I want to limit the pages out of the total number of pages.
Example: 1 2 3 4 5 6 >> out of 30
Example2: << 5 6 7 8 9 10 >> out of 30
Here is my code:
$page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
// records per page
$per_page = 5;
// total records in database
$total_count = Mp3_Model::count_all();
// instantating the $pagination
$pagination = new Pagination($page, $per_page, $total_count);
// find the records for this page
$sql = "SELECT * FROM mp3 ";
$sql .= " LIMIT {$per_page} ";
$sql .= " OFFSET {$pagination->offset()}";
$mp3s = Mp3_Model::find_by_sql($sql);
foreach ($mp3s as $mp3) {
echo $mp3->titlu;
}
and this is the pagination:
<?php
if ($pagination->total_pages() > 1) {
if ($pagination->has_previous_page()) {
echo '<li>«</li>';
}
for ($i = 1; $i <= $pagination->total_pages(); $i++) {
echo '<li';
if ($_GET['page'] == $i) {
echo ' class="active"';
}
echo '>' . $i . '</li>';
}
if ($pagination->has_next_page()) {
echo '<li>» </li>';
}
}
?>
You can use jQuery pagination, it is a much better solution.
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.