How to fetch user avatar by user ID?

Discussion forum for Extension Writers regarding Extension Development.
User avatar
Kami-sama
Registered User
Posts: 104
Joined: Sat May 26, 2018 3:07 pm

Re: How to fetch user avatar by user ID?

Post by Kami-sama » Mon Oct 15, 2018 10:37 am

GanstaZ wrote:
Sun Oct 14, 2018 10:30 pm
Something like this.. or use the method provided by 3Di)

Code: Select all

    
    $user_loader = new \phpbb\user_loader($this->db, $this->phpbb_root_path, $this->phpEx, USERS_TABLE);
    
    $sql = 'SELECT forum_id, topic_id, topic_title, topic_time, topic_views, topic_poster, topic_posts_approved, topic_first_poster_name, topic_first_poster_colour, topic_last_post_id, topic_last_poster_name, topic_last_poster_colour, topic_last_post_time, topic_last_view_time, topic_last_poster_id
		FROM ' . TOPICS_TABLE . '
        WHERE ' . $this->db->sql_in_set('forum_id', $flast) . '
        AND ' . $this->content_visibility->get_visibility_sql('topic', 'topic') . '
		ORDER BY topic_last_post_time DESC';
	$result = $this->db->sql_query_limit($sql, $this->config['last_total']);

	$array = [];
	$user_loader->load_users($array);
	while ($row = $this->db->sql_fetchrow($result))
	{
	        $array[] = $row['topic_last_poster_id'];

	$this->template->assign_block_vars('last_topic', array(
            'LAST_LINK'      => append_sid("{$this->phpbb_root_path}viewtopic.$this->phpEx", 'f=' . $row['forum_id'] . '&t=' . $row['topic_id']),
		    'U_LAST_TOPIC'   => append_sid("{$this->phpbb_root_path}viewtopic.$this->phpEx", 'f=' . $row['forum_id'] . '&p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id']),
            'LAST_POSTER'     => append_sid("{$this->phpbb_root_path}memberlist.$this->phpEx", 'mode=viewprofile' . '&u=' . $row['topic_poster']),
		    'USERNAME_LAST'	 => append_sid("{$this->phpbb_root_path}memberlist.$this->phpEx", 'mode=viewprofile' . '&u=' . $row['topic_last_poster_id']),
			'TOPIC_TITLE'					=> $row['topic_title'],
			'TOPIC_VIEWS'					=> $row['topic_views'],
    	    'TOPIC_REPLIES'	                => $row['topic_posts_approved'],
			'TOPIC_LAST_POSTER_NAME'		=> $row['topic_last_poster_name'],
			'TOPIC_LAST_POSTER_COLOUR'		=> $row['topic_last_poster_colour'],
			'TOPIC_LAST_POST_TIME'			=> $this->user->format_date($row['topic_last_post_time']),
			'TOPIC_LAST_VIEW_TIME'			=> $this->user->format_date($row['topic_last_view_time']),
    		'USERNAME_AV'					=> $user_loader->get_avatar($row['topic_last_poster_id']),
		));
	}
	$this->db->sql_freeresult($result);
    unset($user_loader);
	

This actually did work! Thank you!

User avatar
Kami-sama
Registered User
Posts: 104
Joined: Sat May 26, 2018 3:07 pm

Re: How to fetch user avatar by user ID?

Post by Kami-sama » Mon Oct 15, 2018 10:37 am

3Di wrote:
Sun Oct 14, 2018 11:21 pm
Without injection of user_loader, simply:

Code: Select all

