gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:06:14 25 4
gpt4 key购买 nike

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 不正确,对此我深表歉意。有人能帮我吗 ?提前谢谢你。

最佳答案

修改后的 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);

关于php - 使用 MySQL 和/或 PHP 的数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com