Solved

Displaying data depending on logged in user

Posted on 2008-10-22
7
230 Views
Last Modified: 2012-08-14
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
Comment
Question by:prophit4024
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22782147
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
 

Author Comment

by:prophit4024
ID: 22782172
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
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 100 total points
ID: 22782226
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:prophit4024
ID: 22782672
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
 
LVL 17

Assisted Solution

by:psimation
psimation earned 25 total points
ID: 22784251
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
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 100 total points
ID: 22784550
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
 

Author Comment

by:prophit4024
ID: 22784880
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 81
Why isn't my DIV and Form centering? 1 28
PHP and accessing Array Elements 3 34
Select Query Fails in PHP but not in TERMINAL Mysql 9 31
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question