0

由于某种原因,我无法从投标中检索我想要的信息。我希望它显示当前项目的所有出价。db、query 或 php 有问题吗?其他功能起作用,只是bid[bidprice]不起作用。当我试图让它显示它不会显示等。

/*
============
db class
============
*/

public function allbids($id){
        $qry = "SELECT userName , bidPrice as bidPrice FROM bids , users WHERE productID =                $id";
        $rs = $this -> db -> query($qry);
        if($rs) {
            if($rs ->num_rows > 0) {
                $bid = $rs -> fetch_assoc();

            }
            return $bid;

        } else {
            echo 'Error Executing Query';   
        }
        return false;
    }


/*
============
html php
============
*/
 <?php

class ProductView extends View {
    protected function displayContent() {
        if(isset($_GET['id'])) {
        //get the record from database
            $this -> product = $this -> model -> getProductByID($_GET['id']);
            $this -> bidprice = $this -> model ->allbids($_GET['id']);
                if(is_array($this -> product)) {
                    $html = $this -> displayProduct();
                    } else {

                       $html .= '<p>Sorry, that product is not available</p>';

                    }            
                        } else {
                         header("Location:index.php?page=error");

        }
            return $html;
    }


    private function displayProduct() {

            $html = '<div id="product">';
            $html .= '<img src="images/products/'.$this -> product['productImage'].'" alt="'.$this -> product['productName'].'" />';
            $html .= '<h3>'.$this -> product['productName'].'</h3>';
            $html .= '<p><strong>$'.$this -> product['productPrice'].'.00'.'</strong></p>';
            //.sprintf("%.2f" result was breaking the query placed .00. to give it currency rate.
            $html .= '<p>'.$this -> product['productDescription'].'</p>';
            $html .= '<p>'.$this -> bidprice['bidprice'].'</p>';
               foreach($this -> bidprice as $val)
        {
            $html .= '<p> Bid Price'.$val['bidPrice'].'</p>';
            $html .= '<p> Bidded By'.$val['userName'].'</p>';
        }

            $html .= '</div>';
            $html .='<div id="space">';
            $html .='</div>';

        return $html;        
    }    
}
4

1 回答 1

0

您必须更新查询以指定应从中获取字段的表:

SELECT `users`.`userName` AS `userName`, 
       `bids`.`bidPrice` AS `bidPrice` 
FROM `bids`, `users` 
WHERE `bids`.`productID` = $id

对于上述内容,我假设该productID字段包含在bids表中。此外,您只会从集合中获取第一个返回的记录。如果要获取整个投标历史记录,则需要使用while()循环,然后将每条记录写入bids数组,然后将其返回。

这样的事情应该可以实现您想要做的事情:

public function allbids($id)
{
    $qry = "SELECT `users`.`userName` AS `userName`, 
            `bids`.`bidPrice` AS `bidPrice` 
            FROM `bids`, `users` 
            WHERE `bids`.`productID` = $id";

    $rs = $this->db->query($qry);
    if($rs) 
    {
        $bids = array();
        while($bid = $rs->fetch_assoc())
        {
            $bids = $bid
        }
        return $bids;
    } 
    else 
    {
        echo 'Error Executing Query';   
        return false;
    }
}

调用allbids(1)将返回产品 ID 的出价记录数组1,或者false查询是否有错误。

于 2013-04-08T10:35:19.000 回答