How to create an if statement for a single member

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
norbie
Registered User
Posts: 4
Joined: Fri Mar 14, 2008 11:09 pm

How to create an if statement for a single member

Post by norbie » Fri Mar 14, 2008 11:13 pm

Hi guys,

Complicated title, sorry.

What I want to create for phpbb 2.0.x is the following:

if username = "username" then
do stuff here
end if

How can I set this up within phpbb 2.0.x?

I'd need the syntax for the if statement, and I'd also need to know where to put the code (probably viewtopic.php?).

Many thanks.

User avatar
drathbun
Former Team Member
Posts: 12204
Joined: Thu Jun 06, 2002 3:51 pm
Location: TOPICS_TABLE
Contact:

Re: How to create an if statement for a single member

Post by drathbun » Sat Mar 15, 2008 12:40 am

Hi, coding tips and questions are best posted in the MOD Writers forum, so I will move your topic there now, thanks. Good luck with your project. 8-)
I blog about phpBB: phpBBDoctor blog
Still using phpbb2? So am I! Click below for details
Image

User avatar
Elias
Registered User
Posts: 4626
Joined: Sat Feb 25, 2006 4:31 pm
Location: In the Water!
Name: Elias

Re: How to create an if statement for a single member

Post by Elias » Sat Mar 15, 2008 12:48 am

Here is the if-else syntax:

Code: Select all

if (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false; 
Here is a codded syntax example:

Code: Select all

if ($d=="Fri")
  echo "Have a nice weekend!"; 
else
  echo "Have a nice day!"; 
?>
"Mystery creates wonder, and wonder is the basis of man's desire to understand." - Neil Armstrong
|Installing Extensions|Writing Extensions|Extension Validation Policy|

norbie
Registered User
Posts: 4
Joined: Fri Mar 14, 2008 11:09 pm

Re: How to create an if statement for a single member

Post by norbie » Sat Mar 15, 2008 9:29 am

Thanks drathbun and EY.

I know the general php if syntax, but not where to put the code in the phpbb files, and what the phpbb code would be for checking a username.

jalaln
Registered User
Posts: 1076
Joined: Tue Jul 05, 2005 5:05 am

Re: How to create an if statement for a single member

Post by jalaln » Sat Mar 15, 2008 9:38 am

Using user's ID:

Code: Select all

if ( $userdata['user_id'] == 10149)
{
      statements;
}   
Using user's username:

Code: Select all

if ( $userdata['username'] == 'Sarah')
{
      statements;
}   
You would better use user_id instead of username since some users can change their name on some boards.

What are you planning to do?
No more spambots for phpBB2 and phpBB3.

norbie
Registered User
Posts: 4
Joined: Fri Mar 14, 2008 11:09 pm

Re: How to create an if statement for a single member

Post by norbie » Sat Mar 15, 2008 9:51 am

Thanks jalaln, great help.

What I'm basically planning on doing is something similar to:

Code: Select all

if ( $userdata['user_id'] == 10149)
{
      ** show a message for this user at the top of every page **
}
That's just an example, I'd like to get it to show when viewing topics so I guess viewtopic.php will need editing, but I'm unsure how you get that to actually display - I think you need to edit the template html files too, but not sure on this.

jalaln
Registered User
Posts: 1076
Joined: Tue Jul 05, 2005 5:05 am

Re: How to create an if statement for a single member

Post by jalaln » Sat Mar 15, 2008 10:04 am

Try this:

Open: includes/page_header.php

Find:

Code: Select all

$template->assign_vars(array(
	'SITENAME' => $board_config['sitename'],
Replace with:

Code: Select all

$x_message = ( $userdata['user_id'] == 2 || $userdata['user_id'] == 3 ) ? '&nbsp;<strong>Hello</strong>' : '' ;

$template->assign_vars(array(
	'SITENAME' => $board_config['sitename'],
	'X_MESSAGE' => $x_message,
Open templates/subSilver/overall_header.tpl

Add:

Code: Select all

{X_MESSAGE}
wherever you want.
No more spambots for phpBB2 and phpBB3.

norbie
Registered User
Posts: 4
Joined: Fri Mar 14, 2008 11:09 pm

Re: How to create an if statement for a single member

Post by norbie » Sat Mar 15, 2008 2:28 pm

Thanks jalaln, that worked really well.

How can I create an else statement with that syntax, I notice it's slightly different to the usual if statement.

Example:

Code: Select all

$x_header = if ( $userdata['user_id'] == 104 || $userdata['user_id'] == 60 || $userdata['user_id'] == 569 ) {
echo '<img border=\"0\" src=\"/images/header.jpg\" usemap=\"#FPMap0\">' : '' ;
} else {
echo '<img border=\"0\" src=\"/images/header.jpg\" usemap=\"#FPMap0\">' : '' ;
}
(That code doesn't work).

Edit: Ignore that, I realised it was the second part of the code you supplied.

Thanks again :)

jalaln
Registered User
Posts: 1076
Joined: Tue Jul 05, 2005 5:05 am

Re: How to create an if statement for a single member

Post by jalaln » Sat Mar 15, 2008 4:54 pm

norbie wrote:Edit: Ignore that, I realised it was the second part of the code you supplied.
Yup. That would be it:

Code: Select all

$x_header = ( $userdata['user_id'] == 60 || $userdata['user_id'] == 104 || $userdata['user_id'] == 569 ) ? '<img border='0' src='images/header.jpg' usemap='#FPMap0'>' : '<img border='0' src='images/header.jpg' usemap='#FPMap1'>' ;
No more spambots for phpBB2 and phpBB3.

Post Reply

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