【问题标题】:PHP sum values of a whilefunction一个while函数的PHP总和值
【发布时间】:2010-07-27 19:59:15
【问题描述】:

我们已经编写了这个 PHP 脚本:

function price($id)
              {
                      $ergebnis = mysql_query("SELECT * FROM preiszuordnungen where id=$id");
                           while($row = mysql_fetch_object($ergebnis))
           { 
  //Wenn grundpreis dann färben
  if($row->typ=="grundpreis")
             {
      $gp="style=\"background-color: #FF22FF; color: #FFFFFF; padding: 5px;\"";
                }
              else
             {
               }

  //preisfomat umändern
           $preis="+ ".number_format($row->preis,2)." EUR";
            $GLOBALS["$id"] = $row->preis;





             //zeile ausgeben
          echo"<div $gp class=\"rechnung_bezeichnung\">$row->bezeichnung</div><div $gp                    class=\"rechnung_preis\">$preis</div><p style=\"clear: both;\">";
             }    
       }

我们的问题是我们不能使用这个函数之外的变量。在我们的脚本中,我们需要这样的函数:

$preis=price(101);

$preis=price(909);  
         ...

现在我们要对函数的输出求和。变量 $row->price

如果有人可以帮助我们,我们很高兴。

【问题讨论】:

    标签: php function sum while-loop


    【解决方案1】:

    添加

    return $row->preis;
    

    到函数结束。

    【讨论】:

    • 是的,但是我们每个输出使用一个函数,如果发出回声,函数就会结束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多