如何使用特定于最后一行的条件在循环中回显特定数据

时间:2021-05-31 12:13:57

For an accounting system, I'm using PHP & MySQL. I've two tables "GROUP" and "ACHEADS".

对于会计系统,我使用的是PHP和MySQL。我有两张桌子“GROUP”和“ACHEADS”。

In the GROUP table, I have:

在GROUP表中,我有:

---------------------
| id (AI) |  group  |
---------------------
|    1    | Group 1 |
|    2    | Group 2 |
---------------------

In the ACHEADS table, I have:

在ACHEADS表中,我有:

-----------------------------------------
| id (AI) |  ac_head    | amount | j_id |
-----------------------------------------
|    1    | Something 1 |  2000  |   1  |
|    2    | Something 2 |  1000  |   1  |
|    3    | Something 3 |  5000  |   2  |
|    4    | Something 4 |  4000  |   2  |
|    5    | Something 5 |  8000  |   2  |
-----------------------------------------

I've joined the two tables as GROUP.id <<->> ACHEADS.j_id

我加入了两个表作为GROUP.id << - >> ACHEADS.j_id

Now I need to preview the data like this:

现在我需要像这样预览数据:

----------------------------------------------
Particulars               | Details | Total  |
----------------------------------------------
Group 1                   |         |        |
 Something 1              |    2000 |        |
 Something 2              |    1000 |   3000 |
----------------------------------------------
Group 2                   |         |        |
 Something 3              |    5000 |        |
 Something 4              |    4000 |        |
 Something 5              |    8000 |  17000 |
----------------------------------------------
GRAND TOTAL               |         |  20000 |
------------------------------------==========

