Welcome Guest, Not a member yet? Register   Sign In
Id Parse to Username
#1

[eluser]nagata[/eluser]
I am making the blog as a study of php, I have met a problem, I have made an authentication system (All except login/session), And Initially I coded my app so that each time news are added, they set 3 values: Title, Text and Author,
Author is set as Id of user who posts, Its just I plan to have name change feature in future,
And I met a problem exactly here, I need to display user name, and not user id,
Sadly this 2 things are in separate models and controllers, I need an easy fast way to convert user id's on a page to a user name, counting that author name will be displayed on news list page too, meaning going though all news db has (Which will be alot...)
I know, this might be hard to get, But I really need this, I have been trying to make it work like for 2 hours now... no result...
#2

[eluser]weboap[/eluser]
do some search on mysql join its active record eq is $this->db->join();
http://ellislab.com/codeigniter/user-gui...ecord.html
you will be joining the tables of user and posts.
#3

[eluser]Andreas Bergström[/eluser]
Why can't you do a join on the (presumptive) author-table when you fetch the news item(s)?
#4

[eluser]nagata[/eluser]
[quote author="weboap" date="1338991734"]do some search on mysql join its active record eq is $this->db->join();
http://ellislab.com/codeigniter/user-gui...ecord.html
you will be joining the tables of user and posts.[/quote]
could you show an example?
if there is a table called 'users' with fields: id, username, password, rank, regdate
and table 'news' with fields: id, text, title, author, date?

I would appriciate it... its just I learn better on visual sample
#5

[eluser]boltsabre[/eluser]
your table "news" should store the authors "user_id", not their name (not sure if you're doing this, you didn't specify in your last post what the actual news table columns are storing, and I'm a little confused by your first post - I'm not sure if you want to display the current users name on the screen AND the author of the new item, which could be 2 different people).

Either way it's just a simple matter now of doing a "join". When you get a news item, you get the "user_id" (author) from that table, and use it to get the "user_name" from the user table.

Quote:its just I learn better on visual sample
If you don't want to look at the link already supplied and search for "join" try searching youtube for "mysql join", it's a very visual medium.
#6

[eluser]nagata[/eluser]
[quote author="boltsabre" date="1338995468"]your table "news" should store the authors "user_id", not their name (not sure if you're doing this, you didn't specify in your last post what the actual news table columns are storing, and I'm a little confused by your first post - I'm not sure if you want to display the current users name on the screen AND the author of the new item, which could be 2 different people).

Either way it's just a simple matter now of doing a "join". When you get a news item, you get the "user_id" (author) from that table, and use it to get the "user_name" from the user table.

Quote:its just I learn better on visual sample
If you don't want to look at the link already supplied and search for "join" try searching youtube for "mysql join", it's a very visual medium.[/quote]
Thank you, your reply helped going to try now Big Grin
I hope it works.
#7

[eluser]nagata[/eluser]
I am totaly confused...

Due to Anti-Spam Policy of EllisLab I was Unable to post my code, So I added it to pastebin. Link Below
First code is Model: function I use to retrive news from DB as an array
http://pastebin.com/jGQZYyEp

My problem is that I cant figure out how to make it all work, Cant figure out how to join them and how to use that query result, I tried something but it didnt work..




Theme © iAndrew 2016 - Forum software by © MyBB