Jump to content

Pagination went wrong with WHERE clause


booxvid

Recommended Posts

Been searching for days for this fix already but still can't manage to change function.

 

here's the pagination happened:

<?php require_once("../../includes/initialize.php"); ?>
<?php if (!$session->is_logged_in()) { redirect_to("login.php"); } ?>
<?php
  // Find all the photos
  //$photos = Photograph::find_all();
?>
<?php
$id = isset($_GET['album_id']) ? $_GET['album_id'] : "";

$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;


$per_page = 10;

$total_count = Photograph::count_all_photosin_album($id);


$pagination = new Pagination($page, $per_page, $total_count);

$sql = "SELECT * FROM photo_gallery.photographs WHERE album_id='".$id;
$sql .= "' LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()}";
$photos = Photograph::find_by_sql($sql);


?>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Photo Gallery</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="../stylesheets/bootstrap.css" rel="stylesheet">
     <link href="../stylesheets/main.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
  </head>

  <body>
    <div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <a class="brand" href="index.php">Photo Gallery</a>
          <div class="nav-collapse">
            <ul class="nav">
              <li><a href="index.php">Home</a></li>
              <li><a href="../index.php">Gallery of Photos</a></li>
              <li class="active"><a href="list_photos.php">List of Photos</a></li>
              <li ><a href="manage_album.php">Albums</a></li>
              <li><a href="manage_account.php">Accounts</a></li>
              <li><a href="logfile.php">View Log File</a></li>
      <li><a href="logout.php">Logout</a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container">

<?php echo output_message($message); ?>

	<h2>Album "<?php 
	$album_na = Album::get_album_name($id);
	echo $album_na;
	?>"</h2>
	<br/>

	  <ul class="gallery">
      <?php foreach($photos as $photo): ?>

		      <li>
		      <div class="">
			      <a href="photo.php?id=<?php echo $photo->id; ?>">
				      <?php
					  echo '<span></span>
							<img src="../images/'.$photo->filename.'"alt="image"/>';
				      ?>
			      </a>
		      </div></li>
	  

      <?php endforeach; ?>
      </ul>
      
	<br />

	<div id="number_of_photos">
		<?php 
			echo "There are <font color='red'>" . $total_count. "</font> photos";

		?>
	</div>
	<br/>
	<div id="pagination" style="clear: both;">
	<?php
		if($pagination->total_pages() > 1) {

			if($pagination->has_previous_page()) { 
		echo "<a href=\"photos_album.php?page=";
	      echo $pagination->previous_page();
	      echo "\">« Previous</a> "; 
	    }

			for($i=1; $i <= $pagination->total_pages(); $i++) {
				if($i == $page) {
					echo " <span class=\"selected\">{$i}</span> ";
				} else {
					echo " <a href=\"photos_album.php?page={$i}\">{$i}</a> "; 
				}
			}

			if($pagination->has_next_page()) { 
				echo " <a href=\"photos_album.php?page=";
				echo $pagination->next_page();
				echo "\">Next »</a> "; 
	    }

		}

	?>
	</div><br/>
    </div>

<?php include_layout_template('admin_footer.php'); ?>

 

my pagination class:

<?php

class Pagination {

  public $current_page;
  public $per_page;
  public $total_count;

  public function __construct($page=1, $per_page=20, $total_count=0){
  	$this->current_page = (int)$page;
    $this->per_page = (int)$per_page;
    $this->total_count = (int)$total_count;
  }

  public function offset() {
    return ($this->current_page - 1) * $this->per_page;
  }

  public function total_pages() {
    return ceil($this->total_count/$this->per_page);
}

  public function previous_page() {
    return $this->current_page - 1;
  }
  
  public function next_page() {
    return $this->current_page + 1;
  }

public function has_previous_page() {
	return $this->previous_page() >= 1 ? true : false;
}

public function has_next_page() {
	return $this->next_page() <= $this->total_pages() ? true : false;
}


}

?>

In the first page, it still display the photos on what my query had stated but when I click the next button already it went crazy! \:

Link to comment
https://forums.phpfreaks.com/topic/266833-pagination-went-wrong-with-where-clause/
Share on other sites

Pagination needs to build the pagination links with any existing get parameters in the url or without modifying any existing get parameters in the url, depending on your point of view. See the information I posted in the following thread - http://forums.phpfreaks.com/index.php?topic=363172.0

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.