2016-07-11 2 views
0

프로젝트에서 페이지 매김을하고 있습니다. 내 mysqli_query 클래스 외부에서 제대로 작동하지만 클래스 함수 내에서 작동하지 않았다 .. 내 코드에서 getTotalRows()를 참조하십시오. 나에게 도움말 친구가 ..클래스 함수 내에서 mysqli_query가 작동하지 않았지만 클래스 외부에서 올바르게 작동합니다.

<?php 

include('config_session.php'); 

// Here is my query is ececute successfully. 

echo $result = mysqli_query($conn,"select * from tbl_subcategory order by subcat_pos ") or die("failed"); 

class CSSPagination 
{ 
    private $totalrows; 
    private $rowsperpage; 
    private $website; 
    private $page; 
    private $sql; 

    public function __construct($sql, $rowsperpage, $website) 
    { 


      $this->sql = $sql; 

     $this->website = $website; 
     $this->rowsperpage = $rowsperpage; 
    } 

    public function setPage($page) 
    { 
     if (!$page) { $this->page=1; } else { $this->page = $page; } 
    } 

    public function getLimit() 
    { 
     return ($this->page - 1) * $this->rowsperpage; 
    } 

    private function getTotalRows() 
    { 

      // Here is my query is didn't execute. Only echo failed message 

      $result = mysqli_query($conn,"select * from tbl_subcategory order by subcat_pos ") or die("failed"); 



     $this->totalrows = mysqli_num_rows($result); 
    } 

    private function getLastPage() 
    { 
     return ceil($this->totalrows/$this->rowsperpage); 
    } 

    public function showPage() 
    { 
     $this->getTotalRows(); 

     $pagination = ""; 
     $last = $this->getLastPage(); 
     $lpm1 = $this->getLastPage() - 1; 
     $page = $this->page; 
     $prev = $this->page - 1; 
     $next = $this->page + 1; 

     $pagination .= "<div class=\"pagination\""; 
     if($margin || $padding) 
     { 
      $pagination .= " style=\""; 
      if($margin) 
       $pagination .= "margin: $margin;"; 
      if($padding) 
       $pagination .= "padding: $padding;"; 
      $pagination .= "\""; 
     } 
     $pagination .= ">"; 

     if ($this->getLastPage() > 1) 
     { 
      if ($page > 1) 
       $pagination .= "<a href=$this->website&page=$prev>« prev</a>"; 
      else 
       $pagination .= "<span class=\"disabled\">« prev</span>"; 


      if ($this->getLastPage() < 9) 
      { 
       for ($counter = 1; $counter <= $this->getLastPage(); $counter++) 
       { 
        if ($counter == $page) 
         $pagination .= "<span class=\"current\">".$counter."</span>"; 
        else 
         $pagination .= "<a href=$this->website&page=$counter>".$counter."</a>";     
       } 
      } 
      elseif($this->getLastPage() >= 9) 
      { 
       if($page < 4)  
       { 
        for ($counter = 1; $counter < 6; $counter++) 
        { 
         if ($counter == $page) 
          $pagination .= "<span class=\"current\">".$counter."</span>"; 
         else 
          $pagination .= "<a href=$this->website&page=$counter>".$counter."</a>";     
        } 
        $pagination .= "..."; 
        $pagination .= "<a href=$this->website&page=$lpm1>".$lpm1."</a>"; 
        $pagination .= "<a href=$this->website&page=$last>".$last."</a>";  
       } 
       elseif($last - 3 > $page && $page > 1) 
       { 
        $pagination .= "<a href=$this->website&page=1>1</a>"; 
        $pagination .= "<a href=$this->website&page=2>2</a>"; 
        $pagination .= "..."; 
        for ($counter = $page - 1; $counter <= $page + 1; $counter++) 
        { 
         if ($counter == $page) 
          $pagination .= "<span class=\"current\">".$counter."</span>"; 
         else 
          $pagination .= "<a href=$this->website&page=$counter>".$counter."</a>";     
        } 
        $pagination .= "..."; 
        $pagination .= "<a href=$this->website&page=$lpm1>$lpm1</a>"; 
        $pagination .= "<a href=$this->website&page=$last>".$last."</a>";  
       } 
       else 
       { 
        $pagination .= "<a href=$this->website&page=1>1</a>"; 
        $pagination .= "<a href=$this->website&page=2>2</a>"; 
        $pagination .= "..."; 
        for ($counter = $last - 4; $counter <= $last; $counter++) 
        { 
         if ($counter == $page) 
          $pagination .= "<span class=\"current\">".$counter."</span>"; 
         else 
          $pagination .= "<a href=$this->website&page=$counter>".$counter."</a>";     
        } 
       } 
      } 

     if ($page < $counter - 1) 
      $pagination .= "<a href=$this->website&page=$next>next »</a>"; 
     else 
      $pagination .= "<span class=\"disabled\">next »</span>"; 
     $pagination .= "</div>\n";   
     } 

     return $pagination; 
    } 
} 
?> 
+0

코드 블록에 코드를 넣어하거나 neatening 고려 당신에게 친구를 감사 . 당신의 스 니펫을 따라 가기가 아주 어렵습니다. – vsharper

+0

어떻게 getTotalRows()를 호출하고 있습니까? – jeff

+0

'getTotalRows()'에서 쿼리를 할 때 정의되지 않은 변수'$ conn'를 사용하고 있습니다. 함수 및 메서드는 전역 네임 스페이스 또는 다른 함수 또는 메서드에서 만든 변수에 액세스 할 수 없습니다. '$ conn' 변수를 클래스 생성자에 전달해야합니다 (그리고'$ sql'과 마찬가지로'$ this-> conn'를 사용하십시오). 또는 메소드 자체에 전달해야합니다. –

답변

0

$ CONN이로 대체 -> $ CONN

이 지금은 잘 작동 ...

관련 문제