Auto Update Database When ViewValue Change in List Page

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
JBourne
User
Posts: 16

Auto Update Database When ViewValue Change in List Page

Post by JBourne »

Hello. I'm new in PHPMaker so I will consult to the masters if how to update database when my ViewValue in List Page Change or update?
Where Event should I put my script?

Thanks a lot.


danielc
User
Posts: 1601

Post by danielc »

  1. Does your currentvalue change? Are ViewValue changed by server event or through what?
  2. What data you want to change in your database?

Please describe in detail what you want to do.


JBourne
User
Posts: 16

Post by JBourne »

I have this code and I put it in Row_Rendered server event and it works, it triggers when the current date change and the result displays on hdAge Field View Value.
But in this case I want also to update mysql database once the view value of the hdAge Field change.

// Row Rendered event
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);


$d1 = date ('m/d/Y H:i:s');
$d2 = $this->DateReported->CurrentValue;
$now = strtotime($d1);                   
$your_date = strtotime ($d2);
$status = $this->TicketStateID->CurrentValue;

if ($status == '3') {
     
    } else {     
           $datediff = $now - $your_date;
           $d3 = floor($datediff/3600);
    
                if ($d3 <= "0") { 
                       $this->hdAge->ViewValue = "0"; 
                      } else {
                       $this->hdAge->ViewValue = $d3;                     
                      }

}


Webmaster
User
Posts: 9430

Post by Webmaster »

  1. You'd better use a database view so you don't need to calculate yourself,
  2. If you prefer doing it yourself, you can execute an UPDATE statement after your code, e.g.

ew_Exceute("UPDATE YourTable SET YourField = xxx WHERE YourPrimaryKeyField = " . $this->YourPrimaryKeyField->CurrentValue); // Assume your primary key field is number here so no need to quote


JBourne
User
Posts: 16

Post by JBourne »

Thanks it helps my issue.
Last question in List page, is it also possible to send an email in "List" page when viewvalue change because in Email_Sending it has no "List" page it has "add", "edit" and "view" only. Where I can put my code to send email automatically when Value change without editing the row.

Thanks a lot.


danielc
User
Posts: 1601

Post by danielc »

You can send email when you use Email class. Add this line before you set your recipient and content:
$Email = new cEmail();

And add after your email content:
$Email->Send(); // to send

It depends where you put the code to change the ViewValue. The code can add after your update statement as you have checked the ViewValue change.


JBourne
User
Posts: 16

Post by JBourne »

Hi daniel thanks for this info. I already try this and it works but my problem here is it trigger to send email once my STATUS == "Expired" so it sends email every time my page load. Is there any way to set my email sending only once or schedule only?

Thanks.


danielc
User
Posts: 1601

Post by danielc »

You need to have another field to have this count. So, initially set this count to 0. Before sending email, check status field and count field. After sending email, update the count field.

If your server support cron job, set up one. Google "cron job".


Post Reply