Distress Call

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
Tralalah
Forum Newbie
Posts: 15
Joined: Tue Mar 09, 2010 8:50 am

Distress Call

Post by Tralalah »

Greetings! I am making a distress call at 3AM in the morning. :banghead:

I have a dilemma and it goes like this: I am making a login-logout timer to be implemented on a server terminal for small number of PC terminals. There will be a LOGIN button to (1) post their username, (2) post the terminal they will use, and their starting time and will also trigger the timer. Then there will be a LOGOUT button to (1) stop the timer, (2) compute the elapsed time. AND, the logout button should make the terminal available again for the next user.

First, I have set up the structure of the timer. But I need the real time data to be posted as the starting time and the ending time. (Okay, so I probably wouldn't need that timer since real time is really ticking forward in time).

Second, on the LOGOUT button... I am having trouble figuring out how to clear the data (to make the terminal available again for other patrons) without deleting the previous data stored in the database.

Pardon me for such newbish inquiries. You may or may not put in the codes themselves (although that would be greatly appreciated as I hold a degree in Reverse Engineering :lol: ). I am also nearing a deadline so codes would be better alongside tutorials. Thanks to future respondents to this call for aid. :)
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: Distress Call

Post by Sofw_Arch_Dev »

Seems like you're nearly there. You may just need to rearchitect how you store the terminal session data. The actual schema you're working with would be handy here. But from what it sounds like, I assume you have a DB table to hold terminal session data with one row in that table per terminal (along with start and end/elapsed times for a given terminal).

Code: Select all

 
table terminal_session
  user_name
  terminal_number
  start_time
  end_time
 
This could work as is, but you need to have one row per terminal session rather than one row per terminal. Any row with a non-null end_time would represent a terminal that is in use. If you wanted to know if a terminal was available, you could select based on terminal_number and end_time being NOT NULL. "SELECT UNIQUE terminal_number FROM terminal_session WHERE end_time IS NULL". That yields the terminal_numbers that are in-use. But this has problems because as your table grows over time, a select like that would be time consuming.

A better solution would be to have two tables, one for terminals which contains a status field (a boolean or an ENUM), and one for terminal sessions. Ending a user's terminal session would require setting the end_time in the terminal_session table as well as updating the status field in the terminal table. Similarly for starting a terminal session. This design makes finding available terminals a lot easier and still allows you to keep a history of terminal usage in the terminal_session table. Plus it allows you to buff-out the terminal table and store other information about terminals if need be.

Code: Select all

 
table TERMINAL
   terminal_id
   terminal_number
   status
   ...
 
table TERMINAL_SESSION
    terminal_id
    user_name
    start_time
    end_time
    ...
 
Tralalah
Forum Newbie
Posts: 15
Joined: Tue Mar 09, 2010 8:50 am

Re: Distress Call

Post by Tralalah »

Thank you, kind sir. :) That fixes the problem with LOGIN and LOGOUT (only if the code is easier said than done :mrgreen: ), still thanks Sofw_Arch_Dev.
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: Distress Call

Post by Sofw_Arch_Dev »

I think you already have the code to do the update to the terminal_session table to set start_time and end_time. After you set the start time you just need a second query to update the terminal table to set the corresponding terminal's status to IN_USE or some such. Similarly when updating the terminal_session table's end_time you'd need a second query to update the terminal table's status to AVAILABLE.

Code: Select all

 
UPDATE terminal SET status='IN_USE' WHERE terminal_number=?;
..
UPDATE terminal SET status='AVAILABLE' WHERE terminal_number=?;
 
It may be more complicated than necessary to have a separate terminal_id and terminal_number. You may be able to get away with just using terminal_number as the primary key (or at least a UNIQUE column) on the terminal table and having terminal_number in the session table instead of terminal_id.

What's your schema look like?
Tralalah
Forum Newbie
Posts: 15
Joined: Tue Mar 09, 2010 8:50 am

Re: Distress Call

Post by Tralalah »

Thanks. :)
Post Reply