2010-11-23 20 views
1

我試圖調用函數ProdTotal來獲取產品的總訂購量和總量,並將其加載到顯示函數的表中。我收到錯誤:調用未定義的函數ProdTotal();調用未定義的函數ProdTotal()

<?php 

Class CarsClass { 
     private $user = 'php06'; 
     private $pwd = 'php06'; 
     private $dbConn; 

function __construct($db='classicmodels') { 
     //Create connection to MySQL database requested, if blank just connect up. 

     $this->dbConn = new mysqli('localhost', $this->user, $this->pwd, $db); 
     if (mysqli_connect_errno()) { 
      echo 'Error: Could not connect to database. Please try again later.'; 
      exit; 
     } 
     $query = "select count(*) as 'custcount' from customers"; 

     $result = $this->dbConn->query($query); 
     $row = $result->fetch_assoc(); 
     $custCount = $row['custcount']; 

     print "Connected to DB $db as user $this->user<br><br> Number of Rows $custCount<br><br>"; 
    } 

function __destruct(){ 
     mysqli_close(); 
     Print "DB closed by user <br><br>"; 
    } 

function header(){ 
    echo "Midterm Exam Script 2 Header<br><br>"; 
    } 

function display(){ 
    $totqty = 0; 
    $totamt = 0; 
    //get row from WB_Resident Table 
    $query = "select productCode,productName,productDescription,quantityInStock,buyprice,MSRP from products"; 
    $result = $this->dbConn->query($query); 
?> 

<table id="midterm2"> 

    <tr> 
     <th colspan="13">Product Database Table</th> 
    </tr> 
    <tr> 
     <th width="2%">productCode</th> 
     <th width="10%">productName</th> 
     <th width="10%">productDescription</th> 
     <th width="10%">quantity in stock</th> 
     <th width="10%">buyPrice</th> 
     <th width="2%">MSRP</th> 
     <th width="10%">Total Qty Ordered</th> 
     <th width="10%">Total $ Ordered</th> 
    </tr> 

    <?php 
    while($row = $result->fetch_assoc()): 
    $producta = $row["productCode"]; 
    ProdTotal($producta); 

    ?> 
    <tr> 
     <td> 
     <?php echo $row["productCode"]; ?> 
     &nbsp; 
     </td> 
     <td> 
     <?php echo $row["productName"];?> 
     &nbsp; 
     </td> 
     <td> 
     <?php echo $row["productDescription"]; ?> 
     &nbsp; 
     </td> 
     <td> 
     <?php echo $row["Qty In Stock"]; ?> 
     &nbsp; 
     </td> 
     <td> 
     <?php echo $row["Buy Price"]; ?> 
     &nbsp; 
     </td> 
     <td> 
     <?php echo $row["MSRP"]; ?> 
     &nbsp; 
     </td> 
     <td> 
     <?php echo $totqty; ?> 
     &nbsp; 
     </td> 
     <td> 
     <?php echo $totamt; ?> 
     &nbsp; 
     </td> 

     </tr> 
     <?php 
     endwhile; 
     ?> 
     </table> 
    <?php 
    } 

function footer(){ 
    echo "Midterm Exam Script 3 Footer<br><br>"; 
    } 

function ProdTotal($product){ 

    echo "product code entered = $product <br><br>"; 
    $query = "select RTRIM(productCode) as productt, quantityOrdered, priceEach from orderdetails order by productt"; 

     $result = $this->dbConn->query($query); 

     while($row = $result->fetch_assoc()){ 
     if ($row["productt"] == $product){ 
     echo $row;  
     $total = $row["quantityOrdered"] * $row["priceEach"]; 
     $totqty = $totqty + $row["quantityOrdered"]; 
     $totamt = $totamt + $total; 
     echo totqty; 
     echo totamt; 
     return array($totqty, $totamt); 

     } 
     } 

     } 
     }  
    ?> 

執行該腳本來調用類。

<html> 
<head> 
<title>Midterm2 Script 3</title> 
<link rel="stylesheet" type="text/css" href="midterm2.css" /> 
</head> 

<body> 

<?php 
require 'CarsClass3a.php'; 

$obj1= new CarsClass('classicmodels'); 

$obj1->header(); 
$obj1->display(); 
$obj1->footer(); 

?> 

</body> 
</html> 
+0

你不顯示您正在使用調用類的代碼 - 你可能稱其爲靜態上下文的地方。那真的是你想要做的嗎?看着你的代碼,我懷疑它 – 2010-11-23 23:39:33

+0

並請顯示確切的行發生在 – 2010-11-23 23:50:07

回答

2

display(),調用ProdTotal()改變

list($totqty, $totamt) = $this->ProdTotal($producta);