Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将未知数量的php数组值相乘?_Php_Arrays - Fatal编程技术网

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

如何将未知数量的php数组值相乘?,php,arrays,Php,Arrays,我有一个数据库输出循环 我需要将$total中的值相乘,将结果分配给另一个变量并输出它。我该怎么做 $quantity来自$\u POST $items = implode(",",array_keys($_POST)); mysql_query('SET NAMES utf8'); $query = 'SELECT * FROM items WHERE item_id IN (' . $items . ')'; $result = mysql_query($query, $

我有一个数据库输出循环

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

$quantity
来自
$\u 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>
$items=内爆(“,”,数组_键($_POST));
mysql_查询('SET NAMES utf8');
$query='SELECT*FROM
项目
哪里
(“.$items.”中的项目标识;
$result=mysql_query($query,$db)或die(mysql_error($db));
回声'
名称
成本
量
小计
';
$total=array();
while($row=mysql\u fetch\u assoc($result)){
回声';
回显'.$row['item_name'].'
“.$row[“项目成本”];
//正在获取项目id
foreach($\作为$itemid=>$quantity发布)
{
//显示数量
如果($itemid==$row['item\u id'])){
回声'
“;$sum=($row['item_cost']*$quantity);echo$sum;
回声';
$total.=$sum;
}
}
回声';
}
?>  
总数:
  • $total.=$sum这将不起作用,请将其更改为:
    $total[]=$sum
  • 要获取产品,请使用:after while loop
    array\u product
  • 即:

    参考:

  • $total.=$sum这将不起作用,请将其更改为:
    $total[]=$sum
  • 要获取产品,请使用:after while loop
    array\u product
  • 即:

    参考:我想应该是

    $sum = 1; 
    while ($row = mysql_fetch_assoc($result)) {
        $sum = $row['item_cost'] * $sum; 
    }
    echo $sum;      
    
    我想应该是这样

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


    那么
    $quantity
    呢?@PrasanthBendra不需要
    quantity
    。因为乘法将是从查询中选择的行。主要的是,
    $quantity
    在这里从来没有初始化过,所以它会在这里产生错误。$sum不是$row['item\u cost']*1在每一行,echo$sum将只显示最后一次计算?@bestprogrammerintheworld在每次迭代中,它将把乘法分配给
    $sum
    ,在下一次迭代中,它将用于乘法。啊哈,好的,谢谢!有时最基本的事情还不够明显:-)那么
    $quantity
    呢?@PrasanthBendra不需要
    quantity
    。因为乘法将是从查询中选择的行。主要的是,
    $quantity
    在这里从来没有初始化过,所以它会在这里产生错误。$sum不是$row['item\u cost']*1在每一行,echo$sum将只显示最后一次计算?@bestprogrammerintheworld在每次迭代中,它将把乘法分配给
    $sum
    ,在下一次迭代中,它将用于乘法。啊哈,好的,谢谢!有时最基本的事情还不够明显:-)你能用
    $quantity
    初始化来显示你的完整代码吗?所以你想把
    $row['item\u cost']*$quantity
    相乘,并将其计算结果存储在每次迭代的数组中?@Yogesh是的,没错。我使用数组来存储每次迭代的计算,然后我将每个计算相互叠加并输出。然后,
    Prashant的回答是绝对正确的。你能用
    $quantity
    初始化来显示你的完整代码吗?所以你想要
    $row['item\u cost'的乘法*$quantity
    并将每次迭代的计算结果存储在数组中?@Yogesh是的,没错。我用数组存储每次迭代的计算结果,然后我将每个计算结果相互转换并输出。然后,
    Prashant的回答是绝对正确的。