• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Displaying data depending on logged in user

We have a timesheet database and by the looks of it all users just add to 1 table. What im trying to do is make a report that shows only what each user has submited. Could someone guide me to the right path?
0
prophit4024
Asked:
prophit4024
  • 3
  • 3
3 Solutions
 
Roger BaklundCommented:
What kind of database? Do you have a table definition/schema? Do you know what column in the table identifies the user? Need more info...
0
 
prophit4024Author Commented:
Its a mysql database and we are using a iis6 with php.
Schema is
ID,mon,tues,weds,thurs,fri,sat,sun,total,user,datesub,weekend,
ITs a really simple database. User logs in and is able to enter the time. Currently not able to edit it or even view it. Only mgr is able to edit and view database. Trying to make it now to where the users can see and edit only thier entries.
0
 
Roger BaklundCommented:
ok.

In your php script, you allready have a logged in user? The name of the logged in user should be stored in a variable, lets call it $user. The table maybe is called "timesheet". Below is PHP code that will list rows related to a single user.

You would probably want the output formatted a different way, I guess a table would be best in this case, if it is a on-screen report. If there are very many records, you would want pagination, i.e. limit the number of rows to show for each screen.
# connect to database
$db=mysql_connect('host','user','pass') or die('Could not connect to database server');
mysql_select_db('dbname') or die('database not found');
# select everything exept 'user'
$sql = "select ID, mon, tues, weds, thurs, fri, sat, sun,
  total, datesub, weekend
  from timesheet where user='$user'";
$res = mysql_query($sql);
if(!$res) die('Error: '.mysql_error()); # check for errors
 
# loop trough results
while($row = mysql_fetch_assoc($res)) {
  echo $row['ID'].' '.
       $row['mon'].' '.
       $row['tues'].' '.
       $row['weds'].' '.
       $row['thurs'].' '.
       $row['fri'].' '.
       $row['sat'].' '.
       $row['sun'].' '.
       $row['total'].' '.
       $row['datesub'].' '.
       $row['weekend'].'<br />';
} # end of loop

Open in new window

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
prophit4024Author Commented:
Perfect. Yes the user does log in and we use a sessions to keep the logged in. I had to tweak it a little to get it to work but it did. Now if you mind is there away to get it to check maybe the roles and if they are a mgr it will show all entries?
0
 
psimationCommented:
if you store the role in another table, say "users", and you save that role variable to session on login as , say, $_SESSION[role], then in the script above, simply do:

# connect to database
$db=mysql_connect('host','user','pass') or die('Could not connect to database server');
mysql_select_db('dbname') or die('database not found');
# select everything exept 'user'

if ($_SESSION[role] == "mgr"){

$sql = "select ID, mon, tues, weds, thurs, fri, sat, sun,
  total, datesub, weekend
  from timesheet";

} else {

$sql = "select ID, mon, tues, weds, thurs, fri, sat, sun,
  total, datesub, weekend
  from timesheet where user='$user'";
}
$res = mysql_query($sql);
if(!$res) die('Error: '.mysql_error()); # check for errors
 
# loop trough results
while($row = mysql_fetch_assoc($res)) {
  echo $row['ID'].' '.
       $row['mon'].' '.
       $row['tues'].' '.
       $row['weds'].' '.
       $row['thurs'].' '.
       $row['fri'].' '.
       $row['sat'].' '.
       $row['sun'].' '.
       $row['total'].' '.
       $row['datesub'].' '.
       $row['weekend'].'<br />';
} # end of loop
0
 
Roger BaklundCommented:
For security reasons I would not store the role in the session on login, I would rather do the lookup in the user table for each access. Also, the manager needs to know which records belongs to which user.
# connect to database
$db=mysql_connect('host','user','pass') or die('Could not connect to database server');
mysql_select_db('dbname') or die('database not found');
 
# find the role for this user
$sql = "select role from user where user='$user'";
$res = mysql_query($sql);
if(!$res) die('Error: '.mysql_error()); # check for errors
$role = mysql_result($res,0);
 
if($role=='mgr') 
  # select everything, order by user, ID
  $sql = "select user, ID, mon, tues, weds, thurs, fri, sat, sun,
    total, datesub, weekend
    from timesheet 
    order by user,ID";
else
  # select everything except user, order by ID
  $sql = "select ID, mon, tues, weds, thurs, fri, sat, sun,
    total, datesub, weekend
    from timesheet where user='$user'
    order by ID";
$res = mysql_query($sql);
if(!$res) die('Error: '.mysql_error()); # check for errors
 
# loop trough results
while($row = mysql_fetch_assoc($res)) {
  echo (($role=='mgr')? # show user if this is a manager
       $row['user'].' ' : '').
       $row['ID'].' '.
       $row['mon'].' '.
       $row['tues'].' '.
       $row['weds'].' '.
       $row['thurs'].' '.
       $row['fri'].' '.
       $row['sat'].' '.
       $row['sun'].' '.
       $row['total'].' '.
       $row['datesub'].' '.
       $row['weekend'].'<br />';
} # end of loop

Open in new window

0
 
prophit4024Author Commented:
well after posting lastnight i just ended up using a another page all togeither for the mgr. But the last entry would be better for security purpose. It uses another script to check roles the gives the ya or na depending on whos signed in. thanks for all the help. Im sure i will be asking more questions as the client wants more functionablity. thanks again!! points will be awared to CXR as he answered the orginal question.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now