Jump to content

Recommended Posts

Hi,

 

I have a table for users which has two types of users. In the table there is a usertypeid which gets set to either 1 or 2 depending on what type of user is registered.

 

When a user logs in to the website I have set it up to have a profile page which allows them to see all the details that they had input.

 

I want to have links on the profile page to enable them to carry out tasks, but the links will be different for the different users that log in.

 

So i needed a way to hide them, so I was thinking using an IF statement....

 

IF(usertypeid == 1)

{

    Display link;

}

 

However I'm not sure how to get the usertypeid from the users table of the user logged in and assign it to the variable.

 

Any ideas????

 

I managed to use the username from the SESSION variable to do this, but I need it for usertypeid column.

 

Thanks in advance

 

 

Set the usertypeid into the $_SESSION variable the same way you set the username.  Assuming that the usertypeid is in the same table as your username...

 

Not tested...


//Get the username from the database
$query  = "SELECT usertypeid FROM user WHERE username='{$username}'";
$result = mysql_query($query);

//Assign the username into the $_SESSION
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$_SESSION['usertypeid'] = $row['usertypeid'];

//Do something depending on the usertypeid
switch($_SESSION['usertypeid'])
{
    case 1:
    {
        //Do something
        break;
    }
    case 2:
    {
        //Do something else
        break;
    }
    default:
    {
        //This is probably a usertypeid that we do not support or the result we're getting from the database is incorrect.
        break;
    }
}//switch

 

 

Those are the basics.  You'll want to clean your variables before running a MySQL statement or use Pear MDB2.

 

 

Personally I would do as zeodragonzord suggests, however, if you want to leave your session intact as you've set it before, you could do as follows. You run a query like so:

 

$result = mysql_query('SELECT * FROM users WHERE username = '.addslashes($_SESSION['username']).' LIMIT 1');

 

Then check if the result returned one row:

 

if (mysql_num_rows($result) == 1)

 

If so you get the data:

 

$user_data = mysql_fetch_assoc($result);

 

Then you do your if / else links:

 

if ($user_data['usertypeid'] == 1)
  //do something
else
  //do something else

 

Overall it should look something like this:

 

$result = mysql_query('SELECT * FROM users WHERE username = '.addslashes($_SESSION['username']).' LIMIT 1');
if (mysql_num_rows($result) == 1)
  $user_data = mysql_fetch_assoc($result);

if (is_array($user_data) && $user_data['usertypeid'] == 1)
  //do something
elseif (is_array($user_data) && $user_data['usertypeid'] == 2)
  //do something else
else
  //do something for non-logged in users

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.