使用MySQL和/或PHP的数学运算

时间:2017-01-16 09:24:02

标签: php mysql variables mathematical-expressions

SQL1

$sql1 = "SELECT group_concat(Gericht_ID separator ',') as Gericht_ID FROM `warenkorb` WHERE Kunde_ID = '$kid'";
    $result = mysqli_query($con, $sql1);
    $row = mysqli_fetch_array($result);
    $gerid = $row["Gericht_ID"];
    print $gerid;

SQL2

$sql2 = "SELECT group_concat(warenkorb.Menge separator ',') AS Menge FROM warenkorb INNER JOIN enthaelt ON  warenkorb.Gericht_ID=enthaelt.Gericht_ID WHERE enthaelt.Gericht_ID IN($gerid) AND warenkorb.Kunde_ID='$kid'";
    $result = mysqli_query($con, $sql2);
    $row = mysqli_fetch_array($result);
    $gwmenge = $row["Menge"];       
    print $gwmenge; //i get here 1,1,1,1,3,3,3

SQL3

$sql3 = "SELECT group_concat(zutaten.Menge separator ',') as Menge FROM `zutaten` INNER JOIN enthaelt ON zutaten.Zutat_ID=enthaelt.Zutat_ID WHERE Gericht_ID IN($gerid)";
    $result = mysqli_query($con, $sql3);
    $row = mysqli_fetch_array($result);
    $gzmenge = $row["Menge"];
    print $gzmenge; //I get here 45,76,10,92,32,88,36

SQL4

$sql4 = "SELECT group_concat(Menge separator ',') as Menge FROM `enthaelt` WHERE Gericht_ID IN($gerid)";
    $result = mysqli_query($con, $sql4);
    //print $sql4;
    $row = mysqli_fetch_array($result);
    $gemenge = $row["Menge"];
    print `$gemenge` // I get here 3,1,2,2,3,2,1
    $mengetotal=$gwmenge*$gemenge;// here i want (1,1,1,1,3,3,3 *3,1,2,2,3,2,1) =3,1,2,2,9,6,3
    print $mengetotal;
    $mengezutaten =$gzmenge - $mengetotal;// here i want(45,76,10,92,32,88,36- 3,1,2,2,9,6,3)= 42,75,8,90,23,82,33
    print $mengezutaten;

我该怎么做?我的代码mybe不对,我很抱歉。有人能帮我吗 ?提前谢谢。

2 个答案:

答案 0 :(得分:0)

<?php

$gwmenge = [1,1,1,1,3,3,3];

$gzmenge = [45,76,10,92,32,88,36];

$gemenge = [3,1,2,2,3,2,1];


// $mengetotal
for($i = 0; $i < sizeof($gwmenge); $i++){

    $mengetotal[] = $gwmenge[$i] * $gemenge[$i];
}

//mengezutaten 

for($i = 0; $i < sizeof($gzmenge); $i++){
    $mengezutaten[] = $gzmenge[$i] - $mengetotal[$i]; 
}


print_r($mengetotal);
echo "<br/>";
print_r($mengezutaten);

?>

enter image description here

答案 1 :(得分:0)

修改了Tony Answers。在最终数组上使用implode()函数。

<?php
$gwmenge = '1,1,1,1,3,3,3';
$gwmenge = explode(',',$gwmenge);

$gzmenge = '45,76,10,92,32,88,36';
$gzmenge = explode(',',$gzmenge);

$gemenge = '3,1,2,2,3,2,1';
$gemenge = explode(',',$gemenge);

// $mengetotal
for($i = 0; $i < sizeof($gwmenge); $i++){

    $mengetotal[] = $gwmenge[$i] * $gemenge[$i];
}

//mengezutaten 

for($i = 0; $i < sizeof($gzmenge); $i++){
    $mengezutaten[] = $gzmenge[$i] - $mengetotal[$i]; 
}


print_r(implode(',',$mengetotal));
echo "<br/>";
print_r(implode(',',$mengezutaten));

LIVE DEMO

在您的代码中,只需更改以下代码:

$sql4 = "SELECT group_concat(Menge separator ',') as Menge FROM `enthaelt` WHERE Gericht_ID IN($gerid)";
    $result = mysqli_query($con, $sql4);
    //print $sql4;
    $row = mysqli_fetch_array($result);
    $gemenge = $row["Menge"];
        $gwmenge = explode(',',$gwmenge);
        $gzmenge = explode(',',$gzmenge);
        $gemenge = explode(',',$gemenge);

       // $mengetotal
        for($i = 0; $i < sizeof($gwmenge); $i++){
            $mengetotal[] = $gwmenge[$i] * $gemenge[$i];
        }

        //mengezutaten  
        for($i = 0; $i < sizeof($gzmenge); $i++){
            $mengezutaten[] = $gzmenge[$i] - $mengetotal[$i]; 
        }

        $mengetotal = implode(',',$mengetotal);
        $mengezutaten = implode(',',$mengezutaten);