Solved

Displaying data depending on logged in user

Posted on 2008-10-22
7
226 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mimic google as my ip 11 53
WordPress syntax 2 25
unset shopping cart session 15 29
How can I change this hyperlinked phone number to be black and is it working? 2 0
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
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 count occurrences of each item in an array.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now