根据订购的商品设置总数,有两种类型,有时一种类型不会被订购。

时间:2014-11-24 15:04:28

标签: php html mysql

好吧,我正在踢自己,因为我以前曾经给过我一个,但是现在,它没有抓住所有选择出售的物品,并且它没有完全正确。如何才能正确完成此操作。有两种类型的产品和两个填充这些行的表,一种是OrderIn_Id,另一种是OrderOut_Id。产品填充完美,只是没有得到总数,并给我一个欠定义变量的错误,如果一个人只选择一种类型的产品,如OrderIn_id产品,那么PriceOut给出一个未定义变量的错误。你能帮忙吗?

<?php
echo "<table class='middlecheckOut'> 
<tr>
<td class='td2'><b>Order ID: </b></td>
<td class='td2'><b>Product Name: </b></td>
<td class='td2'><b>Quantity: </b></td>
<td class='td2'><b>Price: </b></td>
</tr>";

if (isset($_GET['user_id'])) {     
    $user_id = $_GET['user_id']; 
} elseif (isset($_POST['user_id']))  {    
    $user_id = $_POST['user_id'];
} 

$display="SELECT * 
    FROM order_instate JOIN in_Product ON 
    order_instate.ip_id = in_product.ip_id
    WHERE user_id = '$user_id'; " ; 

$displayResult = @mysqli_query($dbhandle, $display)
            or die(mysqli_error($dbhandle));

while($row = mysqli_fetch_array($displayResult, MYSQLI_ASSOC)) { 
    if($row['orderIn_complete'] == "No") {      
echo "<tr>
<td class='td2'>" . $row['orderIn_id'] . " &nbsp&nbsp</td>
<td class='td2'>" . $row['ip_name'] . " &nbsp&nbsp</td>
<td class='td2'>" . $row['orderIn_quantity'] . " &nbsp&nbsp</td>
<td class='td2'>$" . $row['orderIn_total'] . " &nbsp&nbsp</td>
 </tr>";
 $priceIn = NULL;
 $priceIn += $row['orderIn_total'];
    }
 }

 if (isset($_GET['user_id'])) {     
    $user_id = $_GET['user_id']; 
} elseif (isset($_POST['user_id']))  {    
    $user_id = $_POST['user_id'];
} 

$display2="SELECT * 
    FROM order_outstate JOIN op_Product ON 
    order_outstate.op_id = op_product.op_id
    WHERE user_id = '$user_id'; " ; 

$displayResult = @mysqli_query($dbhandle, $display2)
            or die(mysqli_error($dbhandle));

while($row2 = mysqli_fetch_array($displayResult, MYSQLI_ASSOC)) { 
    if($row2['orderOut_complete'] == "No") {        
echo "<tr>
<td class='td2'>" . $row2['orderOut_id'] . " &nbsp&nbsp</td>
<td class='td2'>" . $row2['op_name'] . " &nbsp&nbsp</td>
<td class='td2'>" . $row2['orderOut_quantity'] . " &nbsp&nbsp</td>
<td class='td2'>$" . $row2['orderOut_total'] . " &nbsp&nbsp</td>
 </tr>";
  $priceOut = NULL;
 $priceOut += $row2['orderOut_total'];
    }
 }
echo "</table>";

$subtotal = 0;
$tax = 0;
$gtotal = 0;
$subtotal = number_format($priceIn + $priceOut, 2);
$tax = number_format($subtotal * .074, 2);
$gtotal = number_format($subtotal + $tax, 2);

?>

2 个答案:

答案 0 :(得分:1)

这有用吗?

<?php
echo "<table class='middlecheckOut'> 
<tr>
<td class='td2'><b>Order ID: </b></td>
<td class='td2'><b>Product Name: </b></td>
<td class='td2'><b>Quantity: </b></td>
<td class='td2'><b>Price: </b></td>
</tr>";

if (isset($_GET['user_id'])) {     
    $user_id = $_GET['user_id']; 
} elseif (isset($_POST['user_id']))  {    
    $user_id = $_POST['user_id'];
} 

$display="SELECT * 
    FROM order_instate JOIN in_Product ON 
    order_instate.ip_id = in_product.ip_id
    WHERE user_id = '$user_id'; " ; 

$displayResult = @mysqli_query($dbhandle, $display)
            or die(mysqli_error($dbhandle));
$priceIn = 0;
while($row = mysqli_fetch_array($displayResult, MYSQLI_ASSOC)) { 
    if($row['orderIn_complete'] == "No") {      
echo "<tr>
<td class='td2'>" . $row['orderIn_id'] . " &nbsp&nbsp</td>
<td class='td2'>" . $row['ip_name'] . " &nbsp&nbsp</td>
<td class='td2'>" . $row['orderIn_quantity'] . " &nbsp&nbsp</td>
<td class='td2'>$" . $row['orderIn_total'] . " &nbsp&nbsp</td>
 </tr>";

 $priceIn += $row['orderIn_total'];
    }
 }

 if (isset($_GET['user_id'])) {     
    $user_id = $_GET['user_id']; 
} elseif (isset($_POST['user_id']))  {    
    $user_id = $_POST['user_id'];
} 

