· 7 years ago · Sep 18, 2018, 08:24 PM
1Understanding the passing of data/life of a script in web development/CodeIgniter
2<?php
3
4class Site extends CI_Controller {
5
6var $task1;
7var $tasks = array(
8 "task1" => NULL,
9 "date1" => 0,
10 "date2" => 0,
11 "diff" => 0);
12
13function __construct()
14{
15 parent::__construct();
16 include 'timetask.php';
17 $this->task1 = new TimeTask("create");
18}
19
20function index()
21{
22 $this->tasks['task1'] = $this->task1->getTask();
23 $this->tasks['diff'] = $this->task1->getTimeDiff();
24
25 if($this->tasks['diff'] == NULL)
26 {
27 $this->tasks['diff'] = 0;
28 }
29
30 $this->load->view('usability_test', $this->tasks);
31}
32
33function origIndex()
34{
35 $this->task1->setDate1(new DateTime());
36 $this->tasks['date1'] = $this->task1->getDate1()->getTimestamp();
37 $data = array();
38
39 if($q = $this->site_model->get_records())
40 {
41 $data['records'] = $q;
42 }
43
44 $this->load->view('options_view', $data);
45}
46
47function create()
48{
49 $this->task1->setDate2(new DateTime());
50 $this->tasks['date2'] = $this->task1->getDate2()->getTimestamp();
51
52 $data = array(
53 'author' => $this->input->post('author'),
54 'title' => $this->input->post('title'),
55 'contents' => $this->input->post('contents')
56 );
57
58 $this->site_model->add_record($data);
59 $this->index();
60
61}
62
63<?php
64
65class TimeTask
66{
67private $task;
68
69/**
70 * @var DateTime
71 */
72private $date1, $date2;
73
74function __construct($currTask)
75{
76 $this->task = $currTask;
77}
78
79public function getTimeDiff()
80{
81 $hasDiff = $this->date1 && $this->date2;
82 if ($hasDiff) {
83 return $this->date2->getTimestamp() - $this->date1->getTimestamp();
84 } else {
85 return NULL;
86 }
87}
88
89public function __toString()
90{
91 return (string) $this->getTimeDiff();
92}
93
94/**
95 * @return DateTime
96 */
97public function getDate1()
98{
99 return $this->date1;
100}
101
102/**
103 * @param DateTime $date1
104 */
105public function setDate1(DateTime $date1)
106{
107 $this->date1 = $date1;
108}
109
110/**
111 * @return DateTime
112 */
113public function getDate2()
114{
115 return $this->date2;
116}
117
118/**
119 * @param DateTime $date2
120 */
121public function setDate2(DateTime $date2)
122{
123 $this->date2 = $date2;
124}
125
126/**
127 * @return get current task
128 */
129 public function getTask()
130 {
131 return $this->task;
132 }
133
134}
135
136?>
137
138<?php echo form_open('site/create');?>
139...and...
140<?php echo anchor("site/delete/$row->id", $row->title); ?>
141
142CREATE TABLE IF NOT EXISTS `ci_sessions` (
143session_id varchar(40) DEFAULT '0' NOT NULL,
144ip_address varchar(45) DEFAULT '0' NOT NULL,
145user_agent varchar(120) NOT NULL,
146last_activity int(10) unsigned DEFAULT 0 NOT NULL,
147user_data text NOT NULL,
148PRIMARY KEY (session_id),
149KEY `last_activity_idx` (`last_activity`)
150
151$config['sess_use_database'] = TRUE;
152$config['sess_table_name'] = 'ci_sessions';