$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 = "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 = "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 = "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不对,我很抱歉。有人能帮我吗 ?提前谢谢。
答案 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);
?>
答案 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));
在您的代码中,只需更改以下代码:
$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);