![]() |
What is the best way for data in user section - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: What is the best way for data in user section (/showthread.php?tid=39266) Pages:
1
2
|
What is the best way for data in user section - El Forum - 03-06-2011 [eluser]Michal1[/eluser] Hello guys, I am thinking about what is the best way for showing data in user section. Lets say I have a login form and when an user logins he is redirect to membership area. And then I want to display only data which are assigned to certain user. So for example I have a table data and in this table I have fields like "id" "article name", "article_content" etc. And every logged in user has chance to add an article into this table. And when he is logged in I want to display him only articles which he added by myself. ( I hope this make sense). So what would I do is that I would create a new field in my data table called for example "user_id" and then when user ads an article to data table I would insert his id into user_id in that table. So for example I will have user called "Michal" with id "10" So when he adds and article I would insert that value 10 into user_id in data table. And then when displaying articles in membership area I would probaby need to a lot of if statemens, something like: if user_id in table data is equal to id in users table then display data. But this sounds kinda complicated and after a while it will produces a huge code. So is there any better and simple way how to handle this? Thank you very much What is the best way for data in user section - El Forum - 03-06-2011 [eluser]steelaz[/eluser] Everything sounds correct up to the last part, you only need to add <i>where</i> statement to your database query. Your code in the model would look something like this: Code: public function get_articles_by_user($user_id) And in controller, assuming you have logged in user's id in the session: Code: $articles = $this->articles_model->get_articles_by_user($this->session->userdata('user_id')); What is the best way for data in user section - El Forum - 03-06-2011 [eluser]Michal1[/eluser] Thank you for your help. I tried to do code that and still cannot get it work as I do not know how to do some things. Do you think you could help me? This is me "site controller" which controlls displaying articles. Inside of it I have: Code: class Site extends Controller { Then I have "site" model which has function get_data Code: function get_data() So this all takes and display every article from database. Then I have a controller "login" and inside of that is Code: class Login extends Controller { And then the last one is login_model Code: function validate_login() And now I want to do that when certain user is logged in I will store his uniquate field from its database called "id_users" and then I would just probably need to reedit my function in site_model from Code: function get_data() to something different, probably using where. So something like Code: $this->db->where('user_id, $id_users); So the main problem for is how to insert certain user "id" which is field in database to $id_users and then use it in site_model for this $this->db->where('user_id, $id_users); Thank you very much and I hope this makes sense What is the best way for data in user section - El Forum - 03-06-2011 [eluser]steelaz[/eluser] You can change your validate_login() function to: Code: function validate_login() and in login controller: Code: if ($query = $this->login_model->validate_login()) Then your site model get_data() function can be modified to filter by user when user is set: Code: function get_data($user_id = NULL) And in your site controller: Code: function index() What is the best way for data in user section - El Forum - 03-06-2011 [eluser]InsiteFX[/eluser] Also if your using Codeigniter 2.0.0 then you need CI_Controller and CI_Model not Controller and Model. Code: class Site extends CI_Controller { InsiteFX What is the best way for data in user section - El Forum - 03-07-2011 [eluser]Michal1[/eluser] Hey, thanks for the help. I reedit your code little bit as there were some mistakes and it almost works. The only problem is with: Code: function validate() in user_ids part where I am inserting $query->id (which as you said should insert a id value from database. But it does not insert it ![]() If I insert some value manually for example user_ids'=>'2' then the whole works perfectly then. So there must be some probably with $query->id What is the best way for data in user section - El Forum - 03-07-2011 [eluser]steelaz[/eluser] In your validate() function try Code: $query = $this->login_model->validate_login(); and see what you get. What is the best way for data in user section - El Forum - 03-07-2011 [eluser]Michal1[/eluser] I get: bool(true) What is the best way for data in user section - El Forum - 03-07-2011 [eluser]steelaz[/eluser] Ah, my bad, you need to update your validate_login() function: Code: function validate_login() What is the best way for data in user section - El Forum - 03-07-2011 [eluser]Michal1[/eluser] [quote author="steelaz" date="1299508596"]Ah, my bad, you need to update your validate_login() function: Code: function validate_login() Yeah it finally works. Thank you so much. I just need to add return true; after $query->row() because otherwise validation would not work correct |