Need a PHP in/out board

Status
Not open for further replies.

sarbz

[H]ard|Gawd
Joined
Feb 1, 2005
Messages
1,319
Can anyone help me?

I need to write a script in PHP that will allow employees at my company to check an in or out box next to their name on our intranet, to let other employees know where they are. We don't need any security measures, anyone will be able to alter the status of anyone else's name. Very straight forward.

Sounds simple, right? Only problem is that I am clueless when it comes to PHP. Can this be done with a flat file? Either that or mysql, whatever is easiest.

If anyone has written or seen this type of script anywhere, I would greatly appreciate a nod in the right direction.

Thanks!
 
It sounds simple. You'd likely be better doing it with an SQL backend, but flatfiles may work.

It sounds like a nice, easy intro to PHP (and maybe SQL project) for yourself.
 
I know, and I want to learn, but I'm pressed for time on this project.
 
Doesn't seem to fool proof though.Couldn't employees sign in for other people?

Either way, this would be pretty easy to setup. There might be some scripts out there for you.
 
You could do it with a flatfile, and it would save you the trouble of setting up a MySQL server. Basically, what you would want to do is this (and it can be done in one php page :) :

1) Open the flatfile
PHP:
<?php
$loc_file=fopen("wherever\the\file\is.txt");
2) Based on how you set up the flatfile (I'd suggest something like "FirstName|LastName|(1/0);"), loop through the file and spit out names and a checkbox for each like so:
PHP:
// Create an array, each "row" will contain FirstName|LastName,1/0
$person=explode(";",$loc_file);
echo "<form action=" . $PHP_SELF . " method='POST'>
echo "<table>";
foreach ($person as $item) {
// Get each piece for the person
list($fname,$lname,$status)=explode("|",$item);
echo "<tr><td>$fname $lname</td>";
echo "<td><input type='checkbox' name='".$fname.$lname." value='".$status."'></td></tr>";
}

I'm pretty sure a 1 or a 0 means 'On' and 'Off', respectively for checkboxes, but correct me if I'm wrong.

3) Add an "Update" button

PHP:
echo "<tr><td><input type='submit' value='Update' name='Submit'></td></tr>";
echo "</table>";
echo "</form>";
?>

4) At the top of the page, right after the "<?php", you need to put a variable that will grab the data that is updated and then update the flatfile. I'll come back and finish it if someone else doesn't (I'm at work). Hope that helps!
 
scotty do said:
Doesn't seem to fool proof though.Couldn't employees sign in for other people?

Either way, this would be pretty easy to setup. There might be some scripts out there for you.

I've looked, and surprisingly enough, there isn't. As far as fool-proof goes, it doesn't matter to me. We don't need that level of security. If employee A wants to sign out employee B, it doesn't do them any good. This isn't for payroll purposes. Just so we can tell if people are in the office or not without having to go up front to the sign in/out board.

Rabbit - thanks! I'll see what I can do! Would greatly appreciate the variable part too!

P.S. I am a graphic designer. Is there anything I can help you with? I feel like I owe you something :)
 
I modified the code a little, your flatfile should read something like:
------------------------
Bob Smith,on
Tom Jones,off
Pat Rock,on
------------------------

with each entry separated by a newline, and the in office check separated from the name by a comma (no spaces except between first & last name).

Note: I haven't tested this yet, so I'm not 100% that it works like it should. Also, backup your flatfile, since the way it writes the post values in is by w+ (truncates the file to 0 length and writes it all in again). Someone probably has a better idea on flatfile usage but that's what I thought off the top of my head.

PHP:
<?
$ff="/wherever/file/is/at.txt";
if (isset($_POST)) {
	foreach ($_POST as $name=>$value) {
		
		if ($value=="on" || $value=="off") {
			$somecontent.= $value . "\n";
		}
		else {
			$somecontent.= $value . ",";
		}
	
	}
	$handle=fopen($ff, "w+");
	if (fwrite($handle, $somecontent) === FALSE) {
		echo "Cannot write to file ($ff)";
		exit;
	}
  	fclose($ff);
}

display_file($ff);