Challenges

  1. The table will be dynamic and will generate within a PHP loop (I'm using a WHILE loop)
  2. 该表将是动态的,将在PHP循环中生成(我正在使用WHILE循环)

  3. Remember: it's a table and if I miss echoing a td, then the table will break up
  4. 记住:它是一张桌子,如果我想念回应一个td,那么桌子就会分手

Problems

  • When I'm using the loop it's echoing the data on the Details td accurately. But the sum of the details row according to j_id is also echoing in each td
  • 当我使用循环时,它会准确地回显有关详细信息的数据。但是根据j_id的细节行的总和也在每个td中回显

Preview here:

----------------------------------------------
Particulars               | Details | Total  |
----------------------------------------------
Group 1                   |         |        |
 Something 1              |    2000 |   3000 |
 Something 2              |    1000 |   3000 |
----------------------------------------------
Group 2                   |         |        |
 Something 3              |    5000 |  17000 |
 Something 4              |    4000 |  17000 |
 Something 5              |    8000 |  17000 |
----------------------------------------------

My thoughts

  • If I can check whether it is the last data of the query, if isset, then echo the total amount with it's td. (But remember the Challenge#2)
  • 如果我可以检查它是否是查询的最后一个数据,如果是isset,那么用它的td回显总量。 (但请记住挑战#2)

  • Does it require a foreach loop?
  • 它需要foreach循环吗?

I failed

  • I tried checking max(id), it works fine in SQL, but can't use it in condition within a loop.
  • 我试过检查max(id),它在SQL中工作正常,但不能在循环中使用它。

(If you still can't understand me, then on the second phase, I'll post my code.)

(如果你仍然无法理解我,那么在第二阶段,我会发布我的代码。)

2 个解决方案

#1


2  

I would do 2 loops:

我会做2个循环:

  1. Fetch id from GROUP
  2. 从GROUP获取ID

  3. Fetch amount from ACHEADS based on j_id
  4. 根据j_id从ACHEADS获取金额

This would look something like (non-tested code):

这看起来像(未经测试的代码):

echo '<table><tr><td>Particulars</td><td>Details</td><td>Total</td></tr>';

$total = 0;

$q1 = "SELECT id FROM `GROUP`";
$res1 = mysqli_query($q1);
while($row1 = mysqli_fetch_assoc($res1)) {
    echo 

    $group_total = 0;
    $j_id = $row1[id];

    $q2 = "SELECT ac_head, amount FROM ACHEADS WHERE j_id = $j_id";
    $res2 = mysqli_query($q2);
    while($row2 = mysqli_fetch_assoc($res1)) {

        echo '<tr><td>' . $row2[ac_head] . '</td>';
        echo '<td>' . $row2[amount] . '</td></tr>';

        $group_total = $group_total + $row2[amount];
        $total = $total + $row[amount];
    }

   echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}

echo '<tr><td>GRAND TOTAL</td>';
echo '<td colspan="2" align="right">' . $total . '</td></tr>';
echo "</table>";

#2


0  

njk rockz!
It worked nicely. Thanks a lot, brother - it helped me a lot, I can't explain.
Here is my final code:

njk rockz!它工作得很好。非常感谢,兄弟 - 它帮助了我很多,我无法解释。这是我的最终代码:

<tr style="background: #000; color:#fff;">
<th style="width:150px;">Particulars</th>
<th>Details</th>
<th>Amount</th>
</tr>

<tr>
<td>Opening Balance</td>
<td></td>
<td>500000</td> <!-- till not dynamic -->
</tr>

<?php
$total = 0;
$se = "SELECT * FROM group";
$res = mysql_query($se) or die (mysql_error());
while ($row = mysql_fetch_array($res))
{
?>

<tr>
<td colspan="3" style="font-weight:bold;"><?php echo $row['group']; ?></td>
</tr>

<tr>
<?php
$group_total = 0;
$se1 = "SELECT ac_head, amount FROM `acheads` WHERE `j_Id` = '".$row['id']."'";
$res1 = mysql_query($se1) or die (mysql_error());
while ($row1 = mysql_fetch_array($res1))
{
$group_total = $group_total + $row1['amount'];
?>
<td><?php echo $row1['ac_head']; ?></td>
<td><?php echo $row1['amount']; ?></td>
<td>&nbsp;</td>
</tr>

<?php
}
echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}
?>
</table>
</code>

#1


2  

I would do 2 loops:

我会做2个循环:

  1. Fetch id from GROUP
  2. 从GROUP获取ID

  3. Fetch amount from ACHEADS based on j_id
  4. 根据j_id从ACHEADS获取金额

This would look something like (non-tested code):

这看起来像(未经测试的代码):

echo '<table><tr><td>Particulars</td><td>Details</td><td>Total</td></tr>';

$total = 0;

$q1 = "SELECT id FROM `GROUP`";
$res1 = mysqli_query($q1);
while($row1 = mysqli_fetch_assoc($res1)) {
    echo 

    $group_total = 0;
    $j_id = $row1[id];

    $q2 = "SELECT ac_head, amount FROM ACHEADS WHERE j_id = $j_id";
    $res2 = mysqli_query($q2);
    while($row2 = mysqli_fetch_assoc($res1)) {

        echo '<tr><td>' . $row2[ac_head] . '</td>';
        echo '<td>' . $row2[amount] . '</td></tr>';

        $group_total = $group_total + $row2[amount];
        $total = $total + $row[amount];
    }

   echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}

echo '<tr><td>GRAND TOTAL</td>';
echo '<td colspan="2" align="right">' . $total . '</td></tr>';
echo "</table>";

#2


0  

njk rockz!
It worked nicely. Thanks a lot, brother - it helped me a lot, I can't explain.
Here is my final code:

njk rockz!它工作得很好。非常感谢,兄弟 - 它帮助了我很多,我无法解释。这是我的最终代码:

<tr style="background: #000; color:#fff;">
<th style="width:150px;">Particulars</th>
<th>Details</th>
<th>Amount</th>
</tr>

<tr>
<td>Opening Balance</td>
<td></td>
<td>500000</td> <!-- till not dynamic -->
</tr>

<?php
$total = 0;
$se = "SELECT * FROM group";
$res = mysql_query($se) or die (mysql_error());
while ($row = mysql_fetch_array($res))
{
?>

<tr>
<td colspan="3" style="font-weight:bold;"><?php echo $row['group']; ?></td>
</tr>

<tr>
<?php
$group_total = 0;
$se1 = "SELECT ac_head, amount FROM `acheads` WHERE `j_Id` = '".$row['id']."'";
$res1 = mysql_query($se1) or die (mysql_error());
while ($row1 = mysql_fetch_array($res1))
{
$group_total = $group_total + $row1['amount'];
?>
<td><?php echo $row1['ac_head']; ?></td>
<td><?php echo $row1['amount']; ?></td>
<td>&nbsp;</td>
</tr>

<?php
}
echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}
?>
</table>
</code>