使用循环中的当前变量更新上一个变量

时间:2014-11-28 15:22:33

标签: php mysql

我在某些流程下有“优惠价”商品;

PROCESS 1
index no 0      price offer a   count=1 ($selected_price_offers_cnt)

PROCESS 2
index no 1      price offer b   count=3     (3+1=4)
index no 2      price offer c   count=3     (3+1=4)
index no 3      price offer d   count=3     (3+1=4) 3=selected_price_offers_cnt, 1=$previous, 4=$next 


PROCESS 3
index no 4      price offer e   count=2     (2+4=6)
index no 5      price offer f   count=2     (2+4=6) 2=selected_price_offers_cnt, 4=$previous, 6=$next

我需要做的是仅在每个过程的最后价格优惠项下方打印“比例小计”。换句话说,低于 a d f

为了做到这一点,我尝试将最初的价格优惠计数从开头(1,4和6)与当前行索引no + 1进行比较。 如果我们到达PROCESS X的最后一个报价,它的计数将是==索引号+ 1(就像处理报价d计数4 =索引号3 + 1)。 因此,我需要使用$ next更新$ previous变量,以用于以下PROCESS Y.

我的代码需要非常长的时间来完成执行,最后它只打印一个销售子总线。 (由于此代码在Drupal视图全局php字段中使用,SQL查询被安排在那里工作,它们返回正确的值,没有问题。) 除了while循环之外的任何方法也受到欢迎。

<?php 
$process_nid = $row->nid_3;
$current_index_no = $view->row_index; //the number of the row that is being processed

$selected_price_offers = db_query("SELECT entity_id FROM drupal_field_data_field_process WHERE bundle = 'price_offer' AND deleted = '0' AND field_process_nid = {$process_nid} AND entity_id IN (SELECT content_id FROM drupal_flag_counts WHERE fid='18' AND content_type='node' AND COUNT = '1')");
$selected_price_offers_cnt = db_query("SELECT COUNT(entity_id) FROM drupal_field_data_field_process WHERE bundle = 'price_offer' AND deleted = '0' AND field_process_nid = {$process_nid} AND entity_id IN (SELECT content_id FROM drupal_flag_counts WHERE fid='18' AND content_type='node' AND COUNT = '1')")->fetchField();

$previous = 0;
while($current_index_no) { 

$next = $selected_price_offers_cnt + $previous; // add the previous to the current amount

if ($next == ($current_index_no+1)) {

$previous = $next; // save the new price offer count to the $previous in order to use it at the next row

// this part below works as expected
$sum1 = 0;
foreach($selected_price_offers as $item) {
$po_item= $item->entity_id;
$scale1 = db_query("SELECT field_sub_total_scale_1_value FROM drupal_field_data_field_sub_total_scale_1 WHERE bundle = 'price_offer' AND deleted = '0' AND entity_id = {$po_item}")->fetchField();
  $sum1+= $scale1;
}
  print "scale sub total:&nbsp;".number_format($sum1,2,",","."). " TL";
}
else {
echo "";
}
}
?>

0 个答案:

没有答案