//Functions------------------------
function display_file($file){

//Put file into $contents
$contents = file_get_contents($file);

//Split each file row into an array value
$contents=explode("\n",$contents);

// You could try using $PHP_SELF instead of $_SERVER['PHP_SELF'], but if the page is only being accessed through IE you can just leave it.
echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'><table>";
$i=0;
foreach($contents as $item){
	$i+=1;
	$person=explode(",",$item);
	echo "
<tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>
if ($person[1]=='on') {
$mark='CHECKED';
}
else {
$mark='';
}
<input type='checkbox' name='".$i."c' value='$person[1]' $mark>In office?</td></tr>";
}
// Put the Update button under it all
echo "<tr><td><input type='submit' name='Submit' value='Update'></td></tr>";
echo "</table></form>";
}
?>
 
Thanks Rabbit!

I copied and pasted the code, and set up the flat file, and this is what I get when I run it:

inout.jpg


Any ideas?

It also completely erases everything in my flat file.
 
in the middle of the code there's a section of
Code:
echo "
blahblahblahblah
if($person[1]=='on'...

Make the middle line end as
Code:
blahblahblah";
 
after this line right?
Code:
<tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>

I put the "; in there, and now I get a blank page in ie when I run it.

Here's the updated code:

PHP:
<?
$ff="/users/wensco/sites/inout/inoutdb.txt";
if (isset($_POST)) {
    foreach ($_POST as $name=>$value) {
        
        if ($value=="on" || $value=="off") {
            $somecontent.= $value . "\n";
        }
        else {
            $somecontent.= $value . ",";
        }
    
    }
    $handle=fopen($ff, "w+");
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($ff)";
        exit;
    }
      fclose($ff);
}

display_file($ff);

//Functions------------------------
function display_file($file){

//Put file into $contents
$contents = file_get_contents($file);

//Split each file row into an array value
$contents=explode("\n",$contents);

// You could try using $PHP_SELF instead of $_SERVER['PHP_SELF'], but if the page is only being accessed through IE you can just leave it.
echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'><table>";
$i=0;
foreach($contents as $item){
    $i+=1;
    $person=explode(",",$item);
    echo "
<tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>";
if ($person[1]=='on') {
$mark='CHECKED';
}
else {
$mark='';
}
<input type='checkbox' name='".$i."c' value='$person[1]' $mark>In office?</td></tr>";
}
// Put the Update button under it all
echo "<tr><td><input type='submit' name='Submit' value='Update'></td></tr>";
echo "</table></form>";
}
?>

I'm set on learning php now. I have a ton of things I want to do. It's all in my head, and I want to get it out.....I just have a lot to learn.

Thanks for the patience and direction so far!
 
o.k. sorry for the double post.....

I added an echo " before one of the lines and it's running now...but....

It's deleting whatever is in my inoutdb.txt file, and replacing it with:

Code:
,Update,Update,

when I check the "in office" box and update

or:

Code:
,Update,

When I leave the box unchecked and update.

Here's what it looks like in IE:

inout2.jpg


Here's the current code:

PHP:
<?
$ff="/users/wensco/sites/inout/inoutdb.txt";
if (isset($_POST)) {
    foreach ($_POST as $name=>$value) {
        
        if ($value=="on" || $value=="off") {
            $somecontent.= $value . "\n";
        }
        else {
            $somecontent.= $value . ",";
        }
    
    }
    $handle=fopen($ff, "w+");
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($ff)";
        exit;
    }
      fclose($ff);
}

display_file($ff);

//Functions------------------------
function display_file($file){

//Put file into $contents
$contents = file_get_contents($file);

//Split each file row into an array value
$contents=explode("\n",$contents);

// You could try using $PHP_SELF instead of $_SERVER['PHP_SELF'], but if the page is only being accessed through IE you can just leave it.
echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'><table>";
$i=0;
foreach($contents as $item){
    $i+=1;
    $person=explode(",",$item);
    echo "
<tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>";
if ($person[1]=='on') {
$mark='CHECKED';
}
else {
$mark='';
}
echo "<input type='checkbox' name='".$i."c' value='$person[1]' $mark>In office?</td></tr>";
}
// Put the Update button under it all
echo "<tr><td><input type='submit' name='Submit' value='Update'></td></tr>";
echo "</table></form>";
}
?>

Any ideas?
 
Sorry about that missing end echo statement, I must've overlooked it.

Edit: Reread your post and saw that it does show a blank, ok.

Try commenting out the first chunk of code, the whole if block. Run it with just the display and see if it shows data.

I'm debugging blind here, as I don't have access to php at work, but we'll figure it out.

Try this and see what comes out:

PHP:
<?
$ff="/users/wensco/sites/inout/inoutdb.txt";

display_file($ff);

