Jump to content

Problem reusing limits method...


cgm225

Recommended Posts

I have the following class, and I am having the following problem...  If I use the findTotalPages() before the setPageLimits(), the new limits set by the setPageLimits() method are not set, but, rather, the limits stay at 0 and 10000 as set by the findTotalPages().

 

How can I re-set the limits anytime I want with the setPageLimits() or setLimits() methods?

 

Thanks in advance!

 

 


class Gallery {

    public function setNumberDisplayed($numberDisplayed) {
        $this->numberDisplayed = $numberDisplayed;
    }
    
    public function setPageLimits() {
        $this->startingEntry = ($this->page - 1) * $this->numberDisplayed;
        $this->endingEntry   = $this->numberDisplayed;

        $this->setLimits($this->startingEntry, $this->endingEntry);
    }
    
    /* Sets limits for prepared statement. If limits are not manually set, then
     * extreme default values are used.
     */
    public function setLimits($skip, $numberRows) {  
        $this->skip = $skip;
        $this->numberRows = $numberRows;        
    }
        
    public function findTotalPages() {
        
        //Sets exceedingly wide limits for query
        $this->setLimits(0, 10000);
        
        //Calculates and sets total number of pages
        $this->totalPages = ceil(count($this->findAlbums()) / $this->numberDisplayed);
                
        //Returns total number of pages
        return $this->totalPages;   
    }
    
    public function findAlbums() {      
        /* Queries database and creates a two dimensional array of all albums
         * and each album's associated data.
         */ 
        $query = 'SELECT id, full_title, short_title, dir, timestamp
                    FROM albums ORDER BY timestamp DESC
                    LIMIT ?, ?';
        $statement = $this->connection->prepare($query);
        $statement->bind_param('ii', $this->skip, $this->numberRows);
        $statement->bind_result($id, $fullTitle, $shortTitle, $dir, $timestamp);
        $statement->execute();
        while($statement->fetch()) {
            $this->albumData[$dir] = array(
                'id'         => $id,
                'fullTitle'  => $fullTitle,
                'shortTitle' => $shortTitle,
                'dir'        => $dir,
                'timestamp'  => $timestamp
            );
        }

        /* If a dirctory name has been provided via a GET variable, then only
         * the album corresponding to that directory name will be returned in the
         * array.
         */
        if(isset($this->dir)) {
            $this->findAlbum();
            return $this->albumData;
        } else {
            return $this->albumData;
        }
    }

}

Link to comment
https://forums.phpfreaks.com/topic/108091-problem-reusing-limits-method/
Share on other sites


class Gallery {
    
    //Declaring variables
    private $connection;
    private $dir;
    private $item;
    private $albumData = array();
    private $photoData = array();
    private $videoData = array();
    
    protected $skip;
    protected $numberRows;
    
    //Class constructor method
    public function __construct(mysqli $connection) {
        //Sets MySQLi object
        $this->connection = $connection;
        $this->getGetData();
    }
    
    public function getGetData() {
        /* Checks for the GET variables $dir and $image, and, if present,
         * strips them down with a regular expression function with a white
         * list of allowed characters, removing anything that is (1) not a
         * letter, number, underscore or hyphen, or (2) anything that is not a
         * number.
         */      
        $regex1  = '/[^-_A-z0-9]++/'; //Allowed characters: letter, number, underscore or hyphen
        $regex2  = '/[^0-9]++/';      //Allowed characters: numbers
        
        $this->dir = isset($_GET['dir']) ? preg_replace($regex1, '', $_GET['dir']) : null;
        $this->image = isset($_GET['image']) ? preg_replace($regex2, '', $_GET['image']) : null;
        
        $this->page = isset($_GET['page']) ? preg_replace($regex2, '', $_GET['page']) : null;
    }
    
    public function setNumberDisplayed($numberDisplayed) {
        $this->numberDisplayed = $numberDisplayed;
    }
    
    public function setPageLimits() {
        $this->startingEntry = ($this->page - 1) * $this->numberDisplayed;
        $this->endingEntry   = $this->numberDisplayed;

        $this->setLimits($this->startingEntry, $this->endingEntry);
    }
    
    /* Sets limits for prepared statement. If limits are not manually set, then
     * extreme default values are used.
     */
    public function setLimits($skip, $numberRows) {  
        $this->skip = $skip;
        $this->numberRows = $numberRows;        
    }
        
    public function findTotalPages($find = null) {

        //Sets exceedingly wide limits for query
        $this->setLimits(0, 10000);
        
        //Calculates and sets total number of pages
        $this->totalPages = ceil(count($find) / $this->numberDisplayed);
        
        //Returns total number of pages
        echo $this->totalPages;  
        return $this->totalPages;   
    }
    
