Am new to php, i am try to use pagenation , where i post a unique id to a page then use that to load content with the respective id then pagenate all the content with that respective id.
Here below is what have been trying
<?php
if (isset($_GET["post"]))
{
$page = $_GET["post"];
} else
{
$page = 1;
};
$limit = 2;
$total_records = $pagination;
$total_pages = ceil($total_records/$limit);
$lastpage = ceil($total_pages/$limit);
$page != 0;
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
//lastpage is = total pages / items per page, rounded up.
if($prev == 0)
$prev = 1;
$next <= $lastpage;
//if no page var is given, default to 1.
$pagLink = "<ul class='pagination pagination-circle' class='justify- content-center'>";
$pagLink .= "<li class='page-item active'>
<a class='page-link black' href='home.php?post=".$prev."' aria-label='Back'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Next</span>
</a>
</li>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li class='page-item active'><a class='page-link' href='home.php?post=".$i."'>".$i."</a></li> ";
};
$pagLink .= "<li class='page-item pg-red active'>
<a class='page-link black' href='home.php?post=".$next."' aria-label='Next'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a>
</li>";
echo $pagLink . "</ul>";
?>
Here is how i want to pagenate contents per id
enter image description here
Related
hello guys can you help me out ?
i have a problem in pagination
i just want to disable the next and previous
button in my pagination if it is
no item left
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "final";
$con= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysqli_select_db($con, "final") or die ("no database");
$pagination_sql = "SELECT * FROM `ongoing` WHERE approved='approve'";
$run_pagination = mysqli_query($con, $pagination_sql);
$count = mysqli_num_rows($run_pagination);
$total_pages = ceil($count/$per_page);
echo "<ul class='pagination'>";
echo "<li class='page-item'><a class='page-link' href='ongoing.php?page=".($page-1)."' class='button'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Previous</span>
</a></li>";
for($i=1;$i<=$total_pages;$i++){
echo'<li><a class="page-link" href="ongoing.php?page='.$i.'">'.$i.'</a></li>';
};
echo "<li class='page-item'><a class='page-link' href='ongoing.php?page=".($page+1)."' class='button'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a></li>";
echo "</ul>";
?>
and here's the condition of per page
$per_page = 10;
if(isset($_GET['page'])){
$page = $_GET['page'];
}else{
$page= 1;
}
$start_from = ($page-1) * $per_page;
I think you just need to adjust your code as below:
echo "<ul class='pagination'>";
if($page == 1) {
$disable_prev = 'disabled';
$prev_url = "javascript:void(0);";
} else {
$disable_prev = '';
$prev_url = "ongoing.php?page=".($page-1);
}
echo "<li class='page-item ".$disable_prev."'><a class='page-link' href='".$prev_url."' class='button'>
<span aria-hidden='true'>«</span>
<span class='sr-only'>Previous</span>
</a></li>";
for($i=1;$i<=$total_pages;$i++){
echo'<li><a class="page-link" href="ongoing.php?page='.$i.'">'.$i.'</a></li>';
};
if($page+1 == $total_pages) {
$disable_next = '';
$next_url = "ongoing.php?page=".($page+1);
} else {
$disable_next = 'disabled';
$next_url = "javascript:void(0);";
}
echo "<li class='page-item ".$disable_next."'><a class='page-link' href='".$next_url."' class='button'>
<span aria-hidden='true'>»</span>
<span class='sr-only'>Next</span>
</a></li>";
echo "</ul>";
Then you can also add some CSS to make disabled li little visible or faded than other
Try:
if($i != NULL){ //NULL or Empty
//Code here
}
I'm fetching records from a database and displaying them in gruops using php and ajax.
The script below (found somewhere) is almost working, but it seems that when you are on the first page and clicking "Next" you are taken to the LAST page.
Only when clicking the numbers individually, the paging is correct.
I can provide a test link in a PM if needed.
index.php:
<script>
$(document).ready(function() {
$("#results" ).load( "pages.php"); //load initial records
//executes code below when user click on pagination links
$("#results").on( "click", ".pagination a", function (e){
e.preventDefault();
var page = $(this).attr("data-page"); //get page number from link
$("#results").load("pages.php",{"page":page}, function(){ //get content from PHP page
});
});
});
</script>
<div id="results"></div>
pages.php:
<?php
// continue only if $_POST is set and it is a Ajax request
if (isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
include (realpath(__DIR__ . '/../db.php'));
// Get page number from Ajax POST
if (isset($_POST["page"])) {
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
if (!is_numeric($page_number)) {
die('Invalid page number!');
}
}
else {
$page_number = 1; //if there's no page number, set it to 1
}
// get total number of records from database for pagination
$sql = "SELECT * FROM " . $DBtable . " ORDER BY dates DESC";
$rs = $conn->query($sql);
if ($rs === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
}
else {
$get_total_rows = $rs->fetch_row(); //hold total records in variable
}
// break records into pages
$total_pages = ceil($get_total_rows[0] / $item_per_page);
// get starting position to fetch the records
$page_position = (($page_number - 1) * $item_per_page);
// Limit our results within a specified range.
$rs = $conn->prepare("SELECT id, title, description, dates, clicks FROM " . $DBtable . " ORDER BY dates DESC LIMIT $page_position, $item_per_page");
$rs->execute(); //Execute prepared Query
$rs->bind_result($id, $title, $description, $dates, $clicks); //bind variables to prepared statement
echo '<ul class="contents">';
while($rs->fetch()){ //fetch values
echo '<li>';
echo $id. '. <strong>' .$name.'</strong> — '.$description;
echo '</li>';
}
echo '</ul>';
echo '<div align="center">';
/* We call the pagination function here to generate Pagination link for us. */
echo paginate_function($item_per_page, $page_number, $get_total_rows[0], $total_pages);
echo '</div>';
exit;
}
// ############### pagination function #########################################
function paginate_function($item_per_page, $current_page, $total_records, $total_pages) {
$pagination = '';
if ($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages) { //verify total pages and current page number
$pagination.= '<ul class="pagination">';
$right_links = $current_page + 3;
$previous = $current_page - 3; //previous link
$next = $current_page + 1; //next link
$first_link = true; //boolean var to decide our first link
if ($current_page > 1) {
$previous_link = ($previous == 0) ? 1 : $previous;
$pagination.= '<li class="class="page-item" first"><a class="page-link" href="#" data-page="1" title="First">«</a></li>'; //first link
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $previous_link . '" title="Prev"><</a></li>'; //previous link
for ($i = ($current_page - 2); $i < $current_page; $i++) { //Create left-hand side links
if ($i > 0) {
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $i . '" title="Page' . $i . '">' . $i . '</a></li>';
}
}
//set first link to false
$first_link = false;
}
if ($first_link) { //if current active page is first link
$pagination.= '<li class="page-item active first"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
elseif ($current_page == $total_pages) { //if it's the last active link
$pagination.= '<li class="page-item active last"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
else { //regular current link
$pagination.= '<li class="page-item active"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
for ($i = $current_page + 1; $i < $right_links; $i++) { //create right-hand side links
if ($i <= $total_pages) {
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $i . '" title="Page ' . $i . '">' . $i . '</a></li>';
}
}
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $i;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}
$pagination.= '</ul>';
}
//return pagination links
return $pagination;
}
// Free memory
$rs->free();
// Close connection
$conn->close();
?>
This is the current parsed html of the pager:
<ul class="pagination">
<li class="page-item active first"><a class="page-link" href="#">1<span class="sr-only">(current)</span></a></li>
<li class="page-item"><a class="page-link" href="#" data-page="2" title="Page 2">2</a></li>
<li class="page-item"><a class="page-link" href="#" data-page="3" title="Page 3">3</a></li>
<li class="page-item"><a class="page-link" href="#" data-page="4" title="Next">></a></li>
<li class="page-item last"><a class="page-link" href="#" data-page="4" title="Last">»</a></li>
</ul>
just try like below in your paginate_function()
$next_link = ($i > $total_pages) ? $total_pages : $i;
to
$next_link = (($current_page + 1) > $total_pages) ? $total_pages : $current_page + 1;
Please make changes in paginate_function function as below
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $i;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}
Change this to
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $next;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}
I am using ajax to call the questions from my mysql table to show them in pagination form but i am having trouble calling them according the subject and category and show them in the pagination form.
Here is my code .
<?php
print_r($_GET);
//echo $_GET['val12'];
$X=$_GET['val12'];
$category=$_GET['catg12'];
//print_r($_GET);
?>
<?php
//session_start();
function __autoload($classname)
{
include "admin_panel/$classname.php";
}
$obj = new connect();
$st=$obj->con();
$user= new questionclass();
$results=$user->getQuestions($_POST);
//print_r($_POST);
extract($_POST);
//$_SESSION['category']=$category;
//$key_id=$_SESSION['key'];
$qry = "select * from categories where category_name='$category'";
$run =mysqli_query($st,$qry);
$row = mysqli_fetch_assoc($run);
$testname=$row['test_name'];
$subject_name=explode(',', $testname);
?>
<div class="loading-div"></div>
<div id="results"><!-- content will be loaded here --></div>
<?php
{ //include config file
//Get page number from Ajax POST
if(isset($_POST["pagenum"])){
$page_number = filter_var($_POST["pagenum"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
}else{
$page_number = 1; //if there's no page number, set it to 1
}
$item_per_page=3;
//get total number of records from database for pagination
$results = $st->query("SELECT COUNT(*) FROM question");
$get_total_rows = $results->fetch_row(); //hold total records in variable
//break records into pages
$total_pages = ceil($get_total_rows[0]/$item_per_page);
//get starting position to fetch the records
$page_position = (($page_number-1) * $item_per_page);
//echo $page_position;
foreach($subject_name as $values ) {
$values=trim($values);
}
//Limit our results within a specified range.
$sel = $st->prepare("SELECT quesname, answer1,answer2,answer3,answer4,correctanswer FROM question where category_name ='$category' && subject_name ='$X' ORDER BY id ASC LIMIT $page_position, $item_per_page");
$sel->bind_result($quesname,$answer1,$answer2,$answer3,$answer4,$correctanswer); //bind variables to prepared statement
$sel->execute(); //Execute prepared Query
//$qrey=mysqli_query($st,$sel);
//print_r($sel);
$x=1;
while($rowin=mysqli_fetch_assoc($sel)){
//Display records fetched from database.
echo '<ul class="contents">';
while($sel->fetch()){ //fetch values
?>
<div class='cont' id="<?php echo $x; ?>">
<?php
echo "<div id='question_splitter_$x'>";?>
<div id='subject_name<?php echo $x;?>' >
<p class='questions' id="qname<?php echo $x;?>"> <?php echo $x?>.<?php echo $rowin['quesname'];?></p>
<input type="radio" value="1" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer1'];?>
<br/>
<input type="radio" value="2" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer2'];?>
<br/>
<input type="radio" value="3" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer3'];?>
<br/>
<input type="radio" value="4" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/><?php echo $rowin['answer4'];?>
<br/>
<input type="radio" checked='checked' style='display:none' value="smart_quiz" id='radio1_<?php echo $rowin['id'];?>' name='<?php echo $rowin['id'];?>'/>
<br/>
</div>
</div>
<?php
echo '<li>';
echo $quesname. '. <strong>' .$answer1.'</strong> — '. '. <strong>' .$answer2.'</strong> — '. '. <strong>' .$answer3.'</strong> — '. '. <strong>' .$answer4.'</strong> — '.$correctanswer;
echo '</li>';
}
echo '</ul>';
echo '<div align="center">';
/* We call the pagination function here to generate Pagination link for us.
As you can see I have passed several parameters to the function. */
echo paginate_function($item_per_page, $page_number, $get_total_rows[0], $total_pages);
echo '</div>';
exit;
}
}
################ pagination function #########################################
function paginate_function($item_per_page, $current_page, $total_records, $total_pages)
{
$pagination = '';
if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number
$pagination .= '<ul class="pagination">';
$next = $current_page + 1; //next link
$right_links = $current_page + 3;
$previous = $current_page - 3; //previous link
$first_link = true; //boolean var to decide our first link
if($current_page > 1){
$previous_link = ($previous==0)? 1: $previous;
$pagination .= '<li class="first">«</li>'; //first link
$pagination .= '<li><</li>'; //previous link
for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links
if($i > 0){
$pagination .= '<li>'.$i.'</li>';
}
}
$first_link = false; //set first link to false
}
if($first_link){ //if current active page is first link
$pagination .= '<li class="first active">'.$current_page.'</li>';
}elseif($current_page == $total_pages){ //if it's the last active link
$pagination .= '<li class="last active">'.$current_page.'</li>';
}else{ //regular current link
$pagination .= '<li class="active">'.$current_page.'</li>';
}
for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links
if($i<=$total_pages){
$pagination .= '<li>'.$i.'</li>';
}
}
if($current_page < $total_pages){
$next_link = ($i > $total_pages) ? $total_pages : $i;
$pagination .= '<li>></li>'; //next link
$pagination .= '<li class="last">»</li>'; //last link
}
$pagination .= '</ul>';
}
return $pagination; //return pagination links
}
?>
before using pagination i was using this code to call all the question
$sel="select * from question where category_name='$category' && subject_name='$X'";
$qrey=mysqli_query($st,$sel);
print_r($sel);
$x=1;
This is the query for getting the category and the subject from the page.
hi im trying to create a movie website.i've done almost everything but.there's an issue.this is a part of the load_data.php file that displays the movie links from the db
$query_pag_num = "SELECT COUNT(*) AS count FROM videos";
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);
and this is where movies are displayed
<ul class="nav nav-tabs responsive" id="myTab">
<li class="active"><a class="deco-none misc-class" href="#videos">All</a></li>
<li>Action</li>
<li>Adventure</li>
<li>Animation</li>
<li>Horror</li>
<li>Crime</li>
<li>Comedy</li>
<li>Romance</li>
<li>Fantasy</li>
<li>Drama</li>
<li>Mystery</li>
<li><a class="deco-none" href="#thriller">Thriller</a></li>
<li class="dropdown">
<a data-toggle="dropdown" href="#">More <span class="caret"></span></a>
<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="dLabel">
<li>War</li>
<li>Science-fiction</li>
<li>Family</li>
</ul>
</li>
</ul>
<div class="tab-content responsive">
<div class="tab-pane fade in active" id="<?php $table='videos'; echo $table ?>">
<div class="row row1">
<div style="margin-top:10px;"></div>
<div align="center" style="padding-bottom:80px" id="loading"></div>
<div id="container">
<div class="data"></div>
<div class="pagination"></div>
</div>
</div>
</div>
<div class="tab-pane fade in" id="action">
<div class="row row1">
</div>
</div>
movies are loaded by jquery & ajax.now everything is working fine because i have the table videos that contains all the movies and im displaying all data from the videos table but what i want to do is that onclick of every category to display the movies of that category.
this is the jquery&ajax that does the job
<script type="text/javascript">
$(document).ready(function(){
function loading_show(){
$('#loading').html("<i class='fa fa-cog fa-spin fa-5x'>
</i>").fadeIn('fast');
}
function loading_hide(){
$('#loading').fadeOut('fast');
}
function loadData(page){
loading_show();
$.ajax
({
type: "POST",
url: "load_data.php",
data: "page="+page,
success: function(msg)
{
$("#container").ajaxComplete(function(event, request,
settings)
{
loading_hide();
$("#container").html(msg);
});
}
});
}
loadData(1); // For first time page load default results
$('#container .pagination li.active').live('click',function(){
var page = $(this).attr('p');
loadData(page);
});
$('#go_btn').live('click',function(){
var page = parseInt($('.goto').val());
var no_of_pages = parseInt($('.total').attr('a'));
if(page != 0 && page <= no_of_pages){
loadData(page);
}else{
alert('Enter a PAGE between 1 and '+no_of_pages);
$('.goto').val("").focus();
return false;
}
});
});
</script>
this is the full load-data.php file
<?php
ob_start();
if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 3;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
include"database.php";
function html2txt($document){
$search = array('#<script[^>]*?>.*?</script>#si','#<[\/\!]*?[^<>]*?>#si','#<style[^>]*?>.*?</style>#siU','#<![\s\S]*?--[ \t\n\r]*>#');
$text = preg_replace($search, '', $document);
return $text;
}
$query_pag_data = "SELECT * from videos order by id desc LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data)) {
$msg .= "<div class='col-sm-6 col-md-4'><div class='thumbnail'><img class='img-responsive' src='images/".html2txt($row['cover'])."'/><div class='caption'><h3>" . html2txt($row['title']) . "</h3><a href='movies.php?path=".html2txt($row['path'])."&title=".html2txt($row['title'])."&description=".html2txt($row['description'])."&trailer_title=".html2txt($row['trailer_title'])."&trailer_url=".html2txt($row['trailer_url'])."&imdb=".html2txt($row['imdb'])."&category_list=videos' class='btn btn-primary' role='button'><i class='fa fa-play-circle fa-1x'></i> Shiko filmin</a></div></div></div>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
/* --------------------------------------------- */
$query_pag_num = "SELECT COUNT(*) AS count FROM videos";
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);
/* ---------------Calculating the starting and endign values for the loop----------------------------------- */
if ($cur_page >= 7) {
$start_loop = $cur_page - 3;
if ($no_of_paginations > $cur_page + 3)
$end_loop = $cur_page + 3;
else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
$start_loop = $no_of_paginations - 6;
$end_loop = $no_of_paginations;
} else {
$end_loop = $no_of_paginations;
}
} else {
$start_loop = 1;
if ($no_of_paginations > 7)
$end_loop = 7;
else
$end_loop = $no_of_paginations;
}
/* ----------------------------------------------------------------------------------------------------------- */
$msg .= "<ul class='pagination'><ul>";
// FOR ENABLING THE FIRST BUTTON
if ($first_btn && $cur_page > 1) {
$msg .= "<li p='1' class='active'>First</li>";
} else if ($first_btn) {
$msg .= "<li p='1' class='inactive'>First</li>";
}
// FOR ENABLING THE PREVIOUS BUTTON
if ($previous_btn && $cur_page > 1) {
$pre = $cur_page - 1;
$msg .= "<li p='$pre' class='active previous'>«</li>";
} else if ($previous_btn) {
$msg .= "<li class='inactive'>«</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {
if ($cur_page == $i)
$msg .= "<li p='$i' class='active'>{$i}</li>";
else
$msg .= "<li p='$i' class='active'>{$i}</li>";
}
// TO ENABLE THE NEXT BUTTON
if ($next_btn && $cur_page < $no_of_paginations) {
$nex = $cur_page + 1;
$msg .= "<li p='$nex' class='active'>»</li>";
} else if ($next_btn) {
$msg .= "<li class='inactive'>»</li>";
}
// TO ENABLE THE END BUTTON
if ($last_btn && $cur_page < $no_of_paginations) {
$msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
} else if ($last_btn) {
$msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>";
}
//$goto = "<input type='text' class='goto' size='1'/><input type='button' id='go_btn' class='go_button' value='Go'/>";
$total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
$msg = $msg . "</ul>"/* . $goto*/ . $total_string . "</div>"; // Content for pagination
echo $msg;
ob_end_flush();
}
You can pass second attribute in function like :-
$('#container .pagination li.active').live('click',function(){
var page = $(this).attr('p');
loadData(page, category_id);
});
I am unable to maintain a GET variable with this pagination script. Was hoping you all could help.
I am using a GET function so the user can choose which categories are displayed. I'm using a pagination script I found online and I am now confronted with this issue, when I choose "page 2" $dynCat no longer is parsed in the URL because the pagination script creates a new URL and uses GET also.
Could someone help me out with using pagination with a variable query? Thank you.
ERROR Notice: Undefined index: dynCat ----/dynCat.php on line 32
I understand why it is undefined, I just don't know how to integrate and maintain the user's Query (i.e. GET variable from URL) with the pagination script. Thanks again.
Page1 index.php (User Selects option)
<?php
//Generate and list Categories include "storescripts/connect_to_mysql.php";
$dynCat = "";
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$listcategory = $info["category"];
$dynCat .=
'
<li>
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a>
</li>
';
}
mysql_close();
?>
<?php echo $dynCat; ?>
Page2 dynCat.php (User Views Selection with Pagination)
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
//Generate and list Categories
include "storescripts/connect_to_mysql.php";
$dynGallery = "";
$dynCat = "";
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$listcategory = $info["category"];
$dynCat .=
'
<li>
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a>
</li>
';
}
mysql_close();
?>
<?php
//Query User Selection & Pagination
include('storescripts/connect_to_mysql.php');
$cat = mysql_escape_string($_GET['dynCat']);
$tableName="products";
$targetpage = "dynCat.php";
$limit = 3;
$query = "SELECT COUNT(*) as num FROM $tableName WHERE category = '$cat'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
$stages = 3;
$page = mysql_escape_string(isset($_GET['page'])) ? mysql_escape_string($_GET['page']) : 0;
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get category Data and Images
$query1 = "SELECT * FROM $tableName WHERE category = '$cat' ORDER BY views DESC LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev#gallery'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>";
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>";
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next#gallery'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
mysql_close();
?>
<?php
$productCount = mysql_num_rows($result); // count the output amount
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynGallery .=
'
<div>
<li>
<a href="product.php?id=' . $id . '#gallery">
<img style="border:#FFF 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="180" height="255" border="1" style="opacity: 0;/></a>
<h4> <a name= ' . $id . ' id= ' . $id . ' value= ' . $id . ' href="product.php?id= ' . $id . '#gallery"> ' . $product_name . ' </a> </h4>
<p>'. $category .'</p>
<p>'. $subcategory .'</p>
<span> $' . $price . '</span>
</li>
</div>
';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<?php include ("../header.php");?>
<?php include ("../menu.php");?>
<a id="shop"></a>
<div class="body">
<div class="sidebar">
<div class="first">
<h2>Dora Designs</h2>
<!-- Dynamic Categories -->
<ul>
<?php echo $dynCat; ?>
</ul>
</div>
</div>
<div class="content">
<div class="figure">
<img src="/images/galleryholder.png" alt=""/>
</div>
<div class="products">
<div class="paging" align = "center">
<a id="gallery"></a>
<? echo $paginate;?>
<? echo '</br>'. $cat;?>
<? echo '</br>'. $total_pages.' Results'; ?>
</div>
<ul>
<?php echo $dynGallery; ?>
</ul>
I hate to dump this much code in the post but I am really at a loss and have never used Pagination before. Any help will be greatly appreciated.
change ever instance of
$targetpage?page=
to
$targetpage?dynCat=$cat&page=
you want 2 copies of $cat one for the db requires escaping with mysql_escape_string() the other for the url with urlencode()
replace the line
$cat = mysql_escape_string($_GET['dynCat']);
with
$cat=urldecode($_GET['dynCat']); //from the url, raw value to display
$cat_mysql = mysql_escape_string($cat); //use in mysql queries
$cat_url=urlencode($cat); //use in the url
then
$targetpage?dynCat=$cat&page=
will now be
$targetpage?dynCat=$cat_url&page=
Try adding dynCat=$cat to all of your links that look like this - <a href='$targetpage?page=...
<a href='$targetpage?dynCat=$capage=...
on a quick search there are approx. 14 of them to change.