Welcome Guest, Not a member yet? Register   Sign In
Getting a users name from id
#1

[eluser]egunay[/eluser]
Hello,

I'm stuck on some point about database. I mean I know how to do it on old school php but as I'm still new to framework and oop I want to learn the proper way.

I'm trying to build a blog and ofcourse I want to show author information under the title. As in database I'm storing the author data as ID, when I load the page I see the user id not user's name.

What should I do to show the name instead of the id?
#2

[eluser]imn.codeartist[/eluser]
please post your code first so that we can identify your problem :-)
#3

[eluser]mattpointblank[/eluser]
Presumably you have a table with blog posts and a table of users. The blog posts table should have a field called 'user_id' which corresponds to the 'id' field in the users table. When you query the database to get the blog posts, you need to 'join' those two tables together, so you can get the user's name (or whatever else you store in the users table).
#4

[eluser]egunay[/eluser]
Hello,

@mattpointblank, yes it is exactly as you said but the point is I don't know how to join them =)

Can you tell me how to do that?

Anyway,

This is my controller:
Code:
class Lmp extends MY_Controller {
.... etc...
$entries = $this->db->query("SELECT * FROM posts");
$content['posts'] = $entries->result_array();
$this->parser->parse("/content", $content);
....

And my view is:
Code:
....
...
..
{posts}
<p>{title}</p>
<p>{uid}</p>
<hr />
{/posts}
....
...
..
#5

[eluser]cahva[/eluser]
Didnt you say you could do it "oldschool" Smile Its a simple mysql query, just like "oldschool" unless you want to use active record.

I dont know the fields but it should look something like this(and put your database related things in model):

Code:
class Posts_model extends Model {
    
    function __construct()
    {
        parent::__construct();
    }
    
    function get_all()
    {
        $this->db->select('*')
                ->from('posts')
                ->join('users','users.id = posts.uid');
                
        $query = $this->db->get();
        
        return $query->result_array();
    }
}

Theres select('*') but you should put the fields you are going to use there.

Then in the controller:
Code:
$this->load->model('posts_model','posts');
$content['posts'] = $this->posts->get_all();

Look in the user guide for more information about active record.
#6

[eluser]egunay[/eluser]
Yes I said that but I didn't know this thing, I was creating another query and select the user with the id... Wink

Anyway, thank you for the tip!
#7

[eluser]egunay[/eluser]
Hello!

I want to ask another small thing. I didn't want to open a new topic:

I'm saving dates as timestamps on database and ofcourse when I want to add date data to posts I'm seeing the timestamp. How can I convert it in my case?
#8

[eluser]mattpointblank[/eluser]
Look in the PHP manual for the date() function.

Basically, to output a date, you need something like this:

Code:
$date = date('format', strtotime($your_database_timestamp));

Where I wrote 'format', use some of the options from this page: http://uk3.php.net/manual/en/function.date.php (eg 'Y-m-d'). The strtotime() function for the second parameter turns your database timestamp (which might be like "2010-11-30 00:00:00") into a unix timestamp (eg something like '123424543') which date() can work with.
#9

[eluser]egunay[/eluser]
@mattpointblank:

Thank you for your answer but unfortunately it is not the way that I was looking. I mean;

As I'm getting the timestamp in array and parsing it to the view doing it this way doesn't work.

For example, this is my controller:
Code:
$entries = $this->db->query("SELECT * FROM posts INNER JOIN users ON posts.uid=users.uid");
$content['posts'] = $entries->result_array();
$this->parser->parse("/content", $content);

And this is my view;
Code:
{posts}
<div id="post_title">{title}</div>

<div>{username} @ {created}</div> // -> "created" = my timestamp
<hr />
{/posts}

I hope I could have explained what I'm trying to tell Smile
#10

[eluser]mattpointblank[/eluser]
[quote author="egunay" date="1272582915"]
Code:
{posts}
<div id="post_title">{title}</div>

<div>{username} @ {&lt;?php echo date('format', strtotime($your_database_timestamp)); ?&gt;}</div> // -> "created" = my timestamp
<hr />
{/posts}

[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB