将两个数组从MYSQL数据乘以一个新的PHP数组

时间:2014-01-20 15:25:37

标签: php mysql arrays

到目前为止,我已查看过这些示例Multiplying two 2D arrays {这是c ++所以不确定对于php有多大帮助} JSON encode multiplies arrays as single array {虽然php代码不是两个数组相乘} Multiplying two arrays in php {这正是我想做的事情,但解决方案不起作用,所以不确定它是解决方案还是不需要}

基本上我要做的是,将两个从mysql数据库生成的数组转换为另一个数组,其中两个mysql数组的结果相乘。

到目前为止,我已经尝试了很多不同的方法,但没有运气。

<?php

    //Database connection
            $db=mysqli_connect("***","***","***","***") or die(mysqli_error($db));

    //This is to test if there is a php scripting error ie white screen etc if no results are displayed
            echo "test";

    //SQL Statements
            $sql4="SELECT * FROM invoicequote";
            $sql5="SELECT SUM(unitprice*units) AS total FROM invoicequote";

    //Query database for results
            $result4=mysqli_query($db,$sql4) or die(mysqli_error($db));
            $result5=mysqli_query($db,$sql5) or die(mysqli_error($db));

    //Arrays of services performed and prices
            $service=array();
            $workdone=array();
            $unitprice=array();
            $units=array();
            $total=array();

    //While loop for invoice or quote information
            while($num = mysqli_fetch_assoc($result4))
                    {
                            $service[]=$num['servicesused'];
                            $workdone[]=$num['serviceworkdone'];
                            $unitprice[]=$num['unitprice'];
                            $units[]=$num['units'];
                            //$total[]=($unitprice*$units);
                    }
    //Also tried $total[]=($unitprice[]*$units[]); but this gave a white screen i suspect because there was no index supplied

    //Put total into array by multiplying the unit price and number of units
/*
            for ($unitprice as $price && $unit as $u)
                    {
                            $total=array($price*$u);
                            echo $total[0];
                    } //This gives a white screen it appears to be a infinite loop
*/
    //use mysql to do the calculations before it comes out into php
/*
            while($num = mysqli_fetch_array($result5))
            {
                    $total[]=$num['total'];
                    echo $total[0]; //this gives the total off all prices rather than individuals prices
            }
  */
    //putting results into a array
    $arr=count($units);

    for($i=0;$i<$arr;$i++)
    {
            $total[]=$unitprice[$x]*$unit[$x]; //this gives a white screen as well
    }

    //Display the results
            $arrlength=count($units);

    //This is the prefer way to display the results but i am open to anything that works
            for($x=0;$x<$arrlength;$x++)
            {
                    echo $total[$x];
            }

    mysqli_close($db);

?>

删除了数据库信息

我怀疑它是我试图让php进行实际计算的方式,因为每个变量的数据只有当我试图将它们相乘才会显示它会成为一个问题。

1 个答案:

答案 0 :(得分:0)

做,

$total[]=$num['unitprice']*$num['units'];

以下行不正确,因为$unitprice$units是数组而非值。

$total[]=($unitprice*$units);

[]语法用于附加到数组,因此以下内容也不起作用

$total[]=($unitprice[]*$units[]);

但你可以使用for循环,而不是像下面那样假设所有数组的大小相同。

$count = sizeof($unitprice);
for($i=0;$i<$count;$i++){
  $total[$i] = $unitprice[$i]*$units[$i]; //$units[$i] fetches the value at the ith index lllr is the case for $unitprice also
}