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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)