Understanding the passing of data/life of a script in web development/CodeIgniter
- by Pete Jodo
I hope I worded the title accurately enough but I typically use Java and don't have much experience in Web Development/PHP/CodeIgniter. I have a difficult time understanding the life cycle of a script as I found out trying to implement a certain feature to a website I am developing (as a means of learning how to). I'll first describe the feature I tried implementing and then the problem I ran into that made me question my fundamental understanding of how scripts work since I'm used to typical OOP.
Ok so here goes...
I have a webpage that has 2 basic tasks a user can do, create and delete an entry. What I attempted to implement was a way to time a user how long it takes them to complete a certain task. The way I did this was have a homepage where there would be a list of tasks a user to choose from (in this case 2, create and delete). A user would click a task which would link to the 'true' homepage where the user then would be expected to complete the task. My script looks like this:
<?php
class Site extends CI_Controller {
var $task1;
var $tasks = array(
"task1" => NULL,
"date1" => 0,
"date2" => 0,
"diff" => 0);
function __construct()
{
parent::__construct();
include 'timetask.php';
$this->task1 = new TimeTask("create");
}
function index()
{
$this->tasks['task1'] = $this->task1->getTask();
$this->tasks['diff'] = $this->task1->getTimeDiff();
if($this->tasks['diff'] == NULL)
{
$this->tasks['diff'] = 0;
}
$this->load->view('usability_test', $this->tasks);
}
function origIndex()
{
$this->task1->setDate1(new DateTime());
$this->tasks['date1'] = $this->task1->getDate1()->getTimestamp();
$data = array();
if($q = $this->site_model->get_records())
{
$data['records'] = $q;
}
$this->load->view('options_view', $data);
}
function create()
{
$this->task1->setDate2(new DateTime());
$this->tasks['date2'] = $this->task1->getDate2()->getTimestamp();
$data = array(
'author' => $this->input->post('author'),
'title' => $this->input->post('title'),
'contents' => $this->input->post('contents')
);
$this->site_model->add_record($data);
$this->index();
}
I only included create to keep it short. Then I also have the TimeTask class, that actually another StackOverflow so kindly helped me with:
<?php
class TimeTask
{
private $task;
/**
* @var DateTime
*/
private $date1, $date2;
function __construct($currTask)
{
$this->task = $currTask;
}
public function getTimeDiff()
{
$hasDiff = $this->date1 && $this->date2;
if ($hasDiff) {
return $this->date2->getTimestamp() - $this->date1->getTimestamp();
} else {
return NULL;
}
}
public function __toString()
{
return (string) $this->getTimeDiff();
}
/**
* @return \DateTime
*/
public function getDate1()
{
return $this->date1;
}
/**
* @param \DateTime $date1
*/
public function setDate1(DateTime $date1)
{
$this->date1 = $date1;
}
/**
* @return \DateTime
*/
public function getDate2()
{
return $this->date2;
}
/**
* @param \DateTime $date2
*/
public function setDate2(DateTime $date2)
{
$this->date2 = $date2;
}
/**
* @return get current task
*/
public function getTask()
{
return $this->task;
}
}
?>
I don't think posting the views is necessary for the question but here is atleast how the links are made.
...and...
id", $row-title); ?
Now there's no error in the code but it doesn't do what I expect of it and the reason I assume why is because that each time a function of the script is called via a new page it is NOT the same instance of the script called previously so any previously created objects are no longer there. This confuses me and leaves me quite unsure of how to implement this gracefully. Some ways I would guess of how to do this is by passing the necessary data through the URL or have data saved in a database and retrieve it later to compare the times. What would be a recommended way to do, not just this, but anything that needs previously created data? Also, am I correct to think that a script is only 'alive' for one webpage at a time?
Thanks!