【问题标题】:php html table queryphp html表格查询
【发布时间】:2012-11-04 00:25:41
【问题描述】:

我在这方面已经有一段时间了,只是想不通。我有一个 html 表,必须找到 $total/Sale Cost (last) 行中的值的总和。任何人都可以帮助我或指出正确的方向。

代码如下:

<?php

print ("<table border=1><tr><th>Sale Number</th><th>Item</th><th>Item Cost</th><th>Units Sold</th><th>Sale Cost</th></tr>");

    for($x=0; $x<=20; $x=$x+1) {

        /*
        $sofa_total = 0;
        $table_total = 0;
        $chair_total = 0;
        $box_total = 0;
        $tablecloth_total = 0;
        */

        $amount = rand(1,10);
        $res = rand(1, 5);
        $object = 0;
        $total = $amount * $cost;




                if ($res == 1) {
                            $cost = 235.00;
                            $item = "sofa";
                            }
                        elseif ($res == 2) {
                            $cost = 125.00;
                            $item = "table";
                                }
                        elseif ($res == 3) {
                            $cost = 25.99;
                            $item = "chair";
                                }
                        elseif ($res == 4) {
                            $cost = 15.25;
                            $item = "box";
                                }
                        elseif ($res == 5) {
                            $cost = 23.50;
                            $item = "tablecloth";
                                }

        print ("<tr>");
             print ("<td>" . $table[$x][0] = $x+1 . "</td>");
             print ("<td>" . $table[$x][1] = $item . "</td>");
             print ("<td>" . $table[$x][2] = $cost . "</td>");
             print ("<td>" . $table[$x][3] = $amount . "</td>");
             print ("<td>" . $table[$x][4] = $total  . "</td>");
             }
        print ("</tr>");

                if ($object==0) {

                    $sofa_total = $sofa_total + $amount;

                    } elseif ($object==1) {

                    $table_total = $table_total + $amount;

                    } elseif ($object==2) {

                    $chair_total = $chair_total + $amount;

                    } elseif ($object==3) {

                    $box_total = $box_total + $amount;

                    } elseif ($object==4) {

                    $tablecloth_total = $tablecloth_total + $amount;

                    }


print ("</table>"); 

?>

【问题讨论】:

  • 当我尝试您的代码时,我收到两个错误: 注意:未定义变量:成本 ... 第 18 行 注意:未定义变量:sofa_total ... 第 55 行 - 也许先尝试修复这些错误? :)

标签: php html loops for-loop html-table


【解决方案1】:

让我猜猜……桌子上全是1s?

当您打印 &lt;td&gt;s 时,您应该传入一个值,而不是一个 espression。对表达式求值,并在您的情况下返回 1,因为它们是成功的(它们是赋值)。

除了采用这种方法,您可以保留计数器并在每个周期将它们相加,在另一个语句中打印当前值,最后在最后一个周期回显(打印)计数器。

【讨论】:

    【解决方案2】:

    通过将当前实例的值添加到前一个实例的值来循环……

    for( your loop){
        $sampleCost = $sampleCost + $currentCost;
    }
    echo $sampleCost; /*another way of displaying item*/
    

    这个想法是不断增加价值。确保您考虑数据,尤其是带小数的数据。

    $currentCost; /* value from the query*/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多