Getting Username from DB

Discussion forum for MOD Writers regarding MOD Development.
TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Getting Username from DB

Post by TheTechGame » Thu Apr 16, 2015 7:36 pm

Here is a section of a php file (from a mod) - functions_profile_comments.php:

Code: Select all

function error($text, $linktext, $type = E_USER_NOTICE)
	{
		global $user_id, $user, $comment_start, $phpbb_root_path, $phpEx;
		meta_refresh(3, append_sid('memberlist.php?mode=viewprofile&u='.$user_id.''));
		$result = trigger_error(''.$text.'<br /><br /><a href="'.append_sid($phpbb_root_path.'memberlist.'.$phpEx, 'mode=viewprofile&u='.$user_id.'').'">'.$user->lang[$linktext].'</a>', $type);
		return $result;
	}
I need to get the username, so I can make the URL appear like this (because my profile URLs are site.com/(username):

Code: Select all

function error($text, $linktext, $type = E_USER_NOTICE)
	{
		global $user_id, $user, $comment_start, $phpbb_root_path, $phpEx;
		meta_refresh(3, append_sid('memberlist.php?mode=viewprofile&u='.$user_id.''));
		$result = trigger_error(''.$text.'<br /><br /><a href="'.append_sid.append_sid($user_id).'">'.$user->lang[$linktext].'</a>', $type);
		return $result;
	}
Last edited by TheTechGame on Thu Apr 16, 2015 8:16 pm, edited 1 time in total.

User avatar
david63
Registered User
Posts: 16330
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Getting Username from DB

Post by david63 » Thu Apr 16, 2015 7:53 pm

TheTechGame wrote:I need to get the username
You have the user id so what is the problem with an sql query to lookup the username?
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Re: Getting Username from DB

Post by TheTechGame » Thu Apr 16, 2015 8:02 pm

david63 wrote:
TheTechGame wrote:I need to get the username
You have the user id so what is the problem with an sql query to lookup the username?
I don't know how to do that. Could you add it to that function for me :D?

TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Re: Getting Username from DB

Post by TheTechGame » Thu Apr 16, 2015 8:10 pm

Brf wrote:If $user is global, it should be in $user->data['username']
I'm a newb, so I haven't an idea how to use that in the php function:

Code: Select all

global $user_id, $user, $comment_start, $phpbb_root_path, $phpEx, $user->data['username'];
?
Or how I would use it in the code.. like this?

Code: Select all

meta_refresh(3, append_sid('$username''));
?

Thanks for your help
Last edited by TheTechGame on Thu Apr 16, 2015 8:13 pm, edited 1 time in total.

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51785
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Getting Username from DB

Post by Brf » Thu Apr 16, 2015 8:12 pm

Ug. Actually, $user_id is the user you are viewing right? and $user is the viewer, not the viewee, so that would not work anyway.

TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Re: Getting Username from DB

Post by TheTechGame » Thu Apr 16, 2015 8:14 pm

Brf wrote:Ug. Actually, $user_id is the user you are viewing right? and $user is the viewer, not the viewee, so that would not work anyway.
Yes, the user ID is the profile that is being viewed

TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Re: Getting Username from DB

Post by TheTechGame » Thu Apr 16, 2015 8:16 pm

Here is the entire file, if that is of any help:

Code: Select all

<?php
if (!defined('IN_PHPBB'))
{
	exit;
}
class comments
{
	/**
	* Customised version of generate_smilies($mode, $forum_id) function
	*/
	function generate_profile_smilies()
	{
		global $db, $config, $template;
		global $phpbb_root_path;

		$start = request_var('start', 0);
		
		$display_link = false;
		
		$sql = 'SELECT smiley_id
				FROM ' . SMILIES_TABLE . '
				WHERE display_on_posting = 0';
		$result = $db->sql_query_limit($sql, 1, 0, 3600);
		$db->sql_freeresult($result);
			
		$sql = 'SELECT *
			FROM ' . SMILIES_TABLE . '
			WHERE display_on_posting = 1
			ORDER BY smiley_order';
		$result = $db->sql_query($sql, 3600);

		$smilies = array();
		while ($row = $db->sql_fetchrow($result))
		{
			if (empty($smilies[$row['smiley_url']]))
			{
				$smilies[$row['smiley_url']] = $row;
			}
		}
		$db->sql_freeresult($result);

		if (sizeof($smilies))
		{
			$root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;

			foreach ($smilies as $row)
			{
				$template->assign_block_vars('smiley', array(
					'SMILEY_CODE'	=> $row['code'],
					'A_SMILEY_CODE'	=> addslashes($row['code']),
					'SMILEY_IMG'	=> $root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
					'SMILEY_WIDTH'	=> $row['smiley_width'],
					'SMILEY_HEIGHT'	=> $row['smiley_height'],
					'SMILEY_DESC'	=> $row['emotion'])
				);
			}
		}
	}
	
	/**
	* Customised version of generate_text_for_storage
	*/
	function store(&$text, &$uid, &$bitfield, &$flags, $allow_bbcode, $allow_urls, $allow_smilies, $quote_status, $img_status, $url_status, $flash_status)
	{
		global $phpbb_root_path, $phpEx;

		$uid = $bitfield = '';
		$flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);

		if (!$text)
		{
			return;
		}

		if (!class_exists('parse_message'))
		{
			include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
		}

		$message_parser = new parse_message($text);
		$message_parser->parse($allow_bbcode, $allow_urls, $allow_smilies, $img_status, $flash_status, $quote_status, $url_status);

		$text = $message_parser->message;
		$uid = $message_parser->bbcode_uid;

		// If the bbcode_bitfield is empty, there is no need for the uid to be stored.
		if (!$message_parser->bbcode_bitfield)
		{
			$uid = '';
		}

		$bitfield = $message_parser->bbcode_bitfield;

		return;
	}
	
	/**
	* Simplify error triggering
	*@param string $text text of error
	*@param string $linktext text of error link
	*@param strimg $type type of error
	*/
	function error($text, $linktext, $type = E_USER_NOTICE)
	{
		global $user_id, $user, $comment_start, $phpbb_root_path, $phpEx;
		meta_refresh(3, append_sid('memberlist.php?mode=viewprofile&u='.$user_id.''));
		$result = trigger_error(''.$text.'<br /><br /><a href="'.append_sid($phpbb_root_path.'memberlist.'.$phpEx, 'mode=viewprofile&u='.$user_id.'').'">'.$user->lang[$linktext].'</a>', $type);
		return $result;
	}
	
	/**
	* Show comments
	*@param int $user_id id of user
	*/
	function show($user_id)
	{
		global $db, $config, $user, $phpbb_root_path, $auth, $comment, $comment_id, $phpEx, $template;
		
		$start   = request_var('start', 0);
		$limit  = request_var('limit', (int) $config['profile_comments_limit']);

		$pagination_url = append_sid("memberlist.php?mode=viewprofile&u=" . $user_id . "#wall");
		
		$sql_array = array(
			'SELECT'    => 'c.*, u.user_id, u.username, u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height, u.user_colour, u.user_rank',
			'FROM'      => array(
				PROFILE_COMMENTS_TABLE => 'c',
				USERS_TABLE 		   => 'u'
			),
			'WHERE'     =>  'c.poster_id = u.user_id
						AND c.profile_id = ' . $user_id,

			'ORDER_BY'  => 'c.comment_time '. $config['profile_comments_order'],
		);
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$result = $db->sql_query_limit($sql, $limit, $start);

		while($row = $db->sql_fetchrow($result))
		{
		
			// Generate text for display
			$row['bbcode_options'] = (($row['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) +
				(($row['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) + 
				(($row['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);
			$text = generate_text_for_display($row['text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);
		
			// User rank
			$sql = 'SELECT rank_title
				FROM ' . RANKS_TABLE . '
				WHERE rank_id = ' . $row['user_rank'] . '';
			$sqlresult = $db->sql_query($sql);
			$rank = $db->sql_fetchrow($sqlresult);
			$db->sql_freeresult($sqlresult);
			
			// Comment edit and delete permissions
			$comdelete_allowed = ($user->data['is_registered'] && ($auth->acl_get('m_delete_profile_comments') || (
				$user->data['user_id'] == $row['poster_id'] &&
				$auth->acl_get('u_delete_own_profile_comments')
			)));
			$comedit_allowed = ($user->data['is_registered'] && ($auth->acl_get('m_edit_profile_comments') || (
				$user->data['user_id'] == $row['poster_id'] &&
				$auth->acl_get('u_edit_own_profile_comments')
			)));
			
				if ($comment == 'edit' && $comment_id && !$comedit_allowed)
				{
					$this->error($user->lang('NOT_ALLOWED_EDIT_COMMENT'),'BACK_PROFILE');
				}
				
				if ($comment == 'delete' && $comment_id && !$comdelete_allowed)
				{
					$this->error($user->lang('NOT_ALLOWED_DELETE_COMMENT'),'BACK_PROFILE');
				}
			
				$template->assign_block_vars('comment', array(
					'TEXT'		    => $text,
					'COMMENT_DATE'			=> $user->format_date($row['comment_time'], false),
					'AVATAR'				=> get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'] / 2, $row['user_avatar_height'] / 2),
					'USERNAME'				=> $row['username'],
					'COLOUR'				=> $row['user_colour'],
					'COMMENT_ID'			=> $row['comment_id'],
					'RANK_TITLE'			=> $rank['rank_title'],
					'S_COMEDIT_ALLOWED'		=> $comedit_allowed,
					'S_COMDELETE_ALLOWED'	=> $comdelete_allowed,
					'U_COMMENT_EDIT'		=> append_sid('memberlist.php?mode=viewprofile&comment=edit&cid='.$row['comment_id'].'' . (($start) ? "&start=$start" : '') .'&u=' . $user_id . '#message-box'),
					'U_COMMENT_DELETE'		=> append_sid('memberlist.php?mode=viewprofile&comment=delete&cid='.$row['comment_id'].'&u=' . $user_id),
					'U_PROFILE'				=>  append_sid("memberlist.php?mode=viewprofile&u=" .$row['poster_id']. ""),
				));
		}
		$db->sql_freeresult($result);
	

		$sql_array['SELECT'] = 'COUNT(c.comment_id) as total_comments';
		$sql = $db->sql_build_query('SELECT', $sql_array);
		$countresult = $db->sql_query($sql);

		$total_comments = $db->sql_fetchfield('total_comments');

		$db->sql_freeresult($countresult);
		
		$template->assign_vars(array(
			'PAGINATION'        => generate_pagination($pagination_url, $total_comments, $config['profile_comments_limit'], $start),
			'PAGE_NUMBER'       => on_page($total_comments, $limit, $start),
			'TOTAL_COMMENTS'    => ($total_comments == 1) ? $user->lang['COMMENT_COUNT'] : sprintf($user->lang['COMMENTS_COUNT'], $total_comments),
			'TOTAL_COMMENTS_NUMERIC' => $total_comments,
		));
		
		return true;

	}
	
	
	/**
	* Submit comment
	*@param array $input inputs for storing comment
	*/
	function submit($input = array())
	{
		global $db;
		foreach($input as $key => $value)
		{
			$input[$key] = $db->sql_escape($value);
			if(is_string($value))
			{
				$input[$key] = utf8_normalize_nfc($value);
			}
		}
		$sql = 'INSERT INTO ' . PROFILE_COMMENTS_TABLE . ' ' . $db->sql_build_array('INSERT', $input);
		$result = $db->sql_query($sql);

		return $result;
	}
	
	/**
	* Edit comment
	*@param array $input inputs for updating comment
	*@param int $comment_id id of comment
	*/
	function edit($input = array(), $comment_id)
	{
		global $db;
		foreach($input as $key => $value)
		{
			$input[$key] = $db->sql_escape($value);
			if(is_string($value))
			{
				$input[$key] = utf8_normalize_nfc($value);
			}
		}

		$sql = 'UPDATE ' . PROFILE_COMMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $input) . ' WHERE comment_id = ' . $comment_id . '';
		$result = $db->sql_query($sql);

		return $result;
	}
	
	/**
	* Delete comment
	*@param int $comment_id id of comment
	*/
	function delete($comment_id)
	{
	global $db;
		$sql = 'DELETE FROM ' . PROFILE_COMMENTS_TABLE . ' 
				WHERE comment_id = ' . $comment_id . '';
		$result = $db->sql_query($sql);
		
		return $result;
	}
}

?>

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51785
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Getting Username from DB

Post by Brf » Thu Apr 16, 2015 8:18 pm

I am not sure what you are trying to ask?
TheTechGame wrote: I'm a newb, so I haven't an idea how to use that in the php function:

Code: Select all

global $user_id, $user, $comment_start, $phpbb_root_path, $phpEx, $user->data['username'];
?
That is not a function. It is a declaration of global variables.
$user is already globalized there. You would not globalize its methods.
TheTechGame wrote: Or how I would use it in the code.. like this?

Code: Select all

meta_refresh(3, append_sid('$username''));
You wouldn't use it like that. That statement is saying to show the new webpage for 3 seconds, and then redirect to http://TheTechGame, which of course does not go anywhere.

TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Re: Getting Username from DB

Post by TheTechGame » Thu Apr 16, 2015 8:41 pm

Brf wrote:
TheTechGame wrote: Or how I would use it in the code.. like this?

Code: Select all

meta_refresh(3, append_sid('$username''));
You wouldn't use it like that. That statement is saying to show the new webpage for 3 seconds, and then redirect to http://TheTechGame, which of course does not go anywhere.
Okay, then let me rephrase.

Code: Select all

meta_refresh(3, append_sid('http://mysite.com/' . '$username'));
That would (if I could get the username variable) reload to the user's profile - mysite.com/(username) - following 3 seconds, after showing the comment posted successfully page. All I need to know is how to get the username from the user ID. The user ID is that of the profile being viewed, and I need to translate that to the username of the profile being viewed.

This is what happens after the success page following the posting of a comment:
MYSITE.COM/memberlist.php?mode=viewprofile&u=2

I want to use this instead:
MYSITE.COM/{username}

User avatar
david63
Registered User
Posts: 16330
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Getting Username from DB

Post by david63 » Thu Apr 16, 2015 8:44 pm

TheTechGame wrote:The user ID is that of the profile being viewed, and I need to translate that to the username of the profile being viewed.
david63 wrote:
TheTechGame wrote:I need to get the username
You have the user id so what is the problem with an sql query to lookup the username?
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Re: Getting Username from DB

Post by TheTechGame » Thu Apr 16, 2015 8:44 pm

david63 wrote:
TheTechGame wrote:The user ID is that of the profile being viewed, and I need to translate that to the username of the profile being viewed.
david63 wrote:
TheTechGame wrote:I need to get the username
You have the user id so what is the problem with an sql query to lookup the username?
TheTechGame wrote:I don't know how to do that.

TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Re: Getting Username from DB

Post by TheTechGame » Sat Apr 18, 2015 6:23 am

Hello? Is this simple to do?

FoulFoot
Registered User
Posts: 261
Joined: Mon Jun 23, 2003 1:04 am
Location: Honolulu, HI
Contact:

Re: Getting Username from DB

Post by FoulFoot » Sun Apr 19, 2015 2:04 pm

Code: Select all

$sql = 'SELECT username
FROM ' . USERS_TABLE . '
WHERE user_id = ' . $user_id;

$result = $db->sql_query($sql);
$username = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$username now holds the username. However, you already have some complicated database queries in your code, so not sure this is what you're asking.

Foul

TheTechGame
Registered User
Posts: 45
Joined: Wed Oct 31, 2012 3:08 am

Re: Getting Username from DB

Post by TheTechGame » Mon Apr 20, 2015 7:46 pm

FoulFoot wrote:

Code: Select all

$sql = 'SELECT username
FROM ' . USERS_TABLE . '
WHERE user_id = ' . $user_id;

$result = $db->sql_query($sql);
$username = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$username now holds the username. However, you already have some complicated database queries in your code, so not sure this is what you're asking.

Foul
That doesn't work for me, it pulls up an array:
memberlist.php?mode=viewprofile&comment=delete&cid=25&u=Array
Instead of the username

FoulFoot
Registered User
Posts: 261
Joined: Mon Jun 23, 2003 1:04 am
Location: Honolulu, HI
Contact:

Re: Getting Username from DB

Post by FoulFoot » Mon Apr 20, 2015 10:48 pm

Put this after the code I provided, and report back what it prints out:

Code: Select all

var_dump($username);
die;
Foul

Locked

Return to “[3.0.x] MOD Writers Discussion”