dp19001 2016-07-11 06:21
浏览 68
已采纳

在类函数中没有运行mysqli_query,但它在类外部正常工作

I am working pagination in my project. My mysqli_query worked properly on outside the class but didn't work inside the class function.. Refer getTotalRows() in my code. Help me friends..

<?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>
";          
        }   

        return $pagination;
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dsvd407787736 2016-07-11 06:45
    关注

    $conn is replaced by this->$conn

    Now it works fine ... Thank you friends

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含