[英]How to display selected values from two database tables in table row
I have an accounting statement and want to fetch the expense and receive payment values, Using an While loop to display each one below each other.我有一份会计报表,想要获取费用和接收付款值,使用 While 循环将每一项显示在彼此下方。
I managed to do it but as separate table rows, one for expenses and the other for receive payment.我设法做到了,但作为单独的表格行,一个用于费用,另一个用于接收付款。
The variable $value is the customers name.变量 $value 是客户名称。 It loops each table until all the given values are displayed, i want to combine the code so that it displays each expense and payment underneath each other and not separate ... As following but im getting duplicate values它循环每个表,直到显示所有给定的值,我想组合代码,以便它显示每个费用和付款在彼此下面而不是分开的......如下但我得到重复值
<?php //***************** Expenses ************
require '../Database/conn.php';
$query = mysqli_query($conn, "SELECT * FROM expenses WHERE payee LIKE '$value' ");
while($fetch = mysqli_fetch_array($query)) {
$expTot = substr($fetch['total'], 0, 100);
?>
<div style="word-wrap:break-word;">
<tr>
<td class="date">
<h6 class="mb-0 text-sm date"><?php echo substr($fetch['date'], 0, 100);?></h6>
</td>
<td class="date">
<h6 class="mb-0 text-sm date"><?php ?></h6>
</td>
<td class="type">
<h6 class="mb-0 text-sm type"><?php echo substr($fetch['type'], 0, 100);?></h6>
</td>
<td class="total">
<h6 class="mb-0 text-sm total"><?php echo "R-". substr($fetch['total'], 0, 100);?></h6>
</td>
<?php } ?>
</tr>
</div>
And the same for income.收入也一样。
<?php //************ recive payment ************
require '../Database/conn.php';
$query2 = mysqli_query($conn, "SELECT * FROM `recive_payment` WHERE `customer` LIKE '$value' ");
while($fetch2 = mysqli_fetch_array($query2)) {
?>
<div style="word-wrap:break-word;">
<tr>
<td class="date">
<h6 class="mb-0 text-sm customer"><?php echo substr($fetch2['date'], 0, 100);?></h6>
</td>
<td class="date">
<h6 class="mb-0 text-sm date"><?php ?></h6>
</td>
<td class="total">
<h6 class="mb-0 text-sm type"><?php echo substr($fetch2['type'], 0, 100);?></h6>
</td>
<td class="total">
<h6 class="mb-0 text-sm total"><?php echo "R". substr($fetch2['total'], 0, 100);?></h6>
</td>
<?php // search close
}
}
?>
</tr>
</div>
Ideally you should just create one result in MySQL joining tables, but if you want to go down the "quick and dirty" route then try the below.理想情况下,您应该只在 MySQL 连接表中创建一个结果,但如果您想走“快速而肮脏”的路线,请尝试以下方法。 You might have to make amendments to it if you have different amount of rows on each query, but just so you know the while loop will accept as many arguments as you want.如果每个查询的行数不同,您可能必须对其进行修改,但只是为了让您知道 while 循环将接受您想要的任意数量的参数。 This operation is possible:此操作是可能的:
while($fetch = mysqli_fetch_array($query) && $fetch2 = mysqli_fetch_array($query2)) {
...
...
...
}
You can also create a new array by iteration and then use the new array to iterate and render the table.您也可以通过迭代创建一个新数组,然后使用新数组迭代和渲染表格。
您可以在任何表中创建外键,并使用外键上的连接来连接两个表,然后您可以将它们显示在一个表中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.