【问题标题】:Add more then two while loop in one while with multiple tables使用多个表在一个 while 中添加两个以上的 while 循环
【发布时间】:2021-02-08 12:55:09
【问题描述】:

我想在多个表中同时尝试多个,但它不起作用。

我有三个表,我想要一个与表不同的值。

这是我的代码。

$table1 = GROUPMASTER;
$table2 = LEDGERMASTER;
$table3 = TRANSECTIONMASTER;



$groupmasterdata = mysql_query("select * from $table1 WHERE groupname = 'Capital' OR groupname = 'Fund and Reserves' OR groupname = 'Long Term liabilities' OR groupname = 'Current Liabilities' ");
echo "<table align='center' border=1>";
echo "<tr>";
echo "<td colspan=2  style='text-align:center'><b> CAPITAL and LIABILITIES</b></td>";
echo "<td style='text-align:center'><b> Amount </b></td>";
echo "</tr>";
while($recored = mysql_fetch_array($groupmasterdata))
{
    $onegroupname = $recored ['groupname'];
    echo "<tr>";
    echo "<td colspan=3><b>" . strtoupper($onegroupname) . "</b></td></tr>";
    $groupnamelist = mysql_query("select groupname from $table1 WHERE under = '". $onegroupname ."'");
    while($data = mysql_fetch_array($groupnamelist))
    {
        $gname = $data ['groupname'];
        $getledgername = mysql_query("select ledgername from WHERE groupname = '". $gname ."'");
        while($lname = mysql_fetch_array($getledgername))
        {
            $gettocashbal = mysql_query("select *, SUM(amount) AS totalto from $table3 WHERE voucherto = '". $ledgername ."'"); 
            while($datato = mysql_fetch_array($gettocashbal))
            {
                $tototal = $datato['totalto'];
            }
            
            $getbycashbal = mysql_query("select *, SUM(amount) AS totalby from $table3 WHERE voucherby = '". $ledgername ."' "); 
            while($databy = mysql_fetch_array($getbycashbal))
            {
                $bytotal = $databy['totalby'];
            }
            
            $ledgertotal = $opbal - $tototal + $bytotal;
            
            echo "<tr>";
            echo "<td class='test'>" . $gname . "</td>";
            echo "<td>Rs. $ledgertotal</td>";
            echo "<td></td>";
            echo "</tr>";
        }
    }
    echo "<tr>";
    echo "<td style='text-align:right'>Total</td>";
    echo "<td></td>";
    echo "<td>Rs.</td>";
    echo "</tr>";
}
echo "</table>";

我想尝试多个 while 但都不起作用。

我该如何解决这些问题?

【问题讨论】:

  • 也使用连接而不是多个查询
  • 您还应该阅读数据库规范化 - 似乎(尽管可能并非如此)您的第一个表在每一行中都存储了组 names。您应该有一个组表,并存储唯一 ID。
  • 好的,谢谢.. 是不是意味着我不能使用多个while循环了..?
  • 执行多个 while 并来回移动数据以连接三个表是个坏主意。只需使用 JOIN:dev.mysql.com/doc/refman/8.0/en/join.html

标签: php sql while-loop


【解决方案1】:

我尝试了不同的方法来解决这个问题...

//TABLE 1 = GROUPMASTER
$table1 = $_SESSION['table1'];
//TABLE 2 = LEDGERMASTER
$table2 = $_SESSION['table2'];
//TABLE 3 = TRANSECTIONMASTER
$table3 = $_SESSION['table3'];

echo "<table align='center' border=1>";
echo "<tr>";
echo "<td colspan=2  style='text-align:center'><b> CAPITAL and LIABILITIES</b></td>";
echo "<td style='text-align:center'><b> Amount </b></td>";
echo "</tr>";
echo "<tr>";

$liabilitytotal = 0;

    echo "<td colspan=3><b>CAPITAL</b></td></tr>";
    $categorytotal = 0;
    $groupnamelist = mysql_query("select groupname from $table1 WHERE under = 'Capital'");
    while($data = mysql_fetch_array($groupnamelist))
    {
        
        $grouptotal = 0;
        $gname = $data ['groupname'];
        echo "<tr>";
        echo "<td class='test'>" . $gname . "</td>";
        $ledgername = mysql_query("SELECT ledgername from $table2 WHERE groupname = '". $gname ."' ");
        while($datato = mysql_fetch_array($ledgername))
        {
            $lname = $datato['ledgername'];
            $getbytotalquery = mysql_query("SELECT SUM(amount) FROM $table3 WHERE voucherby = '". $lname ."' ");
            $gettototalquery = mysql_query("SELECT SUM(amount) FROM $table3 WHERE voucherto = '". $lname ."' ");
                  
                  $getopbal = mysql_result($getopbalquery,0,0);
                  $getbytotal = mysql_result($getbytotalquery,0,0);
                  $gettototal = mysql_result($gettototalquery,0,0);
                  
                  $result = $getopbal - $gettototal + $getbytotal;
          
            if ($result == null)
            {
               $result = 0;
            }
            $grouptotal = $grouptotal + $result;
        }
        $categorytotal = $categorytotal + $grouptotal;
        echo "<td style='text-align:right'>$grouptotal</td><td></td></tr>"; 
    }
    $liabilitytotal = $liabilitytotal + $categorytotal;
    echo "<tr>";
    echo "<td></td>";
    echo "<td></td>";
    echo "<td style='text-align:right'>$categorytotal</td>";
    echo "</tr>";
echo "</table>";```

I tried my best..

And I will try to also solve this query to another way...

Thank you.. I think and I hope It's helpful..

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2019-03-08
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多