//Functions------------------------
function display_file($file){

//Put file into $contents
$contents = file_get_contents($file);

//Lets see if it has anything
echo "<b>FILE:</b>";
print_r($file);
echo "<br><b>STRING:</b>".$file;

//Split each file row into an array value
$contents=explode("\n",$contents);

// You could try using $PHP_SELF instead of $_SERVER['PHP_SELF'], but if the page is only being accessed through IE you can just leave it.
echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'><table>";
$i=0;
foreach($contents as $item){
    $i+=1;
    $person=explode(",",$item);
    echo "
<tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>";
if ($person[1]=='on') {
$mark='CHECKED';
}
else {
$mark='';
}
echo "<input type='checkbox' name='".$i."c' value='$person[1]' $mark>In office?</td></tr>";
}
// Put the Update button under it all
echo "<tr><td><input type='submit' name='Submit' value='Update'></td></tr>";
echo "</table></form>";
}
?>
 
Here is the working code. Try this out, I've tried it and it works great for me. Had to change to radio buttons, but it should work the same for what you want. :)

PHP:
<?
$ff="/users/wensco/sites/inout/inoutdb.txt";

if (isset($_POST['update'])) {
    foreach ($_POST as $name=>$value) {
        
        if ($value=="on" || $value=="of") {
            $somecontent.= $value . "\n";
        }
		elseif ($value=='update' || $value=='Update') {
		}
        else {
            $somecontent.= $value . ",";
        }
    
    }
    $handle=fopen($ff, "w+");
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($ff)";
        exit;
    }
      fclose($ff);
}

display_file($ff);

//Functions------------------------
function display_file($file){
	//Put file into $contents
	$contents = file_get_contents($file);
	
	//Split each file row into an array value
	$contents=explode("\n",$contents);
	
	// You could try using $PHP_SELF instead of $_SERVER['PHP_SELF'], but if the page is only being accessed through IE you can just leave it.
	echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'><table>";
	$i=0;
	foreach($contents as $item){
		$person=explode(",",$item);
		if ($person[0]=="" || $person[1]==""){
		}
		else {
			echo "
			<tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>";
			if (substr($person[1],0,2)=='on') {
				$mark1='CHECKED';
				$mark2='';
			}
			else {
				$mark1=='';
				$mark2='CHECKED';
			}
			echo "<input type='radio' name='".$i."c' value='on' $mark1>In <input type='radio' name='".$i."c' value='of' $mark2>Out";
			//echo "<input type='checkbox' name='".$i."c' value='".substr($person[1],0,2)."' $mark>In office?</td></tr>";
		$i+=1;
		}
	}
		// Put the Update button under it all
		echo "<tr><td><input type='hidden' name='update' value='update'><input type='submit' name='Submit' value='Update'></td></tr>";
		echo "</table></form>";
}
?>
 
It works! How difficult would it be to make a small description field next to each name?
 
You mean like a textbox that would let you enter a reason why they're out or something along those lines? Wouldn't be too difficult, have to change some code to allow for the flatfile to store it correctly. Let me think about it and I'll post something.

Try this:

PHP:
<?
$ff="/users/wensco/sites/inout/inoutdb.txt";

if (isset($_POST['update'])) {
    foreach ($_POST as $name=>$value) {
        
        if ($value=="on" || $value=="of") {
            $somecontent.= $value . "\n";
        }
        elseif (substr($name,-1,1)=="t"){
        	$somecontent.=$value.",";
        }
        elseif ($value=='update' || $value=='Update') {
        }
        
        else {
            $somecontent.= $value . ",";
        }
    
    }
    $handle=fopen($ff, "w+");
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($ff)";
        exit;
    }
      fclose($ff);
}

display_file($ff);

//Functions------------------------
function display_file($file){
    //Put file into $contents
    $contents = file_get_contents($file);
    
    //Split each file row into an array value
    $contents=explode("\n",$contents);
    
    // You could try using $PHP_SELF instead of $_SERVER['PHP_SELF'], but if the page is only being accessed through IE you can just leave it.
    echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'><table>";
    $i=0;
    foreach($contents as $item){
        $person=explode(",",$item);
        if ($person[0]=="" || $person[2]==""){
        }
        else {
            echo "
            <tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>";
            if (substr($person[2],0,2)=='on') {
                $mark1='CHECKED';
                $mark2='';
            }
            else {
                $mark1=='';
                $mark2='CHECKED';
            }
            echo "<input type='radio' name='".$i."c' value='on' $mark1>In <input type='radio' name='".$i."c' value='of' $mark2>Out</td>
            <td><input type='text' name='".$i."t' value='".$person[1]."'></td></tr>";
            
        $i+=1;
        }
    }
        // Put the Update button under it all
        echo "<tr><td><input type='hidden' name='update' value='update'><input type='submit' name='Submit' value='Update'></td></tr>";
        echo "</table></form>";
}
?>

