Skip to main content
Topic: Page Events (Read 2971 times) previous topic - next topic

Page Events

I decide to start General discussion on page EVENT!
All of us are doing own projects but with similar stuff , so the idea of the post is to help each other, and after all to spent some time!
So:
My page event is related to table 'docs', event after ADD.php page in \app\views\partials\docs .
In general: Page events code is saved in  \app\Controllers\ in my case => DocsController.php
In Page design > page select -  Docs/Add >
I made for DocAddForm page /include page - false
Then I create my own customView and in customCode I just put this <?php include "add_inc_form.php"; ?>,
add_inc_form.php - this is my custom made form that should include and update in 3 tables, I just keep form link and EXPORT part from original add.php
So this is my after add event code

//$_POST['pcount'] gives number of cart items
        for ($i=0; $i<($_POST['pcount']); $i++){
            $ci=$i+1;
            $scr = 0;
            if(isset($_POST['product_scrap_'.$ci])){
            $scr = $_POST['product_scrap_'.$ci];
            }
//few calculations
            $value = ($_POST['product_price_'.$ci])*($_POST['product_qty_'.$ci]);

//array of $_post
 $table_data = array(
 "doc" => $_POST['doc'],
  "doc_id" => $_POST['doc_id'],
  "part_id" => $_POST['part_id'],
  "company_id" => $_POST['company_id'],
  "product_name" => $_POST["product_name_".$ci],
  "product_id" => $_POST["product_id_".$ci],
  "qty" => $_POST["product_qty_".$ci],
  "price" => $_POST["product_price_".$ci],
  "type" => $_POST["product_type_".$ci],
  "value" => $value,
  "scrap_id" => $scr
 
);
//insert into table sales
$db->insert("sales", $table_data);
}

//loop for update in quantity in product table
for ($up=0; $up<($_POST['pcount']); $up++){
$ups        = $up+1;
$qty        = $_POST['product_total_qty_'.$ups] - $_POST['product_qty_'.$ups];
$product_id = $_POST["product_id_".$ups];

$table_update = array(
    "quantity" => $qty
    );

    $db->where("id", $product_id);
    $bool = $db->update("product", $table_update);
}

// clear all cookie
$cc=get_cookie ('cart');
    clear_cookie ("partner");
    if (isset($cc)) {
        foreach ($cc as $name => $value) {
            clear_cookie ("cart[".$name."]");
        }
    }
 
As I'm not a pro, I will appreciated any advices on my code above?
If you have Page Events code that could be useful for other like me, please share...
If you have problems on Page Evens please share


 

Re: Page Events

Reply #1
Hello Again,

In order to improve my add form,  next to SUBMIT button, I want to add UPDATE button  <input type="submit" name ="update">.
Aim is to update the form and make some calculating with updated values in the controller.

So I add following code in Page Event docs/Add BeforeAdd:

//tasks:
// here should be a code that will update the form values
// here should calculate some variable coming from $_POST
// here should create Array from $_Post values
// from here the array($_post + calculated variable)  should return in docs/add
so
  if(isset($_POST['update'])){
      
$post_data = array(
 "doc" => $_POST['doc'],
  "doc_id" => $_POST['doc_id'],
  "part_id" => $_POST['part_id'],
  "company_id" => $_POST['company_id'],
);


       $this->set_flash_msg(get_lang('UPDATED! '), "success");
      return   $this->redirect("docs/add");
         }


so as you see I just create If clause,  to check if it is working !

I seek how to return the data to the add.php ?

I'm stuck on that now, advice?