dongzhanlian6289 2013-01-04 07:02
浏览 18
已采纳

它不会总结,我的代码有什么问题?

I want to be able to sum up all the revenue that is being displayed in the page and it auto sums every time I added another data to the revenue column:

Following is my code :

<?php 
   require_once('Connections/connect.php');
   $id_customer = mysql_real_escape_string($_GET['id_customer']);                                   
   $sql_PK = "SELECT * FROM tbl_delivery_details WHERE tbl_customer_id_customer =    {$id_customer}";
   $PK = mysql_query($sql_PK, $connect);
   if ( mysql_error() ) {
      die ( mysql_error());
   }
   $row_PK = mysql_fetch_assoc($PK);            
   $customer_name = $row_PK['tbl_customer_id_customer'];
   $customer_name = mysql_real_escape_string($customer_name);                       

   $sql = "SELECT tbl_customer.customer_name, 
       tbl_delivery_details.delivery_details_route, 
       tbl_delivery_details.delivery_details_destination, 
       tbl_delivery_details.delivery_details_van_no, 
       tbl_delivery_details.delivery_details_waybill_no, 
       tbl_delivery_details.delivery_details_charge_invoice,
       tbl_delivery_details.delivery_details_revenue,
       tbl_delivery_details.delivery_details_strip_stuff,
       tbl_delivery_details.delivery_details_date           
       FROM tbl_customer, tbl_delivery_details          
       WHERE tbl_customer.id_customer = tbl_delivery_details.tbl_customer_id_customer 
       AND tbl_customer.id_customer = '{$customer_name}'";

       $res = mysql_query($sql) or die(mysql_error());
       $row = mysql_fetch_array($res);
       $sum = 0;

?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/x   html">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Customer Revenue</title>
    <link rel="stylesheet" type="text/css" href="qcc.css"/>
    </head>             
    <body>              
    <table border="1">
      <tr>
         <th>Reveneu</th>                                                 
      </tr>
      <?php do { ?>
          <tr>                          
              <td><?php echo $row_PK['delivery_details_revenue'];?></td>                                                       
      </tr>
      <?php } while ($row_PK = mysql_fetch_assoc($PK));?>
      <?php { ?>
        <?php  $sum+=$row_PK['delivery_details_revenue'] ?>
      <?php } ?>

      </table>

      <?php echo $sum; ?>
    </body>
  </html>

When I load the page echo $sum always is zero how to correctly sum up the column I made that it will sum automatically if I add another data to it :

  • 写回答

3条回答 默认 最新

  • dtnat7146 2013-01-04 10:40
    关注

    If I read this correctly, you're summing up the values outside of your while loop. That won't work.

    I think you're mixing up a normal while loop, and a 'do while' loop.

    See this code:

          <?php do { ?>
              <tr>                          
                  <td><?php echo $row_PK['delivery_details_revenue'];?></td>                                                       
          </tr>
          <?php } while ($row_PK = mysql_fetch_assoc($PK));?>
          <?php { ?>
            <?php  $sum+=$row_PK['delivery_details_revenue'] ?>
          <?php } ?>
    

    It should be more along these lines:

          <?php do { ?>
              <tr>                          
                  <td><?php 
           echo $row_PK['delivery_details_revenue'];
           $sum+=$row_PK['delivery_details_revenue'] 
           ?>
          </td></tr>
          <?php } while ($row_PK = mysql_fetch_assoc($PK));?>
    

    this wouldn't happen if you would write the code a bit more clearly; try to avoid interleaving html and php so much:

    <?php 
      do { 
        $revenue = $row_PK['delivery_details_revenue'];
        $sum += revenue;
        println("<tr><td>$revenue</td></tr>");
      } while ($row_PK = mysql_fetch_assoc($PK));
    ?>
    

    This is a lot clearer, if you ask me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)