Init hook question

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
Yilmaz
Posts: 22
Joined: 2013-01-24 16:58

Init hook question

Post by Yilmaz » 2019-07-24 07:02

Dear Appgini forum,

does anyone an idea for solving the following problem:

- I'm using Appgini 5.75
- Within my Appgini application I have a table, let's call it products.
- This table has the fields: ID, CREATEDDATE, VALID_TO, STATUS,...
- ID: primary key, CREATEDDATE: date, VALID_TO: date, STATUS: varchar (10)
- I would like to update the STATUS field every time when a user calls the Table views from "products" as follows:
>> If ((the date from today) - (VALID_TO)) < 0 then STATUS = "OK"
>> If ((the date from today) - (VALID_TO)) => 0 then STATUS = "NOT OK"

- I tried to implement the condition above in the tablename_init hook, so when the Tableview from the table "products" is called the STATUS fields are showing the updated values (OK, NOT OK etc.)
- But however I could not find a way how to retrieve each data from the table "products" within the foreach loop to implement the conditions above and update the STATUS field!

Anyone a hint for me?

PS:
1. The tutorial under https://bigprof.com/appgini/tips-and-tu ... eview-data was unfortunately not a help for me ;-(
2. I did not understand how to use the DataList-object under https://bigprof.com/appgini/help/advanc ... ist-object

Thx a lot
Yilmaz

onoehring
Veteran Member
Posts: 263
Joined: 2019-05-21 22:42
Contact:

Re: Init hook question

Post by onoehring » 2019-07-24 09:10

Hi Yilmaz,

I would think that it depends on how you want to accomplish your task, if this hook is the correct one.
Using this hook: Why not check recalculate all fields (maybe limit to once a day with an extra field in some table of yours) using php?
I suggest something like this (not tested code, just written down here):

Code: Select all

$res = sql("Select * from products", $eo);
while($row = db_fetch_assoc($res)){
  If (date("Ymd")) - (date($format, $row['VALID_TO']))) < 0 {
    $STATUS = "OK";
  }
  else 
  {
    $STATUS = "NOT OK";
  }
  $res_up1 = sql("UPDATE products SET STATUS= '" . $STATUS, $eo);
}
Olaf

Yilmaz
Posts: 22
Joined: 2013-01-24 16:58

SOLVED: Re: Init hook question

Post by Yilmaz » 2019-07-24 13:57

Thx a lot for your hint Olaf! Great idea!

I've done it using php and (like your suggestion) per cronjob executing the php file once a day:

Code: Select all

$select_abfrage = "SELECT * FROM loans";
$select_ergebnis = mysqli_query($connect, $select_abfrage);


while($dsatz = mysqli_fetch_assoc($select_ergebnis)){
	$differenz = strtotime($dsatz['loandate_till']) - strtotime(date("m/d/Y"));
	$resttage = $differenz/86400;

	If ($resttage >= 5){
		$statusquo = "green";
	}elseif (($resttage > 0) && ($resttage < 5)) {
		$statusquo = "orange";
	} else{
		$statusquo = "red";
	}
$id = $dsatz["id"];
$update_abfrage = "UPDATE loans SET status = '$statusquo' WHERE id = '$id'";
mysqli_query($connect, $update_abfrage);

}
Kind regards,
Yilmaz

onoehring
Veteran Member
Posts: 263
Joined: 2019-05-21 22:42
Contact:

Re: Init hook question

Post by onoehring » 2019-07-24 18:04

Hi Yilmaz,

great to hear this worked.
Btw. do you know that, since you use color coding now, you might add this to your table view in color?
Looks like this:
ec70.png
ec70.png (1.32 KiB) Viewed 134 times
It's very simple to accomplish and described here: viewtopic.php?f=4&t=2876#p9287

Olaf

Post Reply