ashura91 ashura91 - 1 year ago 82
PHP Question

How to insert same `datetime` variable into different tables?

I'd like to store a

datetime
variable into different tables by using two functions. I use
constraint
in
CI
but still have no luck.

This is the constraint:

$date_now = date("ymdhis");
define('TODAY_DATE',$date_now);


These are the functions:

public function save_activity_m(){
foreach($details as $rows){

$stock_in = $rows['product']."_".TODAY_DATE;
$data['STOCK_IN'] = ($rows['product'] == "") ? NULL : $stock_in;

$this->MProduct->ins_product_m($data);
}
echo "<script type='text/javascript'>alert('New stock arrived');window.top.location.reload();</script>";
}

public function save_notifikasi(){

$lampiran = $this->input->post('lamp');

$data['note_date'] = $lampiran."_".TODAY_DATE;
$data['note'] = $this->input->post('isi');

$this->MProduct->ins_notif($data);
echo "<script type='text/javascript'>alert('You have a notification');</script>";


}


How to make the datetime is the same for
$data['STOCK_IN']
and
$data['note_date']
?

Answer Source

Since the web is stateless, no data in a PHP variable will be held from one page (or load) to another; essentially you're booting the application from scratch each time.

The only way around this is to use some sort of semi-persistent storage such as a cookie or session variable (or persistent storage like the database) - setting a constant, e.g. define('TODAY_DATE',$date_now); will only make that data constant for the current execution of the script(s).

This is a basic example using session storage ($_SESSION):

<?php

// crank up the session
// you may well have one running already, 
// in which case ignore this
session_start();

// store the execution time for this script
// as a session variable if it's NOT already set
// i.e. don't overwrite it
if(empty($_SESSION['time_now'])) $_SESSION['time_now'] = date("ymdhis");


public function save_activity_m() {
    foreach($details as $rows) {          
        $stock_in = $rows['product'] . "_" . $_SESSION['time_now'];
        $data['STOCK_IN'] = ($rows['product'] == "") ? NULL : $stock_in;
        $this->MProduct->ins_product_m($data);
    }

    echo "<script type='text/javascript'>alert('New stock arrived');window.top.location.reload();</script>";
}

/**
 * Assuming this is the last thing you want to
 * do with 'time_now' you should unset it here
 */
public function save_notifikasi() {
    $lampiran = $this->input->post('lamp');     
    $data['note_date'] = $lampiran . "_" . $_SESSION['time_now'];
    $data['note'] = $this->input->post('isi');
    $this->MProduct->ins_notif($data);

    // since we're done with the 'time_now' session
    // variable we need to unset it...
    unset($_SESSION['time_now']);

    echo "<script type='text/javascript'>alert('You have a notification');</script>";
}

// just to be on the safe side unset the 'time_now' session var 
// if it's older than 1 minute - otherwise future calls to this 
// script, by the same user, during the same session will use 
// the stored value from $_SESSION['time_now']
if(isset($_SESSION['time_now'])) {
    $sessionTime = DateTime::createFromFormat('ymdhis', $_SESSION['time_now']);
    $oneMinuteAgoTime = new DateTime('-1 minute');

    if($sessionTime < $oneMinuteAgoTime) {
        unset($_SESSION['time_now']);
    }
}

The caveat is that because you've stored the time in a session variable, unless you update or unset it, it will always be there (for the current session) - so if the user runs the script again it'll just use the stored time from the session.

I've put in a couple of unset() calls to try and work around this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download