[Function] convert_id()

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
User avatar
battye
Extension Customisations
Extension Customisations
Posts: 10933
Joined: Wed Feb 11, 2004 11:02 am
Location: Australia
Contact:

[Function] convert_id()

Post by battye » Sat Apr 02, 2005 6:55 am

This is a fairly basic function that I've made which I have found to be pretty useful. I hope I haven't missed another function that does the same thing! :lol:

What does it do?
Really, what this function does, is convert any user_id, to a username (providing that a user with that particular id exists).

I don't get it.
For example, say in my script I had a need to determine the username of the board founder. I could use the following:

Code: Select all

$board_founder = convert_id(2);
As the board founders user id is always 2, it will output the username (in my case: battye)

Wow, I understand now! But what if I want to determine several usernames?
The fun doesn't stop there, it can be used in while loops too!

In some cases it can be easy enough to simply use an SQL query to gain the username, but if you have to cross tables it may not be worth the trouble, so you simply use something like:

Code: Select all

while( $row = $db->sql_fetchrow($result) )
	{
	$usernames = convert_id($row['user_id']);
	}
I'm dying to use this function, what do I do?
It's so simple, open functions.php and find the following line:

Code: Select all

function get_db_stat($mode)
Directly before that, add the following code (the convert_id function):

Code: Select all

function convert_id($id)
{
	global $db;

	$id = intval($id);

	$sql = "SELECT username FROM " . USERS_TABLE . " WHERE user_id = " . $id;
	
	if ( !$result = $db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, 'No username could be obtained', '', __LINE__, __FILE__, $sql);
	}

	while ( $row = $db->sql_fetchrow($result) )
	{
	$username = $row['username'];
	}
	
	return $username;
}
Enjoy :)
Customisations Team Member

User avatar
battye
Extension Customisations
Extension Customisations
Posts: 10933
Joined: Wed Feb 11, 2004 11:02 am
Location: Australia
Contact:

Post by battye » Wed Jul 13, 2005 3:32 pm

And by request, username -> user_id :)

Code: Select all

function convert_name($username)
{
   global $db;

   $id = intval($id);

   $sql = "SELECT user_id FROM " . USERS_TABLE . " WHERE username = " . $username;
   
   if ( !$result = $db->sql_query($sql) )
   {
      message_die(GENERAL_ERROR, 'No username could be obtained', '', __LINE__, __FILE__, $sql);
   }

   while ( $row = $db->sql_fetchrow($result) )
   {
   $user_id = $row['user_id'];
   }
   
   return $user_id;
}
Customisations Team Member

User avatar
Ptirhiik
Registered User
Posts: 7411
Joined: Mon Jan 06, 2003 10:36 pm
Contact:

Post by Ptirhiik » Wed Jul 13, 2005 3:53 pm

Oho, some re-reading is required, especially regarding the second one...

User avatar
battye
Extension Customisations
Extension Customisations
Posts: 10933
Joined: Wed Feb 11, 2004 11:02 am
Location: Australia
Contact:

Post by battye » Wed Jul 13, 2005 4:13 pm

Haven't tested it, that'd be why :lol:
Thanks :)
Customisations Team Member

User avatar
vze3k59w
Registered User
Posts: 485
Joined: Fri May 09, 2003 1:09 am

Post by vze3k59w » Tue Aug 02, 2005 1:19 am

Think of a better way of getting to the query, a while loop should not be needed in a query that can return only one item.
Wanna join a very advanced OO based modular port of phpBB? PM me :)

Swizec
Former Team Member
Posts: 1701
Joined: Mon Mar 10, 2003 9:42 pm
Location: Slovenia
Contact:

Post by Swizec » Tue Aug 02, 2005 5:48 am

is it really worth adding another sql query just to get a username?

what about the good 'ol "JOIN LEFT"?

afterlife_69
I've Been Banned!
Posts: 630
Joined: Tue Nov 30, 2004 10:35 am

Post by afterlife_69 » Tue Aug 02, 2005 10:52 am

Swizec wrote: is it really worth adding another sql query just to get a username?

what about the good 'ol "JOIN LEFT"?
Demo?

User avatar
Flaming_cows
Registered User
Posts: 761
Joined: Sat Jul 05, 2003 1:43 am
Contact:

Post by Flaming_cows » Tue Aug 02, 2005 1:17 pm

Swizec wrote: is it really worth adding another sql query just to get a username?

what about the good 'ol "JOIN LEFT"?

Joining is for merging data across tables, how would that help in retreiving one field from one table? These functions aren't restricted to use in other queries.

battye: Use $db->sql_free_result() and don't use a while loop to get one field from one row.

Swizec
Former Team Member
Posts: 1701
Joined: Mon Mar 10, 2003 9:42 pm
Location: Slovenia
Contact:

Post by Swizec » Tue Aug 02, 2005 5:20 pm

yes, they are to get data from a wide range of tables

I like to query for allt he data I need in one query, which means I include usernames in with the query

Post Reply

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