1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Pagination

Discussion in 'PHP' started by Jeremy Benson, Nov 24, 2015.

  1. #1
    Hello,

    I'm coming back to a pagination class that DeathShadow helped me construct. I'm working on a site that has ad listings both sponsored and unsponsored. I'm going to list unsponsored ads first and sponsored ads right below. The benefit of sponsoring is that they aren't buried under the other ads.

    I've declared two versions of the paginator, as we're keeping track of two types of output sponsored and not. I've also set them both to calculate based on 20 listings. The page will show 20 sponsored and unsponsored... I want to use both off of one set of numbers.. I'm not sure how that would work...

    I'm thinking output numbers for unsponsored.. and on the last page of that test if there is another page if there isn't then output numbers for sponsored...

    really don't know what to do here..

    Here's how I'm getting the count and results

    
    include('data/sqldata.php');
        include('php/classes/pPaginate.php');
        include('php/classes/pQueryBuilder.php');
       
        $country = isset($_GET['country'])?$_GET['country']:'Canada';
        $category = isset($_GET['category'])?$_GET['category']:'community';
        $subcategory = isset($_GET['subcategory'])?$_GET['subcategory']:'';
        $location = isset($_GET['location'])?$_GET['location']:'';
        $search = isset($_GET['search'])?urldecode($_GET['search']):'';
       
        switch($category)
        {
           
            case "community":
                    ////
                    //Pagination for non sponsored ads
                    ////
                // Select count for paginator
                $builder = new pQueryBuilder;
                $builder->append("SELECT COUNT(*) FROM `ad_community` WHERE");
                $builder->add_var('sponsored', 'no');
                $builder->append(' AND ');
                if($builder->add_var('subcategory', $subcategory))
                {
                   
                    $builder->append(" AND ");
                       
                }
                $builder->add_var('country', $country);
                $builder->add_var_like('location', $location);   
                $builder->append(" AND ");
                $builder->add_var_like('search', $search);
                $builder->append("`sponsored` = 'no'");
                $builder->append(" AND ");
                $builder->tidy_query('AND');
                $builder->tidy_query('AND');
                $builder->set_db(array("dsn" => $dsn, "username" => $dbUsername, "password" => $dbPassword));
                $adCount = $builder->fetch_column();
                $builder->reset();
               
                // Paginator for page (unsponsored ads)
                $paginator = new pPaginate(20, 20, $adCount, isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 0);
               
                $limit = $paginator->return_limit();
                $offset = $paginator->return_offset();
               
                $builder->append("SELECT `title`, `timePosted`, `username`, `token` FROM `ad_community` WHERE");
                $builder->add_var_bind('sponsored', 'no');
                $builder->append(" AND ");
                if($builder->add_var_bind('subcategory', $subcategory))
                {
                   
                    $builder->append(" AND");   
                       
                }
                $builder->add_var_bind('country', $country);
                $builder->add_var_like_bind('location', $location);   
                $builder->append(" AND");
                $builder->add_var_like_bind('search', $search);
                $builder->append(" AND");
                $builder->tidy_query('AND');
                $builder->tidy_query('AND');
                $builder->add_single_var_bind(':offset', $paginator->return_offset());
                $builder->add_single_var_bind(':limit', $paginator->return_limit());
                $builder->append("ORDER BY `timePosted` DESC LIMIT :offset, :limit");
                $results = $builder->fetch_all_bind();
           
                $builder->reset();
           
                /////
                //Pagination for sponsored ads
                /////
               
                $builder->append("SELECT COUNT(*) FROM `ad_community` WHERE");
                if($builder->add_var('subcategory', $subcategory))
                {
                   
                    $builder->append(" AND ");
                       
                }
                $builder->add_var('country', $country);
                $builder->add_var_like('location', $location);   
                $builder->append(" AND ");
                $builder->add_var_like('search', $search);
                $builder->append("`sponsored` = 'yes'");
                $builder->append(" AND ");
                $builder->tidy_query('AND');
                $builder->tidy_query('AND');
                $builder->set_db(array("dsn" => $dsn, "username" => $dbUsername, "password" => $dbPassword));
                $adSponsoredCount = $builder->fetch_column();
                $builder->reset();
               
                // Paginator for page (unsponsored ads)
                $sponsorPaginator = new pPaginate(20, 20, $adSponsoredCount, isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 0);
               
                $limit = $sponsorPaginator->return_limit();
                $offset = $sponsorPaginator->return_offset();
               
                $builder->append("SELECT `title`, `timePosted`, `username`, `token` FROM `ad_community` WHERE");
                $builder->add_var_bind('sponsored', 'yes');
                $builder->append(" AND");
                if($builder->add_var_bind('subcategory', $subcategory))
                {
                   
                    $builder->append(" AND");   
                       
                }
                $builder->add_var_bind('country', $country);
                $builder->add_var_like_bind('location', $location);   
                $builder->append(" AND ");
                $builder->add_var_like_bind('search', $search);
                $builder->append(" AND ");
                $builder->tidy_query('AND');
                $builder->tidy_query('AND');
                $builder->add_single_var_bind(':offset', $paginator->return_offset());
                $builder->add_single_var_bind(':limit', $paginator->return_limit());
                $builder->append("ORDER BY `timePosted` DESC LIMIT :offset, :limit");
               
                $sponsoredResults = $builder->fetch_all_bind();;
               
                // END COMMUNITY CASE
            break;
           
        }
    
    PHP:
    Here's how I output the results
    
    switch($category)
                        {
                           
                            case "community":
                                foreach($results as $key => $val)
                                {
                                   
                                    echo '<div class="thread" style="background-color:#e5e5ff; width:80%; margin-left:0.8em;">
                                                ';echo date('D-M-d', $results[$key]['timePosted']);
                                        echo'
                                            </div>
                                            <div id="results" class="listing">
                                                <p class="ad-listing" style="margin-left:0.8em;">';
                                                    echo $results[$key]['username'] . ': ' . '<a href="ad.php?token='.$results[$key]['token'].'">' . $results[$key]['title'] . '</a>';
                                    echo '</p>
                                         </div>';
                                   
                                }
                               
                                /////
                                // Output for sponsored ads
                                /////
                               
                                foreach($sponsoredResults as $key => $val)
                                {
                                   
                                    echo '<div class="thread" style="background-color:#ffffb3; width:80%; margin-left:0.8em;">
                                                ';echo date('D-M-d', $sponsoredResults[$key]['timePosted']);
                                        echo'
                                            </div>
                                            <div id="results" class="listing">
                                                <p class="ad-listing" style="margin-left:0.8em;">';
                                                    echo $sponsoredResults[$key]['username'] . ': ' . '<a href="ad.php?token='.$sponsoredResults[$key]['token'].'">' . $sponsoredResults[$key]['title'] . '</a>';
                                    echo '</p>
                                         </div>';
                                   
                                }
                               
                                ////
                                //Ouput community ads pagination
                                ////
                               
                               
                               
                            break;
                           
                        }
    
    PHP:
    Pagination class

    
    class pPaginate{
       
        private $totalItems;
        private $currentPage;
        private $totalPages;
        private $itemsPerPage;
        private $offset;
        private $maxShow;
        private $paginationMiddle;
       
        public function __construct($maxShowSet = 5, $itemsPerPageSet = 5, $totalItemsSet, $currentPageSet)
        {
           
            $this->currentPage = $currentPageSet;
            $this->maxShow = $maxShowSet;
            $this->totalItems = $totalItemsSet;
            $this->itemsPerPage = $itemsPerPageSet;
            $this->totalPages = ceil($this->totalItems / $this->itemsPerPage);
            $this->offset = $this->currentPage * $this->maxShow;
            $this->paginationMiddle = floor($this->maxShow / 2);
       
        }
    
        public function return_offset()
        {
           
            return $this->offset;
           
        }
       
        public function return_limit()
        {
           
            return $this->itemsPerPage;
           
        }
       
    function pagination($href, $currentPage) {
       
        if ($this->totalItems > $this->itemsPerPage) {
       
            echo '
                <div class="pagination">
                    <ul>';
               
            $href .= (strpos($href, '?') === FALSE ? '?' : '&amp;') . 'page=';
           
            $lastPage = floor(($this->totalItems - 1) / $this->itemsPerPage);
           
            if ($lastPage > 0) {
                echo '
                        <li><a class="page" href="', $href, '0">First</a></li>';
                if ($currentPage > 0) echo '
                        <li><a class="page"
                            href="', $href, $currentPage - 1,'"
                            title="Previous Page"
                            rel="prev"
                        >&laquo;</a></li>';
            }
           
            if ($lastPage >= $this->maxShow) {
                $counter = ($currentPage <= $this->paginationMiddle) ? 0 : $currentPage - $this->paginationMiddle;   
                $endPage = $counter + $this->maxShow;
                if ($endPage > $lastPage) $endPage = $lastPage;
            } else {
                $counter = 0;
                $endPage = $lastPage;
            }
       
            while ($counter <= $endPage) {
                $noAnchor = ($counter == $currentPage);
                echo '
                        <li>', (
                            $noAnchor ? '<span>' : '<a class="page" href="' . $href . $counter . '">'
                        ), ++$counter , (
                            $noAnchor ? '</span>' : '</a>'
                        ),'</li>';
            }
           
            if ($lastPage > 0) {
           
                if ($currentPage < $lastPage) echo '
                    <li><a class="page"
                        href="', $href, $currentPage + 1, '"
                        title="Next Page"
                        rel="next"
                    >&raquo;</a></li>';
               
                echo '
                    <li><a class="page" href="', $href, $lastPage, '">Last</a></li>';
               
            }
           
            echo '
                    </ul>
                <!-- .pagination --></div>';
           
        }
    
    }
                   
    // end of pagination class
    }
    
    
    PHP:
     
    Jeremy Benson, Nov 24, 2015 IP
  2. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #2
    Will it be that easy? If so I just add an is next page function to test if there is a next page...

    output numbers for unsponsored ads then at the end of that output numbers for sponsored ads if there is pages left on that, but not on unsponsored.

    sort of like

    
    // output numbers for pagination
    if($paginator->isNextPage())
    {
    
          $paginator->pagination();
    
    }elseif(!$paginator->isNextPage() && $sponsoredPaginator->isNextPage())
    {
    
        $sponsoredPaginator->pagination();
    
    }
    
    PHP:
     
    Last edited: Nov 24, 2015
    Jeremy Benson, Nov 24, 2015 IP