嘿伙计们,我需要一些有关关联数组的帮助
这就是我所拥有的。
我有两个 mysql 表,一个称为 users...设置如下。
[ MYSQL DB users] *************************************** Id------name-------empnum------amount 1 ------testa------123456------40.00 2 ------testb------652526------300.00 3 ------Testc------919825------100.00 4 ------Testd------354694------50.00
And the other db is setup like this.
[ MYSQL DB po ] *************************************** Id------ponum------reason----empnum---------total------reason 1 ------1234------testa------123456---------40.00------Demo 2 ------1235------testb------652526--------300.00------Demo 3 ------1236------Testc------919825--------100.00------other 4 ------1237------Testd------123456---------50.00------Demo
What i need to do is select each user from "Users" db and then find them in the "Po" db
Then i need to add up the total they have spent (in PO) and compare it with Amount from users db.
I have this javascript bar graph that i would like to display next to each persons name to show them how close to 100% they have spent.
<div id="progressBar<? echo $id; ?>" class="default"><div></div></div>
<script>
progressBar(<?php echo round(($sum/$total) * 100); ?>, $('#progressBar<? echo $id; ?>'));
</script>
我的问题是 我该怎么办呢。我的第一个想法是使用关联数组并从用户那里获取 amount 和 empnum ,然后从 mysql 中提取该信息,然后将其与 Total 进行比较......在点 a 和 b 之间我迷失了。我不太了解 A 数组如何与 mysql 和两个表一起使用。 从我发现的情况来看,我似乎还必须使用 for every 循环。? 我是否以错误的方式处理这个问题..有更简单的方法吗???
最佳答案
您可以在单个查询中完成大部分操作:
SELECT u.amount, SUM(p.total) 'po_total'
FROM users u LEFT JOIN po p
ON u.empnum = p.empnum
GROUP BY p.empnum
然后:
$percentage = $row['po_total'] / $row['amount'];
关于php - 两个mysql表之间和每个循环之间的关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26106675/