You need to change your flatfile to look like this:

Bob Smith,Vacation,of
Tom Jones,Medical Emergency,of
Betty Boop,,on
 
Rabbit, I hope I'm not annoying you with this too much. I REALLY appreciate your expertise on this subject.

It's not working quite right now though. It changes the database like this:

orginal database structure:

Bob Smith,Vacation,of

After you click update:

Bob Smith,of
Vacation,Update,

It also doesn't display anything in the browser except the update button after you click it.
 
Had a problem with the ordering, here's the fixed code:

PHP:
<?
$ff="/users/wensco/sites/inout/inoutdb.txt";

if (isset($_POST['update'])) {
    foreach ($_POST as $name=>$value) {
        
        if ($value=="on" || $value=="of") {
            $somecontent.= $value . "\n";
        }
        elseif ($value=='update' || $value=='Update') {
        }
        else {
            $somecontent.= $value . ",";
        }
    
    }
    $handle=fopen($ff, "w+");
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($ff)";
        exit;
    }
      fclose($ff);
}

display_file($ff);

//Functions------------------------
function display_file($file){
    //Put file into $contents
    $contents = file_get_contents($file);
    
    //Split each file row into an array value
    $contents=explode("\n",$contents);
    
    // You could try using $PHP_SELF instead of $_SERVER['PHP_SELF'], but if the page is only being accessed through IE you can just leave it.
    
    $i=0;
   echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'><table>";
   echo "<tr><td colspan=2><b>Employee</td><td><b>Reason</td><td><b>In/Out of Office</td></tr>";
	foreach ($contents as $item){
		$person=explode(",",$item);
       if ($person[0]=="" || $person[2]==""){
        }
        else {
            echo "
            <tr><td><input type='hidden' name='$i' value='$person[0]'>$person[0]</td><td>";
            if (substr($person[2],0,2)=='on') {
                $mark1='CHECKED';
                $mark2='';
            }
            else {
                $mark1=='';
                $mark2='CHECKED';
            }
            echo "<td><input type='text' name='".$i."t' value='".$person[1]."'></td>";
			echo "<td><input type='radio' name='".$i."c' value='on' $mark1>In <input type='radio' name='".$i."c' value='of' $mark2>Out</td></tr>";
        $i+=1;
        }
    }
        // Put the Update button under it all
        echo "<tr><td><input type='hidden' name='update' value='update'><input type='submit' name='Submit' value='Update'></td></tr>";
				
        echo "</table></form>";
}
?>
 
It works great!

I have another question.

Is there a way to make it update just the record(s) that are being updated? Is this possible with a flat file?
 
sarbz said:
Is there a way to make it update just the record(s) that are being updated? Is this possible with a flat file?

I think it is possible, but it would take a lot of search code to find what was being updated, as well as having the PHP know which fields changed. Someone who knows flatfiles better than I would probably be able to help you out.
 
rabbitt said:
I think it is possible, but it would take a lot of search code to find what was being updated, as well as having the PHP know which fields changed. Someone who knows flatfiles better than I would probably be able to help you out.

ok. I think it will work the way it is right now. I'm going to create a seperate page to make changes and one to display them. That way, people spend less time on the editing page, and we won't run into as many problems with multiple users.

Thanks so much for all your help rabbit.....like I said, if you ever need anything that has to do with graphic design, let me know!
 
sarbz

Are/were you running this PHP code on a web server or on your intranet on a LAN/WAN server?

Is this application access via web browser?
So the host machine must have PHP installed I'm sure?

Any assistance greatly appreciated
 
sarbz has probably moved on during the 7.5 years since his/her last post in this thread.
 
Maybe an experienced coder can reply as to how to best put this code to use.

On a web server, local Intranet server, stand-alone pc, etc.

Any assistance appreciated.
 
Necro bumping a 7.5 year old thread is generally frowned on. Start a new thread for a new topic, please.
 
Status
Not open for further replies.
Back
Top