简体   繁体   English

如何在表行中显示两个数据库表中的选定值

[英]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.

相关问题 根据数据库中的两个表显示下拉选择的值 - display dropdown selected values based on two table from database 如何显示来自MySQL数据库的两个单独表中的值? - How to display values from two separate tables from mySQL database? 如何从两个表中选择数据,而两个表只为第一表的一行选择了第二表的第一行 - How to SELECT data from two tables which only first row of second table is selected for one row of first table 如何将选定的HTML表行值显示为div以用于图像 - How To Display Selected HTML Table Row Values Into a div for purpose of an image 如何从mysql数据库的两个表中查询数据并显示一个HTML表 - How to query data from two tables from mysql database and display one HTML table 如何从数据库的两个表中获取数据并显示在一个html表中 - how to get data from two tables from database and display in one html table 如何从数据库PHP的两个表中随机选择列 - How to randomly selected column from two tables in database PHP 如何通过 $row[ ] 显示 codeigniter 中两个表的记录 - How to display records from two tables in codeigniter by $row[ ] 如何在两个选定日期之间显示数据库中的数据 - How to display data from database between two selected date 从两个下拉列表中选择值时如何显示div? - How to display a div when values are selected from TWO dropdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM