在php Cakephp中计算一个数组的总数

时间:2015-03-31 17:38:59

标签: php cakephp-2.0

我想在我的网站上添加一些统计数据,我想每个月显示我的收入,当汽车从租金返回时(当天:预期返回月份)。 我想计算每月的收入总额(租金价格)(预计退货日期),有人可以帮助我,我该怎么做? enter image description here

这是View / Location / admin_index.ctp:

  
<div class="row" >
<!--<meta http-equiv="refresh" content="5; URL=http://dhafer.com/admin/locations">-->
        
     <div class="col-lg-12">
                                                
        <h1 class="page-header" style="font-family: Kaushan Script';">Administrator<small> Locations management</small></h1>
            <ol class="breadcrumb">
               <li class="active">
                  <i class="fa fa-car"></i> Locations
               </li>
            </ol>
 
        <div class="panel panel-default">
                          
                         
                <div class="panel-heading" style="font-family: Kaushan Script';"><h3> Locations </h3></div>

                     <div class="table-responsive">

                        <table class="table table-bordered table-hover table-striped" style="font-family: 'Montserrat';">
                                <thead>
                                    <tr >
                                    	 



										<th class="actions"><?php echo __('Actions'); ?></th>
										<th><?php echo $this->Paginator->sort('id'); ?></th>
										<th><?php echo $this->Paginator->sort('status'); ?></th>
										<th><?php echo $this->Paginator->sort('departure_date'); ?></th>
										<th><?php echo $this->Paginator->sort('expected_return_date'); ?></th>
										<th><?php echo $this->Paginator->sort('user_id'); ?></th>
										<th><?php echo $this->Paginator->sort('agency_id'); ?></th>
										<th><?php echo $this->Paginator->sort('car_id'); ?></th>
                    <th class="actions"><?php echo __('Decision'); ?></th>
                    <th ><?php echo __('Days'); ?></th>
                    <th ><?php echo __('Rental price '); ?></th>
                                       
                                        
                                    </tr>
                                </thead>
                                <tbody>
                                
                                

                                    
                                    <?php foreach ($locations as $location): ?>
                                    	<tr class="<?php   if($location['Location']['status']== '0'):echo "danger";else: echo "success";endif; ?>" style="color:black;">
                                    <td class="actions">
                    <?php echo $this->Html->link(' <i class="fa fa-eye" style="color:black;"></i>', array('action' => 'view', $location['Location']['id']),array('class'=>'btn-sm btn-sousse','escape' => false)); ?>
                                           

                                            <?php echo $this->Html->link('<i class="fa fa-pencil-square-o" style="color:white;"></i>', array('action' => 'edit', $location['Location']['id']),array('class'=>'btn-sm btn-primary','escape' => false)); ?>
                                            
           <?php echo $this->Html->link('<i class="fa fa-times-circle" style="color:white;"></i>', array('action' => 'delete', $location['Location']['id']),array('class'=>'btn-sm btn-slama','escape' => false), array(), __('Are you sure you want to delete # %s?', $location['Location']['id'])); ?>
                                        </td>
                                        <td style="font-family: 'Montserrat';"><?php echo $this->Html->link($location['User']['id'], array('controller' => 'users', 'action' => 'view', $location['User']['id'])); ?></td>
                                        <td style="font-family: 'Montserrat';">
<?php
                    if( $location['Location']['status'] != 0){ 
                    echo __('Accepted');}else{
                    echo __('Waiting response');
                    }
                     ?>
                                      </td>
                                        <td style="font-family: 'Montserrat';"><?php echo h($location['Location']['departure_date']); ?></td>
                                        
                                        <td style="font-family: 'Montserrat';"><?php echo h($location['Location']['expected_return_date']); ?></td>
                                        <td style="font-family: 'Montserrat';"><?php echo h($location['User']['first_name']); ?></td>
                                        
                                        <td style="font-family: 'Montserrat';"><?php echo $this->Html->link($location['Agency']['name'], array('controller' => 'agencies', 'action' => 'view', $location['Agency']['id'])); ?></td>
                                        
                                       
                                        
                                        <td style="font-family: 'Montserrat';"><?php echo $this->Html->link($location['Car']['title'], array('controller' => 'cars', 'action' => 'view', $location['Car']['id'])); ?></td>
                                       <td style="font-family: 'Montserrat';">
<?php
                    if( $location['Location']['status'] != 0){ 
                    echo $this->Html->link(    "Delete", array('action'=>'delete', $location['Location']['id']),array('class'=>'btn btn-danger'));}else{
                    echo $this->Html->link(    "Accept", array('action'=>'activate', $location['Location']['id']),array('class'=>'btn btn-success'));
                    }
                     ?>
                                      </td>
<td style="font-family: 'Montserrat';">
                                      <?php 


 

 $temp1=$location['Location']['expected_return_date'];
 $temp2 = $location['Location']['departure_date'];

$date1 = new DateTime($temp1);
$date2 = new DateTime($temp2);

$diff=$date1->diff($date2);
$days=$diff->format('%a days');
echo $days ;

 


   ?></td><td style="font-family: 'Montserrat';">
     
     <?php


            echo '$ '.$location['Car']['price'] * $days;


         ;?>
   </td>
                                        
                                    </tr>

                                 <?php endforeach; ?>
                                   
                               
                                </tbody>
                          </table>
                          <p>
                            <?php
                            echo $this->Paginator->counter(array(
                            'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
                            ));
                            ?>  </p>
                            <div class="paging">
                            <?php
                                echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
                                echo $this->Paginator->numbers(array('separator' => ''));
                                echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
                            ?>
                            </div>
                     </div>
                 </div>
                 <?php echo $this->Html->link(' <i class="fa fa-rss-square fa-1x" style="color:orange;"></i>', array('action' => 'admin_getlocations.xml'),array('class'=>'btn-lg btn-default','escape' => false)); ?>
              </div>















            </div>




 <script type="text/javascript">
 //setTimeout(function(){
   //window.location.reload(1);
//}, 10000);


 </script>

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我相信您应该通过使用日期/时间函数执行SUM()COUNT()查询来统计控制器中的统计信息(除非您每页需要它)。有很多非常强大的功能可以帮助您计算日期间隔。

http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html

(我假设您使用的是mysql或类似的东西。)

请添加一些相关的DB结构,这样我就可以帮助您构建查询。