Location: PHPKode > scripts > Comments script with pagination > comm/class.GetComm.php
<?php
/* GetComm Class (select comments, logs Admin) - www.coursesweb.net/php-mysql/ */
class GetComm extends BaseCM {
  public $table = 'comments';           // HERE add the mysql table name
  public $rowsperpage = 10;             // HERE set number of comments displayed in the page

  public $range = 3;           // range number of links around the current
  public $currentpage = 1;  // the number pagination of the current page
  protected $startrow;           // the row from which start to select the content
  protected $totalpages = 0;     // number of total pages
  public $page;              // to store the name of the file
  public $comments = '';       // to store the comments of current page
  public $linkspgs = '';      // will contain the pagination links
  public $totalcomm = 0;       // to store total number of comments for current page

  
  // Constructor
  public function __construct($conn_data) {
    parent::__construct($conn_data);        // incluude the parent __construct() instructions

    $this->rowsperpage = CMROWSPAGE;

    // sets the current page address in $page property 
    $this->page = isset($_POST['pg']) ? $_POST['pg'] : $_SERVER['REQUEST_URI'];
    $this->page = urlencode($this->page);

    // if request to Login, or Logout Admin, calls logAdmin() method
    if(isset($_POST['adminname']) || isset($_GET['logout'])) $this->logAdmin();

    $this->setComments();            // sets the comments in $comments
    $this->setLinkspgs();           // sets the pagination links in $linkspgs
  }

  // Select the rows for the current page from the mysql table, or from a received Array. Returns an Array with the rows
  public function getMysqlRows() {
    $reout = false;              // the variable that will be returned

    // SELECT to set the total number of pages ($totalpages)
    $sql = "SELECT COUNT(*) FROM `$this->table` WHERE `page`='$this->page'";

    // perform the query, then Selects the rows
    $resql = $this->sqlExecute($sql);
    // if the $resql contains at least one row, takes and sets $totalpages
    if($this->affected_rows > 0) {
      // sets totalcomm, totalpages, currentpage, and the $startrow
      $this->totalcomm = $resql[0][0];
      $this->totalpages = ceil($this->totalcomm / $this->rowsperpage);
      $this->currentpage = isset($_POST['nrp']) ? intval($_POST['nrp']) : $this->totalpages;
      if($this->currentpage > $this->totalpages) $this->currentpage = $this->totalpages;
      $this->startrow = ($this->currentpage - 1) * $this->rowsperpage;

      // Define the SELECT to get the rows for the current page
      $sql = "SELECT * FROM `$this->table` WHERE `page`='$this->page' ORDER BY `id` ASC LIMIT $this->startrow, $this->rowsperpage";
      $reout = $this->sqlExecute($sql);
    }

    return $reout;
  }

  // method that sets the links
  protected function setLinkspgs() {
    $re_links = '';         // the variable that will contein the links to be added in $linkspgs
    $pag_get = '?pg=';      // the name for the GET value added in URL

    // if $totalpages>0 and totalpages higher or equal to $currentpage
    if($this->totalpages>0 && $this->totalpages >= $this->currentpage) {
      // links to first and previous page, if it isn't the first links-range
      if ($this->currentpage > $this->range) {
        // show << for link to 1st page
        $re_links .= ' <span>'.$this->clsite['first'].' &lt;&lt;</span> ';  
       
        $prevpage = $this->currentpage - 1;          // the number of the previous page
        // show < for link to previous page, if higher then 1
        if($prevpage>1) $re_links .= " <span>".$this->clsite['prev'].' &lt;</span> ';
      }

      // sets the links in the range of the current page
      for($x = ($this->currentpage - $this->range); $x <= ($this->currentpage + $this->range); $x++) {  
        // if it's a number between 0 and last page
        if (($x > 0) && ($x <= $this->totalpages)) {
          // if it's the number of current page, show the number without link, otherwise add link
          if ($x == $this->currentpage) $re_links .= ' [<b>'. $x. '</b>] ';
          else $re_links .= " <span>$x</span> ";
        }
      }

      // If the current page is not final, adds link to next and last page
      if ($this->currentpage != $this->totalpages) {
        $nextpage = $this->currentpage + 1;
        // show > for next page (if higher then $this->range and less then totalpages)
        if($nextpage>$this->range && $nextpage<$this->totalpages) $re_links .= " <span>&gt; ".$this->clsite['next'].'</span> ';
        //  show >> for last page, if higher than $this->range
        if($this->totalpages > $this->range) $re_links .= " <span>&gt;&gt; ".$this->clsite['last']." ( $this->totalpages)</span> ";
      }
    }

    // adds all links into a DIV and store them in $linkspgs property
    if(strlen($re_links)>1) $re_links = '<div class="linkspg">'. $re_links. '</div>';
    $this->linkspgs = $re_links;
  }

