Guest User

Untitled

a guest
Dec 5th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. class User extends CI_Controller
  2. {
  3. function index()
  4. {
  5. $this->load->model('login_model');
  6. $data['user'] = $this->login_model->get_user();
  7. $data['tweets'] = $this->tweet_model->usertweets();
  8. $this->load->view('templates/header');
  9. $this->load->view('user', $data);
  10. $this->load->view('templates/footer');
  11. }
  12. }
  13.  
  14. class Login_model extends CI_Model
  15. {
  16. function can_login($username, $password)
  17. {
  18. $this->db->where('username', $username);
  19. $this->db->where('password', $password);
  20. $query = $this->db->get('users');
  21.  
  22. //SELECT * FROM users WHERE username = '$username' AND password = '$password'
  23.  
  24. if($query->num_rows() > 0)
  25. {
  26. return true;
  27. }
  28. else {
  29. return false;
  30. }
  31. }
  32.  
  33. function get_user()
  34. {
  35. $query = $this->db->get('users');
  36.  
  37. $username = $_SESSION['username'];
  38. $this->db->select('user_id', 'first_name', 'last_name', 'date_joined');
  39. $this->db->from('users');
  40. $this->db->where('username', $username);
  41. $query = $this->db->get();
  42. return $query->row();
  43. }
  44. }
  45.  
  46. class Tweet_model extends CI_Model
  47. {
  48. public function __construct()
  49. {
  50. $this->load->database();
  51. }
  52.  
  53. public function get_tweets($tweet_id = false)
  54. {
  55. if ($tweet_id === false) {
  56. $this->db->join('users', 'tweets.user_id = users.user_id');
  57. $query = $this->db->get('tweets');
  58. return $query->result_array();
  59. }
  60.  
  61. $query = $this->db->get_where('tweets', array('tweet_id' => $tweet_id));
  62. return $query->row_array();
  63. }
  64.  
  65. public function set_tweet()
  66. {
  67. $tweet = url_title($this->input->post('tweet'), 'dash', true);
  68.  
  69. $query = $this->db->get('users');
  70.  
  71. $username = $_SESSION['username'];
  72. $this->db->select('user_id');
  73. $this->db->from('users');
  74. $this->db->where('username', $username);
  75.  
  76. if ($this->db->affected_rows())
  77. {
  78. $user = $query->row();
  79. var_dump($user_id);
  80. }
  81.  
  82. $data = array(
  83. 'tweet' => $this->input->post('tweet'),
  84. 'user_id' => $user->user_id
  85. );
  86.  
  87. return $this->db->insert('tweets', $data);
  88. }
  89.  
  90. function Usertweets() {
  91. $this->db->flush_cache();
  92.  
  93. $username = $_SESSION['username'];
  94. $this->db->select('user_id');
  95. $this->db->from('users');
  96. $this->db->where('username', $username);
  97.  
  98. $query = $this->db->get();
  99. $user = $query->row();
  100.  
  101. $this->db->flush_cache();
  102.  
  103. $this->db->select('*');
  104. $this->db->from('tweets');
  105. $this->db->where('user_id', $user->user_id);
  106.  
  107. return $this->db->get()->result();
  108.  
  109. }
  110. }
  111.  
  112. <div class="container">
  113. <div class="row">
  114. <div class="col-md-4">
  115. <h4><?php echo $_SESSION['username']; ?></h4>
  116. </div>
  117. <div class="col-md-8">
  118. <?php foreach ($tweets as $tweet): ?>
  119. <div class="user-tweet">
  120. <h4><?php echo $_SESSION['username']; ?></h4>
  121. <h6><?php echo $tweet->tweet_date; ?></h6>
  122. <p><?php echo $tweet->tweet; ?></p>
  123. </div>
  124. <?php endforeach; ?>
  125. </div>
  126. </div>
  127. </div>
Add Comment
Please, Sign In to add comment