IF, Then situation php, mysql code help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m4tt
Forum Newbie
Posts: 8
Joined: Mon Apr 14, 2008 9:50 am

IF, Then situation php, mysql code help

Post by m4tt »

Ok I am working on some code for a time and attendance program and I need to make it so that when a user enters a status that is the same as the status they last entered (in, out, break, lunch, etc..) It will give them an alert and not allow the submission to be entered into the data base. The basic structure of the Mysql table for these areas would be the employee number (which I have under "fullname") and the inout with values of status.

So for example when employee number "fullname" 222 has status of break punches in and submits the form with status of break I need to have a message of "you are already punched in as status break"

I have all the code for everything else except how to figure out how to do this "if , then" condition with the data from the mysql database.

Thanks for your help.

Image
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: IF, Then situation php, mysql code help

Post by pickle »

Before the query runs that stores the status, do another query that asks for the most recent (based on the `timestamp` column) entry for that person. You can then compare the status they're trying to store, with their most recent status.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
m4tt
Forum Newbie
Posts: 8
Joined: Mon Apr 14, 2008 9:50 am

Re: IF, Then situation php, mysql code help

Post by m4tt »

Ok, thank you I will give that a try.
m4tt
Forum Newbie
Posts: 8
Joined: Mon Apr 14, 2008 9:50 am

Re: IF, Then situation php, mysql code help

Post by m4tt »

How would I start the code?

I am lost any help would be appreciated even if it is just a link to a manual section that will tell me how to do it. Or a line of example code even just something to get me started. Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: IF, Then situation php, mysql code help

Post by pickle »

Pseudo code:

Code: Select all

if(user has submitted the form)
{
    $status = newly submitted status
 
    $query = "most recent entry submitted by this user"
    
    if(status returned from query == $status)
    {
        display "Error"
    }
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
m4tt
Forum Newbie
Posts: 8
Joined: Mon Apr 14, 2008 9:50 am

Re: IF, Then situation php, mysql code help

Post by m4tt »

Ok I have made progress and have a variable set for the "inout" status that is being submitted. I need to figure out how to query the "inout" status that is already in the database so I can set that to a variable. Any help please the table is listed above and the submit form variable for the user is "$fullname"

I was trying the following but it pops the message up everytime instead of just when the new status matches the old

Code: Select all

$query = "select inout
              from info where fullname == $fullname
              ";
    $result = mysql_query($query);
$status = $result;
 
   if ($inout == $status)
echo "Please contact your supervisor\n";
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: IF, Then situation php, mysql code help

Post by aceconcepts »

Hi Matt,

To make sure I understand: you are trying to check the user's current status (in the database) with their newly selected status.

If the new status is the same as the current status then throw a message.

Is this correct?
m4tt
Forum Newbie
Posts: 8
Joined: Mon Apr 14, 2008 9:50 am

Re: IF, Then situation php, mysql code help

Post by m4tt »

yes that is what I am trying
thanks
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: IF, Then situation php, mysql code help

Post by aceconcepts »

Ok, what i'd probably try is:

Code: Select all

 
$query=mysql_query("SELECT * FROM info WHERE fullname='$fullname' ");
$row=mysql_fetch_array($query);
 
if($row['inout']==$new_status)
{
   //MESSAGE - STATUS IS SAME AS BEFORE bla bla bla
} else {
   //UPDATE STATUS
   $update=mysql_query("UPDATE info SET inout='$new_status' WHERE fullname='$fullname'");
}
 
I hope this works for you.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: IF, Then situation php, mysql code help

Post by pickle »

I believe you'll have to add an "ORDER BY timestamp DESC' clause to make sure you get the latest entry.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
m4tt
Forum Newbie
Posts: 8
Joined: Mon Apr 14, 2008 9:50 am

Re: IF, Then situation php, mysql code help

Post by m4tt »

Thanks guys, I will try your suggestions and do a lot more reading to get some more basics down. I ll post back with updates.
Post Reply