如何与DB静态连接进行分页连接

时间:2015-11-26 12:08:41

标签: php pagination

我为我的产品页面创建了分页!写下代码。我有这个错误

Error code image screenshot

严格标准:非静态方法DB_Connection :: obj_db()不应在第147行的D:\ wamp \ www \ project \ ali \ models \ product.php中静态调用

require_once 'db_connection.php';
require_once 'brand.php';
class Product extends DB_Connection{
public $productID;
public $product_name;
public $description;
public $product_features;
public $unit_price;
public $quantity;
public $view_count;
public $product_image;
public $featured;

public $brand;


public function __construct() {
    $this->brand = new Brand();//composition
}
public function get_product($view_update = FALSE) {

    $obj_db = $this->obj_db();


    $query = "select * "
            . "from products_full "
            . "where productID = '$this->productID'";


    $result = $obj_db->query($query);

    if($obj_db->errno){

        throw new Exception("Select product Error - $obj_db->error -$obj_db->errno");
    }

    if($result->num_rows == 0){
        throw new Exception("Product not found");
    }

    $data = $result->fetch_object();

    if($view_update){
        $data->view_count++;


        $query_update = "update products set "
                . "view_count = view_count+1 "
                . "where `productID` = '$this->productID'";

        $r2 = $obj_db->query($query_update);
        if($obj_db->affected_rows == 0){

        }
    }

        $this->productID = $data->productID;
        $this->product_name = $data->product_name;
        $this->description = $data->description;
        $this->product_features = $data->product_features;
        $this->product_image = $data->product_image;
        $this->unit_price = $data->unit_price;
        $this->quantity = $data->quantity;
        $this->view_count = $data->view_count;
        $this->brand->brandID = $data->brandID;
        $this->brand->brand_name = $data->brand_name;
        $this->brand->brand_image = $data->brand_image;
}    
public static function get_products($start = -1, $count = 0, $type = "all", $brandID = 0) {

    //$obj_db = $this->obj_db();
    $obj_db = self::obj_db();

    $query = "select * from `products` ";

    if($brandID > 0){
        $query .= " where brandID = $brandID";
    }

    $types = array("all", "top", "new");

    if(!in_array($type, $types)){
        $type = "all";
    }
    if($type == "top"){
        $query .= " order by view_count desc";
    }

    if($type == "new"){
        $query .= " order by productID desc";
    }

    if($start > -1 && $count > 0){
        $query .= " limit $start, $count";
    }


    $result = $obj_db->query($query);

    if($obj_db->errno){

        throw new Exception("Select products Error - $obj_db->error -$obj_db->errno");
    }

    if($result->num_rows == 0){
        throw new Exception("Product(s) not found");
    }

    $products = array();
    while($data = $result->fetch_object()){

        $temp = new Product();
        $temp->productID = $data->productID;
        $temp->product_name = $data->product_name;
        $temp->description = $data->description;
        $temp->product_features = $data->product_features;
        $temp->product_image = $data->product_image;
        $temp->unit_price = $data->unit_price;
        $temp->quantity = $data->quantity;
        $temp->view_count = $data->view_count;
        //$temp->brand = TRUE;
        $products[] = $temp;
    } 

    return $products;
}

public static function pagination($item_per_page = 6, $brandID = 0){
    $obj_db = self::obj_db();

    $query = "select count(*) 'count' from `products`";//alias

    if($brandID > 0){
        $query .= " where brandID = $brandID";
    }

    $result = $obj_db->query($query);


    if($result->num_rows == 0){
        throw new Exception("Product(s) not found");
    }

    $data = $result->fetch_object();
    $total_items = $data->count;

    $page_count = ceil($total_items/$item_per_page);

    $page_nums = array();

    for($i = 1, $j = 0 ; $i <= $page_count; $i++, $j+=$item_per_page){
        $page_nums[$i] = $j;
    }

    return $page_nums;
}

2 个答案:

答案 0 :(得分:0)

首先让我举个例子来解释为什么会这样。

class A{
    public function foo(){
        echo "foo";
    }

    public static function bar(){
        // If you call the foo function like this:
        // error: Strict standards: Non-static method A::foo() should not be called statically
        //self::foo();

        // That means it should be called like this:
        (new A)->foo();
    }
}

A::bar();

staticnon-static之间的区别在于第一个不需要初始化,因此您可以调用classname然后将::附加到其中并调用方法立即。像这样:

ClassName::method();

如果方法不是静态的,你需要像这样初始化它:

$obj = new ClassNmae();
$onj->method();

但是在PHP 5.4中,您可以使用此语法来更快地调用:

(new ClassName)->method(); 

话虽如此,这是您需要在代码中进行更改的内容:

// your code

public function get_product($view_update = FALSE) {

    $obj_db = $this->obj_db();

    // your code
}    
public static function get_products($start = -1, $count = 0, $type = "all", $brandID = 0) {

    $obj_db = (new DB_Connection)->obj_db();

    // your code
}

public static function pagination($item_per_page = 6, $brandID = 0){

    $obj_db = (new DB_Connection)->obj_db();

    // your code
}

答案 1 :(得分:0)

我做错了,我已经为分页创建了静态功能。我忘了在db连接页面上添加静态!

protected static function obj_db(){

public static function get_products($start = -1, $count = 0, $type = "all", $brandID = 0) {

    //$obj_db = $this->obj_db();
    $obj_db = self::obj_db();

感谢所有人,他们也指导我!