2014-01-20 55 views
0

我已经看了这些例子到目前为止Multiplying two 2D arrays {这是C++所以不知道如何有用的ti将为php} JSON encode multiplies arrays as single array {尽管php代码它不乘以两个数组} Multiplying two arrays in php {这几乎是我想要做的,但解决方案不工作,所以不知道如果它的解决方案或它是不需要的}将两个数组从MYSQL数据乘到一个新的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做实际的计算,这是问题的为每个变量的数据显示正常,只有当我尝试繁殖方式他们是否会成为问题。

回答

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 
} 
+0

这个问题有很多代码在里面。如果你能够通过解释代码行的位置来帮助提问者,并且提请注意这条线与提问者之间的区别,那将会很好。 –

+0

抱歉,迟到的回应,当最初尝试它,它从来没有工作,但似乎我正在测试的代码页已成为破碎或缓存在错误的状态,当我添加它住另一个单独的测试页它工作很好,也似乎我在我的一个例子中得到了更多,但我忘了用我替换x – andy

相关问题