  // sets the content with the comments of the current page in $comments, accordin to pagination
  protected function setComments() {
    if($list = $this->getMysqlRows()) {
      $nr_coment = $this->startrow;              // to show the comment number

      for($i=0; $i<$this->affected_rows; $i++) {
        $name = $list[$i]['name'];
        $email = (intval($list[$i]['amail'])===1 || intval($list[$i]['amail'])===3) ? $list[$i]['email'] : '';  // show e-mail only if `amail` is 1 or 3
        $coment = $list[$i]['coment'];
        $site = $list[$i]['site'];
        $data = date('j-M-Y, &\n\b\sp; G:i ', $list[$i]['dt']);
        $id = $list[$i]['id'];
        $ip = $list[$i]['ip'];
        $nr_coment++;

        // Add to name the user's site link
        if (strlen($site)>15) { $name = '<a href="'.$site.'" id="n'.$id.'">'.$name.'</a>'; }

        // Define HTML code, in 2 ways, if logged admin, add Modify, Delete options
        if(isset($_SESSION['adminlog']) && $_SESSION['adminlog']==md5(CMANAME.CMAPASS)) {
          // <span> tag with 'name', for modify
          if (strlen($site)>15) { $span_name = '<span>'.$name.'</span>'; }
          else { $span_name = '<span id="n'.$id.'">'.$name.'</span>'; }

          // html code with comments, for admin
          $this->comments .= "\r\n".'<span id="d'.$id.'" style="display:none;">'.$id.'</span>
           <div class="coms'.($nr_coment%2).'"><div class="n_coms"> &#1440; '.$span_name.' </div><q>- IP: '.$ip.'</q>'.$this->adminMod($nr_coment, $id).
          '<span id="e'.$id.'" class="e_coms">'. $email. '</span>
          <em class="d_coms">'. $data. '</em> &nbsp; &nbsp; &nbsp;
          <br/><div class="c_coms">'. $this->formatBbcode($coment). '</div><div id="c'.$id.'" class="adc_coms">'. $coment. '</div></div><hr class="linie_coms"/>'."\r\n";
        }
        else {    // html code with comments, for visitors
          $this->comments .= "\r\n".'<div class="coms'.($nr_coment%2).'"><div class="n_coms"> &#1440; '.$name.' </div> <div class="nr_coms"> '.$nr_coment.' </div><span class="e_coms">'. $email. '</span><em class="d_coms">'. $data. '</em> &nbsp; &nbsp; &nbsp;
          <br/><div class="c_coms">'. $this->formatBbcode($coment). '</div></div><hr class="linie_coms"/>'."\r\n";
        }
      }
    }
    else $this->comments = '<h3>'.$this->clsite['comments']['nocomm'].'</h3>';
  }

  // Function to add options Modify, Delete
  protected function adminMod($nr_c, $id_c) {
    $modifica = '<div class="nr_coms"><span> <a href="#modify" class="modifyc" title="'.$id_c.'">'.$this->clsite['modify'].'</a> - <input type="checkbox" name="delcomm[]" value="'.$id_c.'" class="delcomm" id="dcm'.$id_c.'" /><label for="dcm'.$id_c.'">'.$this->clsite['delete'].'</label></span></div>';

    return $modifica;
  }

  // login, or logout admmin
  protected function logAdmin() {
    // if received form data to log in admin
    if(isset($_POST['adminname']) && isset($_POST['adminpass'])) {
      if($_POST['adminname']==CMANAME && $_POST['adminpass']==CMAPASS) {
        $_SESSION['adminlog'] = md5(CMANAME.CMAPASS);
      }
      else { echo '<h3 style="color:red;">'.$this->clsite['comments']['eror_logadmin'].'</h3>'; }
    }
    // or, if URL with GET to log out admin
    else if(isset($_GET['logout']) && $_GET['logout']=='adminlog' && isset($_SESSION['adminlog']) && isset($_SESSION['logout_redir'])) {
      unset($_SESSION['adminlog']);
      header('Location: http://'.$_SERVER['HTTP_HOST'].urldecode($_SESSION['logout_redir'])) ;
      exit;
    }
  }
}
Return current item: Comments script with pagination