Problems using format_date()

Discussion forum for MOD Writers regarding MOD Development.
Locked
Maruz
Registered User
Posts: 9
Joined: Mon Aug 25, 2008 2:21 pm
Location: Oslo, Norway

Problems using format_date()

Post by Maruz »

Hi, I'm new at trying to modify phpBB forums, and so far I've managed ok, but I've got a small problem with format_date().

I've set up a function:

Code: Select all

function getUserLoginTime($user_id) {
	$user_lastvisit = sprintf("SELECT user_lastvisit FROM phpbb_users WHERE user_id = '%s' LIMIT 1", $user_id);
	$user_lastvisit = mysql_fetch_array(mysql_query($user_lastvisit));
	$lastvisit = $user_lastvisit['user_lastvisit'];
	
	return $lastvisit;
}
create a variable:

Code: Select all

$user_lastvisit = getUserLoginTime($user_id);
and print out:

Code: Select all

echo "Login time: " . $user->format_date($user_lastvisit);
So far so good, but when I try the following I get the fatal error: Call to a member function format_date() on a non-object

Code: Select all

function getPosts($user_lastvisit, $topic_id) {
	$select_posts = sprintf("SELECT post_text, poster_id, post_time FROM phpbb_posts WHERE (post_time > '%s') AND topic_id = '%s'", 
					$user_lastvisit, $topic_id);
	$select_posts = mysql_query($select_posts);

	return $select_posts;
}

while($post = mysql_fetch_array(getPosts($user_lastvisit, $topic_id))) {
	$text = $post['post_text'];
	$user = getUserByID($post['poster_id']);
	$time = $user->format_date($post['post_time']); //<--- this is the problem
	
	$output = <<< OUTPUT
	$text (posted by $user at $time)
OUTPUT;

	echo $output; /**/
}
If I just skip the $user->format_date() method in the while loop, everything works as intended only the date is not formatted obviously. I'm guessing this has to do with the fact that $time comes from an array, and I get a conflict between the array and the method expecting an object. But is there a way to achieve what I describe here?
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52126
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Problems using format_date()

Post by Brf »

Code: Select all

   $user = getUserByID($post['poster_id']);
   $time = $user->format_date($post['post_time']); //<--- this is the problem
That first line is overwriting the $user object, assuming the $user object has context there.
Maruz
Registered User
Posts: 9
Joined: Mon Aug 25, 2008 2:21 pm
Location: Oslo, Norway

Re: Problems using format_date()

Post by Maruz »

Doh, stupid error... :( Changed the variable $user to $username and it worked perfectly. Thanks allot!
User avatar
igorw
Former Team Member
Posts: 8024
Joined: Fri Dec 16, 2005 12:23 pm
Location: {postrow.POSTER_FROM}
Name: Igor Wiedler

Re: Problems using format_date()

Post by igorw »

Code: Select all

   $select_posts = sprintf("SELECT post_text, poster_id, post_time FROM phpbb_posts WHERE (post_time > '%s') AND topic_id = '%s'", 
Using something like prepared statements is nice, but it should be used correctly. %s should probably be %i here.
Igor Wiedler | area51 | GitHub | trashbin | Formerly known as evil less than three
User avatar
Typo
Former Team Member
Posts: 404
Joined: Tue Dec 30, 2008 6:12 am
Location: USA
Name: Jeremy Lorenzo
Contact:

Re: Problems using format_date()

Post by Typo »

eviL<3 wrote:

Code: Select all

   $select_posts = sprintf("SELECT post_text, poster_id, post_time FROM phpbb_posts WHERE (post_time > '%s') AND topic_id = '%s'", 
Using something like prepared statements is nice, but it should be used correctly. %s should probably be %i here.
Why should %i be used in place of %s?

I know when naming variables, phpbb recommends starting at $i in the coding guidelines but I don't see anything about %.
The only situation where a one-character variable name is allowed is when it's the index for some looping construct. In this case, the index of the outer loop should always be $i. If there's a loop inside that loop, its index should be $j, followed by $k, and so on. If the loop is being indexed by some already-existing variable with a meaningful name, this guideline does not apply, example:
I also notice that %s is used in a couple places in the coding guidelines and %i doesn't even appear in the whole thing.

For instance..

Code: Select all

'PAGE_OF'	=>	'Page %s of %s',
or

Code: Select all

'CONV_ERROR_NO_AVATAR_PATH'
	=>	'Note to developer: you must specify $convertor[\'avatar_path\'] to use %s.',
This leaves me wondering why you advise what you are?

Just curious.

Typo
comkidwizzer3
Registered User
Posts: 375
Joined: Fri Jul 13, 2007 8:24 am
Location: $user->data['user_location'];
Contact:

Re: Problems using format_date()

Post by comkidwizzer3 »

He's thinking about what it would stand for ;)

Like %i = id = topic_id ;)
~My MODs~
Login After Register - v1.0.0 | Custom Ranks MOD - RC

~!Hasher!~
User avatar
igorw
Former Team Member
Posts: 8024
Joined: Fri Dec 16, 2005 12:23 pm
Location: {postrow.POSTER_FROM}
Name: Igor Wiedler

Re: Problems using format_date()

Post by igorw »

No. :)

Take a look at the sprintf documentation: http://ch2.php.net/sprintf. Different names correspond to different variable types. %s is a string, %i is an integer. It makes sure that it's rendered as a number.
Igor Wiedler | area51 | GitHub | trashbin | Formerly known as evil less than three
User avatar
Typo
Former Team Member
Posts: 404
Joined: Tue Dec 30, 2008 6:12 am
Location: USA
Name: Jeremy Lorenzo
Contact:

Re: Problems using format_date()

Post by Typo »

Thanks evil, cleared it up perfectly.

Typo
comkidwizzer3
Registered User
Posts: 375
Joined: Fri Jul 13, 2007 8:24 am
Location: $user->data['user_location'];
Contact:

Re: Problems using format_date()

Post by comkidwizzer3 »

eviL<3 wrote:No. :)

Take a look at the sprintf documentation: http://ch2.php.net/sprintf. Different names correspond to different variable types. %s is a string, %i is an integer. It makes sure that it's rendered as a number.
Oh :lol:

I just looked at it and the first thing I thought of was id :roll:
~My MODs~
Login After Register - v1.0.0 | Custom Ranks MOD - RC

~!Hasher!~
Locked

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