I want to add dropdown in pagination so that if there is more then 10 pages it will be stored in dropdown so that any one can select any page from it.
Here is my php code which show this output:
1 2 3 4 5 6 7 8 9 10 11........... 100
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql);
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo " <a href='View.php?page=".$i."'>".$i."</a> ";
};
echo " <a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page
Output i want :
1 2 3 4 5 6 7 8 9 10 dropdown(contains all pages 11 to 100)
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql);
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page
$array = [];
for ($i=1; $i<=$total_pages; $i++) {
$array[$i] = "<a href='View.php?page=".$i."'>".$i."</a>";
};
echo " <a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page
Add all the page informations in array and then later populate the dropdown with an array I have changed the for loop code only ... you just popultae your dropdown with the $array;
I done it with php using your php code. Here is my code:
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql); //run the query
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'FIRST <<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++)
{
if($total_pages<=10)
{
echo " <a style='color:#333;' href='View.php?page=".$i."'>".$i."</a> ";
}
else
{
for ($i=1; $i<=10; $i++)
{
echo " <a style='color:#333;' href="View.php?page=".$i."'>".$i."</a> ";
};
echo "<select class='mySelectBox' onchange='location = this.options[this.selectedIndex].value;'>";
for ($i=11; $i<=$total_pages; $i++)
{
echo "<option value= View.php?page=".$i.">".$i."</option>";
};
echo "</select>";
}
};
echo " <a href='View.php?page=$total_pages'>".'........>> LAST'."</a> "; // Goto last page
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Update to include full code.
I am pulling records from a database and then displaying them 5 per page like this:
$num_rec_per_page=5;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$school = $_REQUEST['school'];
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%$school%'";
$rs_result = mysql_query($sql);
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search.php?school=".urlencode($school)."&page=".$i."'>".$i."</a>";
};
This gives me a long list like this for each page:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
What I would like to do is something like:
<Prev 1 2 3 4....53 54 55 Next>
Use LIMIT and OFFSET clauses in your SELECT query to implement pagination.
Here's the reference:
PHP Limit Data Selections From MySQL
So your code should be like this:
$num_rec_per_page = 5;
if(isset($_GET["page"])){
$page = $_GET["page"];
}else{
$page=1;
}
$offset = ($page - 1) * $num_rec_per_page;
$school = $_GET['school'];
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%" . $school. "%' LIMIT 5 OFFSET " . $offset;
$rs_result = mysql_query($sql);
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
for ($i=1; $i<=$total_pages; $i++){
if($i != 1){
$prev_page = $i - 1;
echo "<a href='search.php?school=".urlencode($school)."&page=" . $prev_page ."'><prev</a>";
}
echo " <a href='search.php?school=".urlencode($school)."&page=".$i."'>".$i."</a>";
if($i != $total_pages){
$next_prev = $i + 1;
echo " <a href='search.php?school=".urlencode($school)."&page=" . $next_page ."'>next></a>";
}
}
Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.
I am trying to output each row from a mysql query slightly differently in terms of styling, however I want to try to avoid having to use offsets and multiple queries.
Current code:
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 1";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
// output first news item with a style
echo '<div class="style-1">'.$title.'</div>';
}
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 1 OFFSET 1";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
// output second news item with different style
echo '<div class="style-2">'.$title.'</div>';
}
I would thus like to avoid having 2 (or more) queries simple because I want to use different css classes for each while row, something like this:
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 10";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
} // end while
$i = 1;
for($i; $i<2; $i++){ // output first row item with first style
echo '<div class="style-1">'.$title.'</div>';
} // end for loop 1
for($i; $i<3; $i++){ // output first row item with second style
echo '<div class="style-2">'.$title.'</div>';
} // end for loop 2
for($i; $i<4; $i++){ // output third row item with third style
echo '<div class="style-3">'.$title.'</div>';
} // end for loop 3
...
Desired Output:
<div class="style-1">News Headline Title 1</div>
<div class="style-2">News Headline Title 2</div>
<div class="style-3">News Headline Title 3</div>
...
I'm assuming you want 3 different styles, and here's how I would accomplish this:
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 10";
$query = mysqli_query($connection, $sql);
$i = 1;
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
if($i<2){
$styleNumber = 1;
}
else if($i<3){
$styleNumber = 2;
}
else if($i<4){
$styleNumber = 3;
}
echo '<div class="style-'.$styleNumber.'">'.$title.'</div>';
$i++;
}
// end while
Output:
Title 1 (Style 1)
Title 2 (Style 2)
Title 3 (Style 3)
Title 4 (Style 3)
This is not an answer. Just a comment/improvement of #JonTan answer since it was chosen as correct one.
My guess is that OP need to loop styles for every 3 records, but not to set style only for 3 first returned.
That means we can change condition from if($i<3){, if($i<2){ to something like:
$i = 1;
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
if($i % 3 == 0){
$styleNumber = 3;
} else if($i % 2 == 0){
$styleNumber = 2;
} else {
$styleNumber = 1;
}
echo '<div class="style-'.$styleNumber.'">'.$title.'</div>';
$i++;
}
I have one page where I echo 10 items from MySql DB and has pagination for next page. The problem is that there are 1000+ rows in database.. so you can guess how many pages are in pagination links. How can I limit their numbers for example to show
1 2 3 4 5 ... N
when user change to page 2
2 3 4 5 6 ... N
and so on?
Or something else.. the main goal is just to hide every single page number.
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 10;
$result = $pdo->prepare("SELECT t.* FROM images t order by randorder ASC LIMIT $start_from, 10");
$result->execute();
for($i=0; $row = $result->fetch(); $i++)
{
echo 'echo data';
}
echo '<div class="pagination" >';
$result = $pdo->prepare("SELECT COUNT(image_id) FROM images");
$result->execute();
$row = $result->fetch();
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='?page=".$i."'";
if($page==$i)
{
echo "class=active";
}
echo ">";
echo "".$i."</a> ";
};
echo '</div>';
?>
i'm already create page schools and ads
This page show like this
school 1 - school 2 - school 3
ads1
school 4 - school 5 - school 6
ads2
school 7 - school 8 - school 9
ads default << when looping ads finished
but i have i problem when i search 1 school
this school not show, i know this school not show because result query not multipe 3 data. if my query result 1 or 2 data. this page not show this result because this result not multiple 3 data.
This is my website : http://www.tingali.com/pencarian.html
you can search and type "MA AL KARIMIYAH"
This is my script :
pagging_page3.php
<?php
include('connection.php');
$limit = 24;
$adjacent = 3;
if(isset($_REQUEST['actionfunction']) && $_REQUEST['actionfunction']!=''){
$actionfunction = $_REQUEST['actionfunction'];
call_user_func($actionfunction,$_REQUEST,$con,$limit,$adjacent);
}
function showData($data,$con,$limit,$adjacent){
$page = $data['page'];
$dt = $_POST['dt'];
if($page==1){
$start = 0;
}
else{
$start = ($page-1)*$limit;
}
$sqlo = "select * from schools where showschool='YES' ".$dt." and typeschool='school' order by paid,urut asc";
$sql = stripslashes($sqlo);
$rows = $con->query($sql);
$rows = $rows->num_rows;
$sqlo = "select * from schools where showschool='YES' ".$dt." and typeschool='school' order by paid,urut asc limit $start,$limit";
$sql = stripslashes($sqlo);
$data = $con->query($sql);
$str='';
$i=0;
$k=0;
$m =1;
$rumus = $page * ($limit / $adjacent); // bntr
$data_web = mysqli_query($con,"SELECT * from web");
$wew = ($page - 1) * 8;
$row2 = mysqli_fetch_object($data_web);
while($row = $data->fetch_array(MYSQLI_ASSOC)){
// This Data Ads
if($i % 3 == 0)
{
$data_ads = mysqli_query($con,"SELECT * from ads limit $wew,1");
$rowads = mysqli_fetch_object($data_ads);
$str = is_null($rowads->link) ? "<a href='#'><img src='images/ads/default/default.jpg' alt=''></a>":"<a href='".$rowads->link."'><img src='images/ads/default/".$rowads->file."' alt=''></a>";
$wew++;
}
// This Data School
$jd = $row['link'];
$data_sl = mysqli_query($con,"SELECT count(kode) as counter FROM `school_counter` where kode ='".$row['kode']."' and visit between '".$row2->range_start."' AND '".$row2->range_end."' ");
$sl = mysqli_fetch_object($data_sl);
$str.=" <li class='col-lg-4 col-md-4 col-sm-4 col-xs-4'>
<div class='product_item'>
<div>
<a href='".$jd."'>
<img height='228px' width='224px' src='images/sekolah/logo/".$row['logo']."' class='c_image_1' alt=''>
</a>
</div>
<div class='product_info'>
<a href='".$jd."'><h4>".$row['name_school']."</h4></a>
<div class='clearfix'>
<font size='2' color='blue'>Dilihat : ".$sl->counter." </font></br>
<i class='fa fa-home'></i> : ".$row['addres']."<br/>
</div>
</div>
</div>
</li>
";
$i++;
if ($i%3 == 0) {
echo $str;
}
}
?>
Help me thank's
Try to change
$adjacent = 3;
to
$adjacent = 1;
I have code that I have used on several sites to deal with pagination. The problem I'm facing is that the code only give 'next' and 'previous' links. For the site I am working on I need page numbers too, not them all, maybe 5 at a time, kin of like this
< 1 2 3 4 5 >
then when you get to page 5
< 6 7 8 9 10 >
This is my pagination code so farf
//paganation settings
$pages_query = mysqli_query($link, "SELECT COUNT(`id`) FROM `products` WHERE subcat = 1 AND
status = 1") or die(mysqli_error($link));
$result = mysqli_fetch_array($pages_query, MYSQLI_NUM);
$pages = $result[0] / $per_page;
$page = (isset($_GET['page']) AND (int)$_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$last = ($pages - 1) / $per_page;
$prev = $page - 1;
$next = $page + 1;
//query the db
$q =mysqli_query($link, "SELECT * FROM products WHERE subcat = 1 AND status = 1
ORDER BY id DESC LIMIT $start, $per_page");
if(mysqli_num_rows($q) > 0){
//find out the page we are on and display next and previous links accordingly
if ($page >= 1){
if ($page < $pages) {
echo "<span class='next'>";
echo "Next ";
echo "</span>";
}
if ($page >=2){
echo "<span class='prev'>";
echo "Back ";
echo "</span>";
}
}
else if ($page > $pages + 1){
echo 'No more images in the database';
}
Can anyone help me add the page numbers in here
Thanks
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";
};
This might hel you
put it between if loops for next and prev page