$display2="SELECT * 
    FROM order_outstate JOIN op_Product ON 
    order_outstate.op_id = op_product.op_id
    WHERE user_id = '$user_id'; " ; 

$displayResult = @mysqli_query($dbhandle, $display2)
            or die(mysqli_error($dbhandle));

$priceOut = 0;
while($row2 = mysqli_fetch_array($displayResult, MYSQLI_ASSOC)) { 
    if($row2['orderOut_complete'] == "No") {        
echo "<tr>
<td class='td2'>" . $row2['orderOut_id'] . " &nbsp&nbsp</td>
<td class='td2'>" . $row2['op_name'] . " &nbsp&nbsp</td>
<td class='td2'>" . $row2['orderOut_quantity'] . " &nbsp&nbsp</td>
<td class='td2'>$" . $row2['orderOut_total'] . " &nbsp&nbsp</td>
 </tr>";

 $priceOut += $row2['orderOut_total'];
    }
 }
echo "</table>";

$subtotal = 0;
$tax = 0;
$gtotal = 0;
$subtotal = number_format($priceIn + $priceOut, 2);
$tax = number_format($subtotal * .074, 2);
$gtotal = number_format($subtotal + $tax, 2);

?>

答案 1 :(得分:1)

看起来您只在if块内部声明变量$priceIn$priceOut,并且每次都将它们设置为null。如果在任何时候,其中一个if块不等于true,则该变量永远不会被设置。

在运行while循环之前,请尝试设置$priceIn$priceOut。例如:

<?php
echo "<table class='middlecheckOut'> 
<tr>
<td class='td2'><b>Order ID: </b></td>
<td class='td2'><b>Product Name: </b></td>
<td class='td2'><b>Quantity: </b></td>
<td class='td2'><b>Price: </b></td>
</tr>";

if (isset($_GET['user_id'])) {     
    $user_id = $_GET['user_id']; 
} elseif (isset($_POST['user_id']))  {    
    $user_id = $_POST['user_id'];
} 

$priceIn = 0;
$priceOut = 0;

$display="SELECT * 
    FROM order_instate JOIN in_Product ON 
    order_instate.ip_id = in_product.ip_id
    WHERE user_id = '$user_id'; " ; 

$displayResult = @mysqli_query($dbhandle, $display)
            or die(mysqli_error($dbhandle));

while($row = mysqli_fetch_array($displayResult, MYSQLI_ASSOC)) { 
    if($row['orderIn_complete'] == "No") {      
        echo "<tr>
        <td class='td2'>" . $row['orderIn_id'] . " &nbsp&nbsp</td>
        <td class='td2'>" . $row['ip_name'] . " &nbsp&nbsp</td>
        <td class='td2'>" . $row['orderIn_quantity'] . " &nbsp&nbsp</td>
        <td class='td2'>$" . $row['orderIn_total'] . " &nbsp&nbsp</td>
         </tr>";
         $priceIn += $row['orderIn_total'];
    }
 }

 if (isset($_GET['user_id'])) {     
    $user_id = $_GET['user_id']; 
} elseif (isset($_POST['user_id']))  {    
    $user_id = $_POST['user_id'];
} 

$display2="SELECT * 
    FROM order_outstate JOIN op_Product ON 
    order_outstate.op_id = op_product.op_id
    WHERE user_id = '$user_id'; " ; 

$displayResult = @mysqli_query($dbhandle, $display2)
            or die(mysqli_error($dbhandle));

while($row2 = mysqli_fetch_array($displayResult, MYSQLI_ASSOC)) { 
    if($row2['orderOut_complete'] == "No") {        
        echo "<tr>
        <td class='td2'>" . $row2['orderOut_id'] . " &nbsp&nbsp</td>
        <td class='td2'>" . $row2['op_name'] . " &nbsp&nbsp</td>
        <td class='td2'>" . $row2['orderOut_quantity'] . " &nbsp&nbsp</td>
        <td class='td2'>$" . $row2['orderOut_total'] . " &nbsp&nbsp</td>
         </tr>";
         $priceOut += $row2['orderOut_total'];
    }
 }
echo "</table>";

$subtotal = 0;
$tax = 0;
$gtotal = 0;
$subtotal = number_format($priceIn + $priceOut, 2);
$tax = number_format($subtotal * .074, 2);
$gtotal = number_format($subtotal + $tax, 2);

?>