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
Solved

Displaying data depending on logged in user

Posted on 2008-10-22
7
228 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 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