1

After lurking for some years, I've finally created a user.

I'll try to explain my problem to you as good as I can:

I need a small browser friendly document (HTML, PHP, whatever) that enables me and a few colleagues to flip a switch so we can see if any drove to work. I can create the switch, and it works fine - however, I need it to keep it's state, spanning on several computers, and I was wondering if that is possible without making a DB?

I'd love to keep it simple, and in one file.

Example:

Dude1 comes to work, cycling. Switch is OFF

Dude2 comes to work, walking. Switch is OFF

Dude3 comes to work, driving. Switch is ON

--

Dude1 wants to see who's driving today. Opens the PHP/HTML and can see the status:

Dude1 - OFF

Dude2 - OFF

Dude3 - ON


I hope that explains my problem, if not please ask and I'll try to be clearer.

 <div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="switch" checked>
    <label class="onoffswitch-label" for="switch">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

That was the easy part - now how to keep the state.


Edit, added a picture. enter image description here

4
  • you need to post the variables, put everything inside a form with an action to a php script that either inserts the data into a database or stores it as a $_SESSION (the latter is not recommended) Commented Jul 12, 2017 at 10:07
  • Should every colleague be able to access the application from their devices or is there a shared/public station everyone uses for this purpose? What is the reason you don't want to use a database? Is a file-based database such as SQLite acceptable? Commented Jul 12, 2017 at 10:27
  • @simlev It should be accessable from their workstation/devices on the internal network. SQLite would be acceptable, although I don't know much about it - what is the requirements? The reason for not using a DB in the first place, was to make it as "small" as possible, like having a single PHP-file on a server, so they just had to navigate to server/file.php or 10.10.10.10/file.php Commented Jul 12, 2017 at 12:07
  • Then I'd recommend using a database, since it allows for greater flexibility at the "cost" of having an additional file other than your file.php. Requirements for SQLite are a writable file and the php sqlite extension. Commented Jul 12, 2017 at 12:41

3 Answers 3

3

There are several ways to do that without any database. The simplest one I can think of is using HTML5 Local Storage.

With this, you can manage to keep some data related to a specific website in the browser. Data is stored in JSON format, which apparently means you will be needing a key and a value.

Setting the value -

localStorage.setItem('driving', 'Yes');

Javascript to set the value of checkbox -

if (localStorage.getItem('driving')=='Yes'){
    //code to set your checkbox
}

For more checkout Web Storage

Sign up to request clarification or add additional context in comments.

Comments

2

Bro, to save state you have the following methods

Client Side

  1. LocalStorage
  2. SessionStorage

Server Side

  1. Session

Application wide

  1. DB
  2. File

Client Side & Server Side only works with in a computer. If you want to maintain in different computers then you have to go with DB or File.

8 Comments

So there is no "easy fix" for this? I have to create a DB and write the state of the switch, and then read it again? And if so - how do I do that, without making 10+ entries each day?
You would only write an entry for each colleague that drove to work; is that too much? Why?
if you don't want to use DB. Just write to a file. Override the entry after each colleague change the status. w3schools.com/php/func_filesystem_fwrite.asp
@simlev Wouldn't it write an entry for each click, or would it overwrite?
@imranahmed Thanks, I'll look into that link.
|
1

You cannot do this without a Database. Simple as that.

If you want to reduce the DB entries, you can keep a table with following fields:

id, name, status, date_modified

if a colleague turns on the switch for today, just update the status and date for that colleague

while displaying the list (for today), the rows having today's date will have the status as per the database, rest all will have the switch turned off

3 Comments

I did almost exactly what you wrote - but I'm having issues extracting the data from the date_modified row of the SQL? I used the CURRENT_TIMESTAMP to update the rows - but when I try to extract it, it ends up blank? Any ideas?
Stupid me - a simple typo. It works (almost) perfectly now. Thanks.
@j.andreasen - sorry! I gave the answer but forgot to reply to the follow-up questions. Glad to hear it is working. You're welcome!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.