Solved

Displaying data depending on logged in user

Posted on 2008-10-22
7
225 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

13 Experts available now in Live!

Get 1:1 Help Now