    public function findAlbums() {      
        /* Queries database and creates a two dimensional array of all albums
         * and each album's associated data.
         */ 
        $query = 'SELECT id, full_title, short_title, dir, timestamp
                    FROM albums ORDER BY timestamp DESC
                    LIMIT ?, ?';
        $statement = $this->connection->prepare($query);
        $statement->bind_param('ii', $this->skip, $this->numberRows);
        $statement->bind_result($id, $fullTitle, $shortTitle, $dir, $timestamp);
        $statement->execute();
        while($statement->fetch()) {
            $this->albumData[$dir] = array(
                'id'         => $id,
                'fullTitle'  => $fullTitle,
                'shortTitle' => $shortTitle,
                'dir'        => $dir,
                'timestamp'  => $timestamp
            );
        }
        $statement->close(); 
//print_r($this->albumData);
//echo "<br><br>";

        /* If a dirctory name has been provided via a GET variable, then only
         * the album corresponding to that directory name will be returned in the
         * array.
         */
        if(isset($this->dir)) {
            $this->findAlbum();
            return $this->albumData;
        } else {
            return $this->albumData;
        }
    }
    
    public function findAlbum() {
        //Returns a specific album and its associated data
        $this->albumData = array($this->dir => $this->albumData[$this->dir]);
        return $this->albumData;
    }

    public function getAlbumDir() {
        return $this->albumData[$this->dir]['dir'];
    }
    
    public function findImages() {
        /* Queries database and creates a two dimensional array of all images
         * and each images's associated data.
         */ 
        $query = 'SELECT id, filename, caption, date, location, album, timestamp
                    FROM images WHERE album = ? ORDER BY filename ASC
                    LIMIT ?, ?';
        $statement = $this->connection->prepare($query);
        $statement->bind_param('iii', $this->albumData[$this->dir]['id'], $this->skip, $this->numberRows);
        $statement->bind_result($id, $filename, $caption, $date, $location, $album, $timestamp);
        $statement->execute();
        while($statement->fetch()) {
            $this->imageData[$id] = array(
                'id'        => $id,
                'filename'  => $filename,
                'caption'   => $caption,
                'date'      => $date,
                'location'  => $location,
                'album'     => $album,
                'timestamp' => $timestamp
            );
        }
        $statement->close();
//print_r($this->imageData);
//echo "<br><br>";
        /* If an image number has been provided via a GET variable, then only
         * the image corresponding to that number name will be returned in the
         * array.
         */
        if(isset($this->image)) {
            $this->findImage();
        } else {
            return $this->imageData;
        }

    }
    
    public function findImage() {
        /* Finds image from array based on the image variable retrieved with GET
         * from the getGetData() method minus one because array keys start at
         * zero.
         */
        $itemArrayPosition = $this->image - 1;
        $this->imageData = array_values($this->imageData);
        
//print_r($this->imageData[$itemArrayPosition]);
//echo "<br><br>";

        return $this->imageData[$itemArrayPosition];
    }
    
    public function findVideos() {
        /* Queries database and creates a two dimensional array of all videos
         * and each video's associated data.
         */
        $query = 'SELECT id, filename, title, album, timestamp
                    FROM videos WHERE album = ? ORDER BY filename ASC
                    LIMIT ?, ?';
        $statement = $this->connection->prepare($query);
        $statement->bind_param('iii', $this->albumData[$this->dir]['id'], $this->skip, $this->numberRows);
        $statement->bind_result($id, $filename, $title, $album, $timestamp);
        $statement->execute();
        while($statement->fetch()) {
            $this->videoData[$id] = array(
                'id'        => $id,
                'filename'  => $filename,
                'title'     => $title,
                'album'     => $album,
                'timestamp' => $timestamp
            );
        }
        $statement->close();
//print_r($this->videoData);
//echo "<br><br>";
        return $this->videoData;
    } 
}

For each bound variable in bind_param(), there should be another letter in the format string that specifies how the variable should be handled. e.g.

 

$stmt->bind_param('s', $foo);

$stmt->bind_param('si', $foo, $bar);

$stmt->bind_param('sid', $foo, $bar, $baz);

 

The bind types let the mysqli extension know how to encode the data that it sends for greater efficiency.

 

The type definitions are very simple: data in the bound variables will be treated as an integer value, a rational number (double) or a string.

 

There is also a special type that allows long blobs to be sent to the MySQL server in chunks.

 

Basically, I want to be able to (1) find the total number of entries/rows so I can calculate the number of pages, but then later (2) simply get the results I need for that particular page number.

 

I was trying to do that by changing the query limits as needed, but I am having problems re-setting the limits (they always seem to keep the value initial set to them)

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.