⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

how to compute average/avg on callback_column



DREON
  • profile picture
  • Member

Posted 06 May 2013 - 07:56 AM

hi to all masters in CI and GC, im new to CI and GC

 

i have question, i have table called rating with fields

 

rating1 |   rating2 | rating3

 

and i want to compute the avg of all my 3 fields that show on my

 

$c->callback_column('Total AVG',array($this,'total_avg'));

$c->callback_column('Interpretation',array($this,'interpretation'));

 

---------------------------------------------------------------------------------

public function total_avg($row)
{
   ?: i dont know what code i put here to get that result.. please help guys
}
 

-------------------------------------------------------------------------------

public function interpretation($row)
{
   ?: i dont know what code i put here to get that result.. please help guys


}

 

example output/result on my GC table grid:

rating 1        |         rating 2 |                rating 3|         TOTAL AVG |       Interpretation

93                              93                        93                     93                           superior

 

 


heruprambadi
  • profile picture
  • Member

Posted 07 May 2013 - 07:49 AM

try this :

function  total_avg($value, $row)
    {
        $avg= ($row->rating_1 + $row->rating_2 + $row->rating_3) / 3;
        return $avg;
    }


DREON
  • profile picture
  • Member

Posted 07 May 2013 - 12:55 PM

sir heruprambadi, thank you for your help but i try also to do that

 

but the problem is what if the the rating_1 is blank or 0, the result is not correct..

 

how can i get agv using codeigniter.


davidoster
  • profile picture
  • Member

Posted 07 May 2013 - 14:01 PM

if($row->rating_1 == null) $row->rating_1 = 0;


DREON
  • profile picture
  • Member

Posted 07 May 2013 - 14:42 PM

sir david thank you for your help

 

can u give an exact code or example? thank you sir..

 

im new to CI and GC.


heruprambadi
  • profile picture
  • Member

Posted 07 May 2013 - 14:58 PM

really ? i think if rating_1 = 0, it still divide by 3.

example : 

rating_1 | rating_2 | rating_3

    0        |       3      |     3

 

the average is = 2.

right ?

 

or can you gime me your screenshot ?


DREON
  • profile picture
  • Member

Posted 07 May 2013 - 15:17 PM

yes sir..the result is the same when 1 digit:

 

but i try this

 

rating_1 | rating_2 | rating_3

    0        |       95      |     96

 

when u divide it on 3 the answer is 63.66

 

correct answer :95.5 this is answer when u divide on 2..

 

but what if i have  0 rating of one or two of my row..


heruprambadi
  • profile picture
  • Member

Posted 07 May 2013 - 15:38 PM

i think althogh the results like :

 

 

rating_1 | rating_2 | rating_3

    0        |       0      |     96

 

it still divide by 3. because, if you calculate for average, it still divide by 3. not just row with value.

am i wrong ?


DREON
  • profile picture
  • Member

Posted 07 May 2013 - 22:43 PM

rating_1 | rating_2 | rating_3


    0        |       0      |     96

 

the result should be : 96

 

 

try to put on excel with 3 fields:

example:

A     | B |       C|         D|

96      93       93       =Average(A1,B1,C1)

 

the result is 94..

 

and even if you put blank on 1one of the row: like example below:

example:

A     | B |       C|         D|

96      93             =Average(A1,B1,C1)

 

the result is 94.5

 

on excel when u put 0 one of the row the result is 63 which is wrong.

but when u leave it black the result is 94.5 which is i want.

on your given code above even i blank one of the row the result is 63..


davidoster
  • profile picture
  • Member

Posted 07 May 2013 - 22:54 PM

Then count the non blank numbers and divide by this number.

Excel thinks that there is no number there and this is why you get 94.5.

If you ask me this is wrong but anyway...


DREON
  • profile picture
  • Member

Posted 07 May 2013 - 23:11 PM

sir david thank you, but what code?

 

because on my project:

 

somtimes all my rows field up and theres a chance only 2 rows are field up, and a chance 1 row..

 

how to get that?


davidoster
  • profile picture
  • Member

Posted 07 May 2013 - 23:26 PM

/topic/1644-how-to-compute-averageavg-on-callback-column/#entry7201


DREON
  • profile picture
  • Member

Posted 07 May 2013 - 23:39 PM

but the problem is what if the the rating_1 is blank or 0, the result is not correct..


davidoster
  • profile picture
  • Member

Posted 08 May 2013 - 00:22 AM

function total_avg($value, $row)
{
$divider = 0;
if($row->rating_1 != 0 && $row->rating_1 != null) $divider += 1;
if($row->rating_2 != 0 && $row->rating_2 != null) $divider += 1;
if($row->rating_3 != 0 && $row->rating_3 != null) $divider += 1;
if($divider == 0) $divider = 1;
$avg = ($row->rating_1 + $row->rating_2 + $row->rating_3) / $divider;
return $avg;
}

 

 

This code is elementary! This code is for people starting to learn a programming language like PHP (or C).

Since you can't right this code by yourself you need to get some programming lessons.

These questions are not in any way related to Grocery CRUD and shouldn't be asked or answered here!


DREON
  • profile picture
  • Member

Posted 08 May 2013 - 00:26 AM

thank you sir and sorry..


DREON
  • profile picture
  • Member

Posted 08 May 2013 - 02:38 AM

for the reference of the others: for my second question on my first post i got the result:

 

thnak you sir david for the big help

 

if ($avg >= 96)
           {
          $remarks = "Outstanding";
        }
         else if ($avg >= 91)
           {
          $remarks = "Superior";
        }
         else if ($avg >= 86)
           {
          $remarks = "Very Satisfactory";
        }
         else if ($avg >= 81)
           {
          $remarks = "Satisfactory";
        }
        else if ($avg >= 76)
           {
          $remarks = "Minimally Santisfactory";
        }
        else if ($avg <= 75)
           {
          $remarks = "Needs Improvement";
        }
    
        return $remarks;


seth
  • profile picture
  • Member

Posted 30 March 2017 - 14:02 PM

is this topic resolved DREON? I have the same problem on how to output an average of two values in rows... I am also new in CI and GC Can you share the code thanks!