//Last active topics
	$sql = 'SELECT t.forum_id, t.topic_id, t.topic_title, t.topic_time, t.topic_views, t.topic_poster, t.topic_posts_approved, t.topic_first_poster_name, t.topic_first_poster_colour, t.topic_last_post_id, t.topic_last_poster_name, t.topic_last_poster_colour, t.topic_last_post_time, t.topic_last_view_time, t.topic_last_poster_id, u.user_id, u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height
			FROM ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . ' u 
			WHERE u.user_id = t.topic_last_poster_id
				AND ' . $this->db->sql_in_set('forum_id', $flast) . '
				AND ' . $this->content_visibility->get_visibility_sql('topic', 'topic') . '
			ORDER BY t.topic_last_post_time DESC';
	$result = $this->db->sql_query_limit($sql, $this->config['last_total']);
	
	while ($row = $this->db->sql_fetchrow($result))
	{
		/* Map arguments for  phpbb_get_avatar() */
		$row_avatar = array(
			'avatar'		 => $row['user_avatar'],
			'avatar_type'	 => $row['user_avatar_type'],
			'avatar_height'	 => 20,
			'avatar_width'	 => 20,
		);

		$topic_last_poster_av = (!empty($row['user_avatar'])) ? phpbb_get_avatar($row_avatar, '') : '';

		$this->template->assign_block_vars('last_topic', array(
			'LAST_LINK'						=> append_sid("{$this->phpbb_root_path}viewtopic.$this->phpEx", 'f=' . $row['forum_id'] . '&t=' . $row['topic_id']),
			'U_LAST_TOPIC'					=> append_sid("{$this->phpbb_root_path}viewtopic.$this->phpEx", 'f=' . $row['forum_id'] . '&p=' . $row['topic_last_post_id'] . '#p' . $row['topic_last_post_id']),
			'LAST_POSTER'					=> append_sid("{$this->phpbb_root_path}memberlist.$this->phpEx", 'mode=viewprofile' . '&u=' . $row['topic_poster']),
			'USERNAME_LAST'					=> append_sid("{$this->phpbb_root_path}memberlist.$this->phpEx", 'mode=viewprofile' . '&u=' . $row['topic_last_poster_id']),
			'TOPIC_TITLE'					=> $row['topic_title'],
			'TOPIC_VIEWS'					=> $row['topic_views'],
			'TOPIC_REPLIES'					=> $row['topic_posts_approved'],
			'TOPIC_LAST_POSTER_NAME'		=> $row['topic_last_poster_name'],
			'TOPIC_LAST_POSTER_COLOUR'		=> $row['topic_last_poster_colour'],
			'TOPIC_LAST_POST_TIME'			=> $this->user->format_date($row['topic_last_post_time']),
			'TOPIC_LAST_VIEW_TIME'			=> $this->user->format_date($row['topic_last_view_time']),

			'TOPIC_LAST_POSTER_AV'			=> $topic_last_poster_av,
		));
	}
	$this->db->sql_freeresult($result);
in the template find:

Code: Select all

<img src="{BOARD_URL}ext/galandas/lasttopics/styles/all/theme/images/icon_profile.png" height="20" width="20" title="{last_topic.TOPIC_LAST_POSTER_NAME}" alt="" />
replace with

Code: Select all

{% if last_topic.TOPIC_LAST_POSTER_AV is not empty %}{{ last_topic.TOPIC_LAST_POSTER_AV }}{% else %}<img src="{BOARD_URL}ext/galandas/lasttopics/styles/all/theme/images/icon_profile.png" height="20" width="20" title="{last_topic.TOPIC_LAST_POSTER_NAME}" alt="" />{% endif %} 

Thank you for suggestion! Is there a big difference using user_loader?
Which method would be considered better?

User avatar
3Di
Registered User
Posts: 12997
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: How to fetch user avatar by user ID?

Post by 3Di » Mon Oct 15, 2018 12:15 pm

You should be able to read the class user_loader and compare the snippets, you will realize it yourself.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate

:game_die: The new Dice Roller extension for phpBB 3.2 is out! :game_die:

Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
Kami-sama
Registered User
Posts: 104
Joined: Sat May 26, 2018 3:07 pm

Re: How to fetch user avatar by user ID?

Post by Kami-sama » Mon Oct 15, 2018 12:24 pm

Hah, I see the difference, I mean logically witch method is better? Or it does not matter?

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 280 guests