I have a problem in my code, my pagination is not working when i navigate link its generate notice error undefined index: package name.
here is my code :
include("includes/header.php");
include('php_script/db.php');
$package_name = $_GET['package_name'];
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 1;
$offset = ($pageno-1) * $no_of_records_per_page;
$result = mysqli_query($con,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql = "select * from tour_package where package_name
='".$package_name."'
order by id desc LIMIT $offset, $no_of_records_per_page ";
$res_data = mysqli_query($con,$sql);
Here is pagination link :
<ul class="pagination">
<li>First</li>
<li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
<a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".
($pageno - 1); } ?>">Prev</a> </li>
<li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
<a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?
pageno=".($pageno + 1); } ?>">Next</a></li>
<li>Last</li>
</ul>
Try this
<?php
include("includes/header.php");
include('php_script/db.php');
if(isset($_GET['package_name'])){
$package_name = $_GET['package_name'];
$_SESSION['package_name']=$package_name; //First time when you have package_name in url then assign this in session
}
if(isset($_SESSION['package_name'])){
$package_name = $_SESSION['package_name']; //Other times when you have not package_name in url then use this by session
}
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 1;
$offset = ($pageno-1) * $no_of_records_per_page;
$result = mysqli_query($con,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
if(isset($package_name)){ // check if you have package_name variable then your query will run
$sql = "select * from tour_package where package_name
='".$package_name."'
order by id desc LIMIT $offset, $no_of_records_per_page ";
$res_data = mysqli_query($con,$sql);
}
?>
Related
I need serial number next to next page, first page from 1 to 10, next from 11 to 20...... but my code prints 1 to 10 for all pages.
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$number = 1;
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT COUNT(*) FROM regs";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
//print_r($total_pages);
$result = mysqli_query($conn, "SELECT * FROM regs LIMIT $offset, $no_of_records_per_page");
while($res = mysqli_fetch_array($result)) {
// How many elements per page
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['Email']."</td>";
echo "<td>".$res['Gender']."</td>";
echo "<td>View | Edit | Delete</td>";
$pageNumber = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$currentNumber = ($pageNumber - 1) * $no_of_records_per_page + $number;
echo "<td> ". $currentNumber++ ." </td>";
$number++;
}
?>
your code should be like that
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$number = 1;
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT COUNT(*) FROM regs";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
//print_r($total_pages);
$result = mysqli_query($conn, "SELECT * FROM regs LIMIT $offset, $no_of_records_per_page");
while($res = mysqli_fetch_array($result)) {
// How many elements per page
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['Email']."</td>";
echo "<td>".$res['Gender']."</td>";
echo "<td>View | Edit | Delete</td>";
$currentNumber = ($pageno - 1) * $no_of_records_per_page + $number;
echo "<td> ". $currentNumber++ ." </td>";
$number++;
}
I have a little script that have a Previous & Next button.
My problem is I want to detect & place the ID in those buttons
this is the code from the pagination
<body>
<?php include_once 'data.php'; ?>
<center>
<ul class="pagination">
<?php
if($page_counter == 0){
echo "<li><a href=?start='0' class='active'>0</a></li>";
for($j=1; $j < $paginations; $j++) {
echo "";
}
}else{
echo "<a href=?start=$previous><button>Previous</button></a>";
for($j=0; $j < $paginations; $j++) {
if($j == $page_counter) {
echo " ";
}else{
echo " ";
}
}if($j != $page_counter+1)
echo "<a href=?start=$next><button>Next</button></a>";
}
?>
</ul>
</center>
In this part I have the ID but the problem is I can`t get it with this example to place it into the pagination.
<?php
foreach($result as $row) {
echo '
<div class="card"><button1>
'. $row['notice_id'] .'
<div class="time-left">
<div class="dropdown1">
' ;
}
}
else {
echo '';
}
$conn->close();
?>
</div></div>
This is the code from data.php I think I need to place some code into for detection from the ID
<?php
//include configuration file
require 'configuration.php';
$start = 0; $per_page = 1;
$page_counter = 0;
$next = $page_counter + 1;
$previous = $page_counter - 1;
if(isset($_GET['start'])){
$start = $_GET['start'];
$page_counter = $_GET['start'];
$start = $start * $per_page;
$next = $page_counter + 1;
$previous = $page_counter - 1;
}
// query to get messages from messages table
$q = "SELECT * FROM group_notice LIMIT $start, $per_page";
$query = $db->prepare($q);
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
// count total number of rows in students table
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
// calculate the pagination number by dividing total number of rows with per page.
$paginations = ceil($count / $per_page);
?>
You can use some formulas for calculate limit and offset of results.
For example you have 100 records, and you want to paginate it into 10 records per page, and the formula is like below.
// Get Page from Query String
$page = 1;
if(!empty($_GET["page"])){
$page = $_GET["page"];
}
// Get record count
$count_query = "SELECT * FROM group_notice";
$query = $db->prepare($count_query);
$query->execute();
$count = $query->rowCount();
$per_page = 10; // records per page
$pages = ceil($count/$per_page); // get total page
$start = $page * $per_page - $per_page; // get offset
// Do something with your records
$query = db->prepare("SELECT * FROM group_notice LIMIT ?, ?");
$query->bind_param("ii", $start, $per_page); // you should use bind param if you use prepared statement
$query->execute();
if($query->rowCount() > 0){
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}
if($page > 1){
echo 'Prev'; // Print Prev Page
}
for($i = 1; $i <= $pages){
echo ''.$i.''; // Print Page Number
}
if($page < $pages){
echo 'Next'; // Print Next Page
}
Hope it helps.
My pagination is working good .But problem is that when I click on pagination then its does not generate new link .Its add a page id with old link .Like
videos.php?page=2page=3 and if again i cliked on 4th number pagination .Its show like this videos.php?page=2page=3page=4.
<?php
$limit = 20;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "$que LIMIT $start_from, $limit";
$rs_result = mysql_query($sql);
$result = mysql_query($que);
$total_bookss = mysql_num_rows($result);
$full_linkp = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$full_linkEx=explode('&page',$full_linkp);
$full_link=$full_linkEx[0];
if($total_bookss>$limit){
$total_records = $total_bookss;
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
//$pagLink .= "<li><a href='$full_link.php&page=". $i."'>".$i."</a></li>";
$pagLink .= "<li><a href='$full_link&page=".$i."'>".$i."</a></li>";
}
//show pagination variable
$show_pagination=$pagLink . "</ul></nav>";}
?>
<script>
jq(document).ready(function(){
jq('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : <?php echo $page;?>,
hrefTextPrefix : '<?=$full_link?>page='
});
});
</script>
Try that
<?php
$limit = 20;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "$que LIMIT $start_from, $limit";
$rs_result = mysql_query($sql);
$result = mysql_query($que);
$total_bookss = mysql_num_rows($result);
$full_linkp = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$full_linkEx=explode('?page',$full_linkp);
$full_link=$full_linkEx[0];
if($total_bookss>$limit){
$total_records = $total_bookss;
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
//$pagLink .= "<li><a href='$full_link.php&page=". $i."'>".$i."</a></li>";
$pagLink .= "<li><a href='$full_link?page=".$i."'>".$i."</a></li>";
}
//show pagination variable
$show_pagination=$pagLink . "</ul></nav>";}
?>
<script>
jq(document).ready(function(){
jq('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : <?php echo $page;?>,
hrefTextPrefix : '<?=$full_link?>?page='
});
});
</script>
ok si im not very fimiliar with PDO im just starting off, could someone help me here why my results will not display? the pages are working but no data would show ..
<?php
require_once 'db/_db.php';
$stmt = $db->prepare("SELECT * FROM article");
$stmt->execute();
$result = $stmt->fetchAll();
$total_rows = $stmt->rowCount();
//$allRecords = mysql_query('select * from article');
//$total_rows = mysql_num_rows($allRecords);
//$base_url = 'https://localhost/pagi/'; //
global $per_page;
$num_links = 4;
$total_rows = $total_rows;
$cur_page = 1;
if(isset($_GET['page']))
{
$cur_page = $_GET['page'];
$cur_page = ($cur_page < 1)? 1 : $cur_page; //if page no. in url is less then 1 or -ve
}
$offset = ($cur_page-1)*$per_page; //setting offset
$pages = ceil($total_rows/$per_page);
$start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
$end = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;
//$res = mysql_query("SELECT * FROM article ORDER BY id DESC LIMIT ".$per_page." OFFSET ".$offset);
$stm = $db->prepare("SELECT * FROM article ORDER BY id DESC LIMIT ".$per_page." OFFSET ".$offset);
$stm->execute();
$res = $stm->fetchAll();
$rows = $stm->rowCount();
if($rows < 1){
header("Location: news-events.php");
exit;
}
if(is_resource($result))
{
foreach( $res as $row ) {
$desc = $row["ne_article"];
$img = $row['ne_image'];
if(!empty($img)){
$img = '<br/><img src="uploads/images/'.$img.'" alt="" class="responsive-shrink">';
}else{
$img = '';
}
$youtube = $row["ne_youtube"];
if(!empty($youtube)){
$youtube = '<br/><div class="video-container"><iframe src="http://www.youtube.com/embed/'.$youtube.'"></iframe></div>';
}else{
$youtube = '';
}
//shortenString($row['ne_article']);
?>
<h4><?php echo $row['ne_title']; ?></h4>
<h5><b>Views:</b> <?php echo $row['views']; ?>, <b>Posted on:</b> <?php echo format_date($row['created']); ?></h5>
<?php echo $img; ?>
<?php echo $youtube; ?>
<p>
<br/><?php echo bbcode(nl2br(shortenString($desc))); ?>
</p>
<div id="pagelink">
Read more...</button>
</div>
<?php
}
}
?>
<div id="pagination">
<div id="pagiCount">
<br/><br/>
<?php
if(isset($pages))
{
if($pages > 1)
{ if($cur_page > $num_links) // for taking to page 1 //
{ $dir = "First";
echo '<span id="prev"> '.$dir.' </span>';
}
if($cur_page > 1)
{
$dir = "Prev";
echo '<span id="prev"> '.$dir.' </span>';
}
for($x=$start ; $x<=$end ;$x++)
{
echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':''.$x.' ';
}
if($cur_page < $pages )
{ $dir = "Next";
echo '<span id="next"> '.$dir.' </span>';
}
if($cur_page < ($pages-$num_links) )
{ $dir = "Last";
echo ''.$dir.' ';
}
}
}
?>
would really appreciate your time and help
You are doing nearly everything wrong. And you did with old mysql as well
To get the number of records in database you should never select all the rows, nor fetch them. You have to select the count already.
$total_rows = $pdo->query("SELECT count(1) FROM article")->fetchColumn();
is how it have to be done with PDO.
Besides, for the other query you should use prepared statements.
Finally, you have to take out all the useless verifications and start with foreach right away:
$stm = $db->prepare("SELECT * FROM article ORDER BY id DESC LIMIT ?,?");
$stm->execute([$offset,$per_page]);
$res = $stm->fetchAll();
foreach( $res as $row ) {
And read up on setting the error mode properly here.
check if you have problem with the quotes... when you echo html code inside ' ' if you have " " and again inside '' like this example echo ' " '' " ' you will have problem. you should use \' in order to avoid these problem here echo ' '.$dir.' ';
I'm using this script to pagination. But when url does have $_GET['word'] I cant change URL's of Links. How can I do it?
<?
if (isset($_GET['page']) ) {
$pageno = $_GET['page'];
} else {
$pageno = 1;
} // if
$limit = "";
if(isset($_GET['word'])) {
$word = mysql_real_escape_string($_GET['word']);
$word = $word{0};
$limit = " WHERE baslik LIKE '$word%'";
}
$query = mysql_query("SELECT count(*) FROM m3_music_mp3" .$limit);
$query_data = mysql_fetch_row($query);
$numrows = $query_data[0];
$rows_per_page = 30;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
} // if
$limit .= " ORDER BY id DESC LIMIT " .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
//$limit = 'ORDER BY id DESC LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = mysql_query("SELECT * FROM m3_music_mp3 $limit");
if ($pageno == 1) {
echo "<a>««</a> <a>«</a> ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?sayfa=1'>««</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?sayfa=$prevpage'>«</a> ";
} // if
for($page_number = 1; $page_number <= $lastpage; $page_number++)
if($page_number == $pageno) {
echo "<span class='current'>$pageno</span>";
}
else {
echo "<a href='{$_SERVER['PHP_SELF']}?sayfa=$page_number' class='page' title='$page_number'>$page_number</a>";
}
if ($pageno == $lastpage) {
echo " <a>»</a> <a>»»</a> ";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?sayfa=$nextpage'>»</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?sayfa=$lastpage'>»»</a> ";
} // if
?>
I'm trying to do if isset $_GET['word'] set urls <a href='{$_SERVER['PHP_SELF']}?word=$word&page=$lastpage'>»»</a> if NOT <a href='{$_SERVER['PHP_SELF']}?page=$lastpage'>»»</a> i cant let it work thank you if you will help
Try following:
$word_str = "";
if(isset($_GET['word'])) {
$word_str = "&word=".$_GET['word'];
//......
}
And then just append $word_str to urls, for example
echo " <a href='{$_SERVER['PHP_SELF']}?sayfa='.$nextpage.$word_str.'>»</a> ";
You should better use $_SERVER['REQUEST_URI'] to get the current requested URI path and query. You can then use parse_url to get just the URI path and http_build_query to build the query from an array:
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// …
$href = htmlspecialchars($_SERVER['REQUEST_URI_PATH']."?".http_build_query(array('sayfa'=>1)+$_GET), ENT_QUOTES);
echo " <a href='$href'>««</a> ";
$prevpage = $pageno-1;
$href = htmlspecialchars($_SERVER['REQUEST_URI_PATH']."?".http_build_query(array('sayfa'=>$prevpage)+$_GET), ENT_QUOTES);
echo " <a href='$href'>«</a> ";
// …