title: I need to choose more values from some tasks that belong to same project
body: <p><strong>My question is:</strong> Is there any other method to select the values I need from all the tasks that belongs to same project and sum them?  Other method that I tried in the <strong>down description</strong>?</p>
  <p><strong>Description:</strong></p>
  <p>I have an app where I can create a Project and, inside the Project, I can create Tasks. For every Task, I can add a percent and calculate a price (example picture 1).</p>
  <p>Then I have a report page where I want to calculate all the prices from all of the Tasks of same project (example picture 2).</p>
  <p>My problem is when I want to select all the prices from all the tasks that belongs to same projects and sum them in the table &quot;price&quot; of report page.
  For the moment I tried a method with the code I put down, and it's not working as I need, because it take the correct value from the first task of the project but dont take corect value from the ohther tasks from the same project</p>
  <p>picture 1
  <a href="https://i.stack.imgur.com/c3KQr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c3KQr.png" alt="picture 1" /></a></p>
  <p>picture 2 (but here the problem is that just the first array is corrent the other are not)
  <a href="https://i.stack.imgur.com/LjoEK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LjoEK.png" alt="enter image description here" /></a></p>
  <p>code I use ( I commented in the code with &quot;the code that doesnt work&quot; the part that make the sum) :</p>
  <pre><code>&lt;?php
  
  
  
  defined('BASEPATH') or exit('No direct script access allowed');
  
  
  
  $hasPermissionEdit   = has_permission('tasks', '', 'edit');
  
  $hasPermissionDelete = has_permission('tasks', '', 'delete');
  
  $tasksPriorities     = get_tasks_priorities();
  
  
  
  $aColumns = [
  
      db_prefix() . 'tasks.id as id',
  
      db_prefix() . 'tasks.name as task_name',
  
      'count(tbltasks.id) as totaltasks',
  
      //'(SELECT GROUP_CONCAT(name SEPARATOR &quot;,&quot;) FROM ' . db_prefix() . 'tasks) as itemlist',
  
  ];
  
  
  
  $sIndexColumn = 'id';
  
  $sTable       = db_prefix() . 'tasks';
  
  
  
  $where = [];
  
  $join  = [];
  
  $sGroupBy = ' group by '.db_prefix() . 'tasks.rel_id';
  
  
  
  // =============== Filter =================
  
      if (isset($ts_filter_data['period-from']) &amp;&amp; $ts_filter_data['period-from'] != '' &amp;&amp; isset($ts_filter_data['period-to']) &amp;&amp; $ts_filter_data['period-to'] != '') {
  
  
  
          $ts_filter_from          = to_sql_date($ts_filter_data['period-from']);
  
          $ts_filter_from          = date('Y-m-d', strtotime($ts_filter_from));
  
          $ts_filter_to            = to_sql_date($ts_filter_data['period-to']);
  
          $ts_filter_to            = date('Y-m-d', strtotime($ts_filter_to));
  
      }
  
  
  
      if (isset($ts_filter_data['this_month']) &amp;&amp; $ts_filter_data['this_month'] != '') {
  
          $ts_filter_from = date('Y-m-01'); 
  
          $ts_filter_to  = date('Y-m-t 23:59:59');
  
      }
  
  
  
      if (isset($ts_filter_data['last_month']) &amp;&amp; $ts_filter_data['last_month'] != '') {
  
          $ts_filter_from = date('Y-m-01', strtotime('-1 MONTH'));; 
  
          $ts_filter_to  = date('Y-m-t 23:59:59', strtotime('-1 MONTH'));
  
      }
  
  
  
      if (isset($ts_filter_data['this_week']) &amp;&amp; $ts_filter_data['this_week'] != '') {
  
          $ts_filter_from = date('Y-m-d', strtotime('monday this week'));
  
          $ts_filter_to  = date('Y-m-d 23:59:59', strtotime('sunday this week'));
  
      }
  
  
  
      if (isset($ts_filter_data['last_week']) &amp;&amp; $ts_filter_data['last_week'] != '') {
  
          $ts_filter_from = date('Y-m-d', strtotime('monday last week'));
  
          $ts_filter_to  = date('Y-m-d 23:59:59', strtotime('sunday last week'));
  
      }
  
  
  
      if($ts_filter_from){
  
          array_push($where, 'AND '.db_prefix().'tasks.startdate &gt;=&quot;'.$ts_filter_from.'&quot;');
  
      }
  
  
  
      if($ts_filter_to){
  
          array_push($where, 'AND '.db_prefix().'tasks.startdate &lt;=&quot;'.$ts_filter_to.'&quot;');
  
      }
  
  
  
  // =============== Filter =================
  
  
  
  array_push($where, 'AND '.db_prefix().'tasks.rel_type=&quot;project&quot;');
  
  // Script By Dev Websyms
  
  $aColumns = hooks()-&gt;apply_filters('tasks_table_sql_columns', $aColumns);
  
  
  
  // Fix for big queries. Some hosting have max_join_limit
  
  if (count($custom_fields) &gt; 4) {
  
      @$this-&gt;ci-&gt;db-&gt;query('SET SQL_BIG_SELECTS=1');
  
  }
  
  
  
  $result = data_tables_init(
  
      $aColumns,
  
      $sIndexColumn,
  
      $sTable,
  
      $join,
  
      $where,
  
      [
  
          'tbltasks.rel_type',
  
          'tbltasks.rel_id',
  
          'tbltasks.task_item_percentage',
  
          'tbltasks.task_duration',
  
          tasks_rel_name_select_query() . ' as rel_name',
  
  
  
          get_sql_select_task_assignees_ids() . ' as assignees_ids',
  
  
  
          '(SELECT staffid FROM ' . db_prefix() . 'task_assigned WHERE taskid=' . db_prefix() . 'tasks.id AND staffid=' . get_staff_user_id() . ') as is_assigned',
  
      ],
  
      $sGroupBy
  
  );
  
  
  
  $output  = $result['output'];
  
  $rResult = $result['rResult'];
  
  
  
  $loop = 1;
  
  foreach ($rResult as $aRow) {
  
      $row = [];
  
      $projectTaskIds = get_assign_project_task($aRow['rel_id']);
  
      $assignees_ids = $aRow['assignees_ids'];
  
      $assignees_id = explode(',', $assignees_ids);
  
      $calA = get_task_user_hourly_rate($assignees_id[0]); // hourly_rate
  
  
      if($projectTaskIds &amp;&amp; count($projectTaskIds)){
  
      $realTime = 0;
      $estimateCost = 0;
      $itemName = '';
      $realCost = 0;
      $realCostArr = array();
      $totalCalEArr = array();
  
      foreach ($projectTaskIds as $key =&gt; $projectTaskId) {
  
          // echo $projectTaskId['id'];
          // echo &quot;&lt;br&gt;&quot;;
          $calB = 0;
          $calC = 0;
          
          $itemtotal = 0;
          $items = get_items_by_type('task', $projectTaskId['id']);
          if($items &amp;&amp; count($items) &gt; 0){
              $itemNo = 1;
              foreach ($items as $key =&gt; $item) {
                  $itemtotal += ($item[&quot;rate&quot;]*$item[&quot;qty&quot;]);
                  $itemName .= '&lt;div&gt;'.$itemNo.'.'.$item[&quot;description&quot;].' &lt;b&gt;('.round($item[&quot;qty&quot;]).')&lt;/b&gt;'.'&lt;/div&gt;';
                  $itemNo++;
              }
          }
              
           // =============== the code that doesn't work ============
          $calF = get_task_user_hourly_rate($assignees_id[0]); // hourly_rate
          $calG = get_task_custom_billable_amount($aRow['id']);
          
          $calE = ($itemtotal+$aRow['task_item_manual_total_price']-$itemtotal);
          if(isset($aRow['task_item_percentage'])){
              $calE = ((round($itemtotal + ((($calF * $aRow['task_duration'] / 60))))
                        *($aRow['task_item_percentage']/100)))
                  +(round($itemtotal + (($calF * $aRow['task_duration'] / 60)))); // additionalPriceTotal
           
          }
          
          $totalCalEArr[] = $calE;
          
          if($totalCalEArr &amp;&amp; count($totalCalEArr) &gt; 0){
              $totalsCalEaMount = 0;
              foreach ($totalCalEArr as $key =&gt; $totalvalue) {
                  $totalsCalEaMount += $totalvalue;
              }
          }
          // =============== the code that doesnt work ============
  
          $realTime += get_calc_task_real_logged_time($projectTaskId['id']);
          $calB = get_task_custom_billable_amount($projectTaskId['id']);
          
          if($aRow['task_item_percentage']){
              $calC = ((round($itemtotal + ((($calF * $aRow['task_duration'] / 60))))*($aRow['task_item_percentage']/100)))+(round($itemtotal + (($calF * $aRow['task_duration'] / 60)))); // additionalPriceTotal
          } else {
              $calC = ($itemtotal+$aRow['task_item_manual_total_price']-$itemtotal);
          }
  
          $estimateCost += round($itemtotal+($calA * $aRow['task_duration'] / 60)); 
          $realCost = round($itemtotal+$calB);
          
  //           if($calC = 0){  $realCost = round($itemtotal+$calB);} else { $realCost = round($itemtotal+$calB+$calC);
  // }
          $realCostArr[] = $realCost;
  
      }
      if($realCostArr &amp;&amp; count($realCostArr) &gt; 0){
          $realCostAmount = 0;
          foreach ($realCostArr as $key =&gt; $reslcostvalue) {
              $realCostAmount += $reslcostvalue;
          }
      }
  }
  
  $outputName = '';
  $relName = '';
  $row[] = $loop;
  
  if ($aRow['rel_name']) {
      $relName = task_rel_name($aRow['rel_name'], $aRow['rel_id'], $aRow['rel_type']);
      $link = task_rel_link($aRow['rel_id'], $aRow['rel_type']);
      $relName = '&lt;span class=&quot;hide&quot;&gt; - &lt;/span&gt;&lt;a class=&quot;text-muted task-table-related&quot; data-toggle=&quot;tooltip&quot; title=&quot;' . _l('task_related_to') . '&quot; href=&quot;' . $link . '&quot;&gt;' . $relName . '&lt;/a&gt;';
  }
  
  $row[] = $relName;
  $row[] = count($projectTaskIds);
  $row[] = $itemName;
  $row[] = $aRow['task_duration']*count($projectTaskIds);
  $row[] = round($realTime/60);
  $row[] = round($estimateCost);
  $row[] = round($realCostAmount);
  $row[] = '';
  $row[] = $totalCalEArr; // display as array or 
  //$row[] = $totalsCalEaMount; //to display as a sum
  $output['aaData'][] = $row;
  $loop++;
  }
  </code></pre>
  <p>And if you need more details you can find a guide how to use it: <a href="http://amco.ro/pdf/Error%20presentation.pdf" rel="nofollow noreferrer">http://amco.ro/pdf/Error%20presentation.pdf</a></p>