⚠ In case you've missed it we are migrating 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 forum is read-only and soon will be archived. ⚠


How to divide two numbers from a field and insert into the third field


  • Please log in to reply
11 replies to this topic

#1 mahadir

mahadir

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 15 July 2015 - 04:47 AM

Hi.. im totally new in php and using grocerycrud is so easy... until i need to perform arithemetic operation and insert the result into other field. Below is the example:

 

Table : arithmetic

field : num1,num2 and num3

 

i need to ' add(num1+num2)/2 and store the result into field num3.

 

how am i going to put this into line of code and run in grocery crud..

 

 

thanks for your help.

 



#2 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 15 July 2015 - 11:01 AM

Are u waiting that anybody write all code for u?


How many fields u will output? Only num1 and num2 or num3 too?

Use callback_before_insert/callback_before_update



#3 mahadir

mahadir

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 15 July 2015 - 04:55 PM

No... Im not waiting for anybody to do it for me.... To tell you frankly.. I've been trying to solve this problem for 5 days without thinking to post at this forum... This is a lame question... Yeah... I know.... But i've been a way toooo long in the programming world... Vb 5 is my last programming i ever touched.... Thanks for your idea anyway.

#4 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 15 July 2015 - 09:22 PM

For me this is very easy task and I could help u but I need more information - controller code, mb db shema from phpmyadmin and the excatly logic that u want to achieve

For, example, u have 2 fields on insert/update state, then u write them and press save, next u can set third hidden field where u in callback before operation  function calculate what u need

or without hidden field just update/insert using callback after (insert/update) with pure update query to the database. That is it:)



#5 mahadir

mahadir

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 16 July 2015 - 02:28 AM

Ahh... Let me try do it by myself based on what you have mentioned... I can learn more by doing this way.... Thanks very much indeed... Anything will be post soon.... 👍

#6 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 16 July 2015 - 08:50 AM

you're welcome!



#7 mahadir

mahadir

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 20 July 2015 - 07:54 AM

Hi... triedarrow-10x10.png... but couldn't getarrow-10x10.png it right... can you showarrow-10x10.png me where did i do wrong in my coding...

 

This is my phpmyadmin... Sorry ... some of the words are based on my native language...

 

*phpmyadmin1.gif

 

 

As you can see below... i need to divide the 'Jumlah_Pengeluaran' with 'Jumlah_Tandan' and stored the resultarrow-10x10.png in 'Nisbah_Pengeluaran'.

 

 

*phpmyadmin2.gif

 

 

 

and here is my coding ..

 

*phpmyadmin3.gif

 

 

To sum up...

 

1) I want to divide [Jumlah_Pengeluaran] with [Jumlah_Tandan] and insert the resultarrow-10x10.png in [Nisbah_Pengeluaran]

 

2) How to hide the [Nisbah_Pengeluaran] during the Add newarrow-10x10.png record because logically, [Nisbah_Pengeluaran] value is automaticallyarrow-10x10.png being inserted.

 

 

Thanks indeed.

 

 

Attached Thumbnails

  • phpmyadmin2.gif
  • phpmyadmin1.gif
  • phpmyadmin3.gif


#8 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 20 July 2015 - 09:18 AM

 1) Set [Nisbah_Pengeluaran]

 

       $crud->fields(...,'Nisbah_Pengeluaran',...)

                ->field_type('Nisbah_Pengeluaran','invisible');

2) In callback

 

public function _before_insert($post_array)
{

      if(isset($post_array['Jumlah_Pengeluaran'],$post_array['Jumlah_Tandan']))
      {
          $post_array['Nisbah_Pengeluaran'] = $post_array['Jumlah_Pengeluaran'] / ['Jumlah_Tandan'];

      }

      return $post_array;
}



#9 mahadir

mahadir

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 21 July 2015 - 03:08 AM

Thanks for your reply.

 

It works for the 'invisible fields' but not for the logic operations. I can add the records but nothing happen on the logic side. It just put zero instead of the division result... Am i doing it wrongly.. here is my snapshot for the coding i made..my coding.jpg



#10 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 21 July 2015 - 09:50 AM

Friend, you need to write right callback function and method in GC. Check if it working!



#11 mahadir

mahadir

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 22 July 2015 - 02:18 AM

Pal... It works correctly and nicely... Thanks for your assistance.

#12 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 22 July 2015 - 08:23 AM

you're welcome






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users