如何将未知的php数组值相乘?

时间:2021-11-13 01:02:13

I have a database output loop.

我有一个数据库输出循环。

I need to multiply values in $total, assign the result to another variable and output it. How should I do this?

我需要将$total中的值相乘,将结果赋给另一个变量并输出它。我该怎么做呢?

$quantity comes from $_POST.

量来自$ _POST美元。

$items = implode(",",array_keys($_POST));

mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM
    items
WHERE
    item_id IN (' . $items . ')';

$result = mysql_query($query, $db) or die (mysql_error($db));

echo ' 

<table id="items">
<tr class="head">
    <td>Name</td>
    <td>Cost</td>
    <td>Quantity</td>
    <td>Subtotal</td>
</tr>

';
$total = array ();  
while ($row = mysql_fetch_assoc($result)) {
echo '<tr class="targetfields">';
echo '<td>' . $row['item_name'] . '</td>

<td>' . $row['item_cost'];
//Getting item id
foreach ($_POST as $itemid=>$quantity)
{
//Displayin' quantity
if ($itemid == $row['item_id']){
    echo '</td><td><input name="' . $row['item_id'] . '" class="input-small" size="2" maxlength="2" type="text" value="';
    echo "{$quantity}";
    echo '" readonly></td>
    <td>'; $sum = ($row['item_cost'] * $quantity); echo $sum;
    echo '</td>';

    $total.= $sum;

}


}
        echo '</tr>';
}

?>  <tr>
<td class="sum" colspan="4">    Total: 
<?php 



?> </td>
</tr>
</table>

2 个解决方案

#1


2  

  1. $total .= $sum; this will not work change it to : $total[] = $sum;
  2. 总美元。=美元金额;这将不会使其变为:$total[] = $sum;
  3. To get the product all elements of array $total use : after while loop array_product
  4. 要获取数组$total的所有元素:after while循环array_product

ie :

即:

$items = implode(",",array_keys($_POST));

mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM
    items
WHERE
    item_id IN (' . $items . ')';

$result = mysql_query($query, $db) or die (mysql_error($db));
$total = array ();  
while ($row = mysql_fetch_assoc($result)) {

$sum = ($row['item_cost'] * $quantity); echo $sum;      
$total[] = $sum;

}

echo array_product($total);

Ref: http://php.net/manual/en/function.array-product.php

裁判:http://php.net/manual/en/function.array-product.php

#2


2  

I think it should be

我想应该是这样

$sum = 1; 
while ($row = mysql_fetch_assoc($result)) {
    $sum = $row['item_cost'] * $sum; 
}
echo $sum;      

#1


2  

  1. $total .= $sum; this will not work change it to : $total[] = $sum;
  2. 总美元。=美元金额;这将不会使其变为:$total[] = $sum;
  3. To get the product all elements of array $total use : after while loop array_product
  4. 要获取数组$total的所有元素:after while循环array_product

ie :

即:

$items = implode(",",array_keys($_POST));

mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM
    items
WHERE
    item_id IN (' . $items . ')';

$result = mysql_query($query, $db) or die (mysql_error($db));
$total = array ();  
while ($row = mysql_fetch_assoc($result)) {

$sum = ($row['item_cost'] * $quantity); echo $sum;      
$total[] = $sum;

}

echo array_product($total);

Ref: http://php.net/manual/en/function.array-product.php

裁判:http://php.net/manual/en/function.array-product.php

#2


2  

I think it should be

我想应该是这样

$sum = 1; 
while ($row = mysql_fetch_assoc($result)) {
    $sum = $row['item_cost'] * $sum; 
}
echo $sum;