2014-01-20 49 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

相關問題