diff --git a/www/application/controllers/All_Tweets.php b/www/application/controllers/All_Tweets.php index 8816ca7..4e32d65 100644 --- a/www/application/controllers/All_Tweets.php +++ b/www/application/controllers/All_Tweets.php @@ -12,9 +12,19 @@ function __Construct(){ public function index() { $this->data['posts'] = $this->All_Tweets_Model->getPosts(); // calling Post model method getPosts() + $this->load->view('templates/header', NULL); $this->load->view('home', $this->data); // load the view file , we are passing $data array to view file $this->load->view('templates/footer', NULL); } + function search_keyword(){ + $keyword = $this->input->post('keyword'); + $data['results'] = $this->All_Tweets_Model->search($keyword); + + $this->load->view('templates/header'); + $this->load->view('home', $data); // load the view file , we are passing $data array to view file + $this->load->view('templates/footer'); + } + } diff --git a/www/application/controllers/Login.php b/www/application/controllers/Login.php index eaa8137..da638ce 100644 --- a/www/application/controllers/Login.php +++ b/www/application/controllers/Login.php @@ -1,9 +1,15 @@ load->database(); // load database + $this->load->model('User_Info_Model'); // load model + $this->output->enable_profiler(TRUE); + } + public function index(){ $this->load->helper(array('form', 'url')); - $this->load->library('form_validation'); $this->form_validation->set_rules('username', 'Username', 'required'); @@ -15,7 +21,17 @@ public function index(){ // load the homepage if login successful // https://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods else { - $this->load->view('formsuccess'); + //check if user in db + $user = $this->User_Info_Model->isRegistered(); + + // if yes get user id, launch session, send to home + if($user !== []){ + $data['user'] = $user; + redirect('/all_tweets'); + } + // if no, refresh login + else $this->load->view('login', $data); + } } } diff --git a/www/application/controllers/Register.php b/www/application/controllers/Register.php index a9f7707..f8ff82b 100644 --- a/www/application/controllers/Register.php +++ b/www/application/controllers/Register.php @@ -27,9 +27,11 @@ public function index(){ // load the homepage once properly submitted // https://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods + + // add user to db, launch session, send to home else { $this->User_Info_Model->addUser(); - $this->load->view('formsuccess'); + redirect('/all_tweets'); } } } diff --git a/www/application/controllers/User_Tweets.php b/www/application/controllers/User_Tweets.php index e5b6fb6..fc5b71c 100644 --- a/www/application/controllers/User_Tweets.php +++ b/www/application/controllers/User_Tweets.php @@ -18,4 +18,24 @@ public function index() { $this->load->view('templates/footer', NULL); } + public function create(){ + $this->load->helper(array('form', 'url')); + $this->load->library('form_validation'); + $this->form_validation->set_rules('tweet', 'Tweet', 'required'); + + if ($this->form_validation->run() === FALSE) + { + $this->data['posts'] = $this->User_Tweets_Model->getPosts(); // calling Post model method getPosts() + $this->load->view('templates/header', NULL); + $this->load->view('user', $this->data); // load the view file , we are passing $data array to view file + $this->load->view('templates/footer', NULL); + } + else + { + $this->User_Tweets_Model->addTweet(); + redirect("/user_tweets"); + } + + } + } diff --git a/www/application/models/All_Tweets_Model.php b/www/application/models/All_Tweets_Model.php index 1e0f488..0c70fb7 100644 --- a/www/application/models/All_Tweets_Model.php +++ b/www/application/models/All_Tweets_Model.php @@ -10,5 +10,15 @@ function getPosts(){ return $query->result_array(); } + function search($keyword){ + $this->db->select("first_name, last_name, username, tweet, date_time"); + $this->db->from('Users, Tweets'); + $this->db->where('Users.id = Tweets.user_id'); + $this->db->order_by('date_time', 'DESC'); + $this->db->like('tweet', $keyword); + $query = $this->db->get(); + return $query->result_array(); + } + } ?> diff --git a/www/application/models/Search_Model.php b/www/application/models/Search_Model.php new file mode 100644 index 0000000..7d46821 --- /dev/null +++ b/www/application/models/Search_Model.php @@ -0,0 +1,16 @@ +load->database(); // load database + } + + function search($keyword) + { + $this->db->like('tweet', $keyword); + $query = $this->db->get('Tweets'); + return $query->result(); + } +} diff --git a/www/application/models/User_Info_Model.php b/www/application/models/User_Info_Model.php index fd6b042..e073a8b 100644 --- a/www/application/models/User_Info_Model.php +++ b/www/application/models/User_Info_Model.php @@ -1,10 +1,19 @@ input->post('username'); + $password = $this->input->post('password'); + $this->db->select('*'); + $this->db->from('Users'); + $this->db->where('username', $username); + $this->db->where('password', $password); + $query = $this->db->get(); + $result = $query->result_array(); + return $result; } - // new user + // register function addUser(){ $this->load->helper('url'); diff --git a/www/application/models/User_Tweets_Model.php b/www/application/models/User_Tweets_Model.php index 50904b7..7e066d2 100644 --- a/www/application/models/User_Tweets_Model.php +++ b/www/application/models/User_Tweets_Model.php @@ -11,27 +11,13 @@ function getPosts(){ return $query->result_array(); } - function addTweet(){ - $this->load->helper('url'); - - $slug = url_title($this->input->post('title'), 'dash', TRUE); - - $data = array( - 'tweet' => $this->input->post('tweet'), - 'date_time' => 10, - 'user_id' => 1 - ); - - return $this->db->insert('Tweets', $data); - - /* - $this->load->helper('url'); - $data = array( - 'tweet' => $this->input->post('tweet') - ); - - return $this->db->insert('tweets', $data); - */ - } - + function addTweet(){ + $this->load->helper('url'); + $data = array( + 'tweet' => $this->input->post('tweet'), + 'user_id' => 2 // UPDATE THIS WITH SESSION DATA + ); + + return $this->db->insert('Tweets', $data); + } } diff --git a/www/application/views/home.php b/www/application/views/home.php index 7153591..e2d71ab 100644 --- a/www/application/views/home.php +++ b/www/application/views/home.php @@ -1,10 +1,44 @@ +
diff --git a/www/application/views/templates/header.php b/www/application/views/templates/header.php index de05d9c..9b498e7 100644 --- a/www/application/views/templates/header.php +++ b/www/application/views/templates/header.php @@ -1,10 +1,10 @@ -
-
- - - - -