根据Foreach中使用的次数添加变量

时间:2016-09-09 10:29:50

标签: php

首先......我将为您提供我的代码:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt1 = $conn->prepare("SELECT reference, price FROM ps_product_attribute WHERE id_product = ". implode(' AND ', $_POST['checkbox']) ."");
    $stmt1->execute();
    $product = $stmt1->fetchAll();

    // Get Product Price
    $stmt2 = $conn->prepare("SELECT * FROM ps_product WHERE id_product = ". implode(' AND ', $_POST['checkbox']) ."");
    $stmt2->execute();
    $productPrice = $stmt2->fetchAll();

    foreach ($productPrice as $rowPrice) {
        foreach ($product as $row) {
            $Price = $rowPrice['price'] + $row['price'];
            $NET = $Price;
            $VAT = $Price * 0.2;
            $Total = $Price + $VAT;

            print '<tr>';
                print '<td>'. $row['reference'] .'</td>';
                print '<td>Product Name</td>';
                print '<td>'. $quantity .'</td>';
                print '<td>&pound;'. number_format((float)$Price, 2, '.', '') .'</td>';
            print '</tr>';
        }
    }

    echo '
        <tr>
            <td style="border: 0;"></td>
            <td style="border: 0;"></td>
            <td><p style="font-weight: bold; float: right;">NET Amount</p></td>
            <td><p style="float: right;">&pound;'. number_format((float)$NET, 2, '.', '') .'</p></td>
        </tr>
        <tr>
            <td style="border: 0;"></td>
            <td style="border: 0;"></td>
            <td><p style="font-weight: bold; float: right;">VAT</p></td>
            <td><p style="float: right;">&pound;'. number_format((float)$VAT, 2, '.', '') .'</p></td>
        </tr>
        <tr>
            <td style="border: 0;"></td>
            <td style="border: 0;"></td>
            <td><p style="font-weight: bold; float: right;">Total</p></td>
            <td><p style="float: right;">&pound;'. number_format((float)$Total, 2, '.', '') .'</p></td>
        </tr>
    ';
}

catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}

$conn = null;

我试图使变量$ NET等于但是返回了许多行的价格。例如,如果id_product为8,它将返回两个产品£18.95和£21.00。目前$ NET的价格为21.00英镑,但应该是39.95英镑。

1 个答案:

答案 0 :(得分:0)

你的SQL语句看起来不正确 - 如果$_POST['checkbox']是一个包含多个项目的数组,我认为生成的sql将无法正确准备。

例如,请考虑以下事项:

$_POST['checkbox']=array(1,2,3,4,5,6,7,8,9);
$stmt2 = "SELECT * FROM ps_product WHERE id_product = ". implode(' AND ', $_POST['checkbox'] );
echo $stmt2;

这将生成如:

的sql
SELECT * FROM ps_product WHERE id_product = 1 AND 2 AND 3 AND 4 AND 5 AND 6 AND 7 AND 8 AND 9

但是,如果你使用IN运算符,你可以按如下方式构造你的SQL:

$_POST['checkbox']=array(1,2,3,4,5,6,7,8,9);
$stmt2 = "SELECT * FROM ps_product WHERE id_product IN ( " . implode( ',', $_POST['checkbox'] ) ." );";
echo $stmt2;

哪个会生成如:

的sql
SELECT * FROM ps_product WHERE id_product IN ( 1,2,3,4,5,6,7,8,9 );

但是因为你使用了两个相关的表,你可以加入表并进行单个查询。

$sql="select * from `ps_products` p
    left outer join `ps_product_attribute` a on a.`product_id`=p.`product_id`
    where p.`product_id` in (".implode(',',$_POST['checkbox']).");";

然后应该使结束循环更容易使用(虽然我可能在这里遗漏了一些东西)并且您可能使用这样的循环:

$TOTAL=0;$NET=0;
while( $rs = $stmt1->fetch( PDO::FETCH_OBJECT ) ){
    $price = $rs->price;
    $VAT = $price * 0.2;
    $NET += $price;
    $TOTAL += ( $price + $VAT );
    /* other stuff */
}