Page 1 of 2

Simple search form to get status from external file

Posted: Mon Feb 06, 2012 6:12 am
by Colucas
Hello people, first time posting on this forum! :D

Am just starting to learn php, so i don't know a lot besides the most basic functions.

Working on a new project, I need to be able to get a delivery-status after a user inputs a specific ID/serial on a basic search form.

The thing is, the statuses will be updated through a text file (preferably a text-file with table-layout). As the user searches for example, FFFX-FFFG-666, it will get 0, 1, 2 or 3 values- then each one will make appear a different div (in order to style each one individually through css).

Looks pretty simple, but i have no clue how to start on it. Any help?! :)

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 6:37 am
by Celauran
I guess the obvious question is why you'd use a text file instead of a database. That aside, you could open the file with fopen(), then loop through it line by line with fgets() looking for a match.

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 7:50 am
by Colucas
We chose a text file, or similar, cause it's easier to upload and replace once a day, simply via ftp.

the idea is to have two columns, one for the ID, other for the value, for example FFFX-FFFG-666 - 0.

The user would insert the ID on the text input, such as

<form name="input" action="html_form_action.asp" method="get">
ID: <input type="text" name="id" />
<input type="submit" value="Submit" />
</form>

After submitting, it would open a new webpage with a new div, such as <div id="id_0"></div>.

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 10:37 am
by Colucas
anyone? :(

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 10:41 am
by Celauran
What's your question? Did you try what I suggested above? Is it not working? What errors are you encountering?

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 11:20 am
by Colucas
Don't really know how to start.. As i said i don't know much about php.

Started with something like:

<?php
$handle = fopen("status.txt", "r");
?>

status.txt contains an ID and a specific value, from 0 to 3, like:

XYZ0123ABC 1
YYY0123ABD 0
XYZ0933DBA 2

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 11:31 am
by Celauran
Something like this should get you started

Code: Select all

if (isset($_GET['id']))
{
    // Strip out the hyphens, since they don't seem to appear in status.txt
    $id = str_replace('-', '', $_GET['id']);

    $handle = fopen('status.txt', 'r');
    while (($line = fgets($handle, 1024)) != FALSE)
    {
        if (preg_match('/^' . $id . '/', $line))
        {
            echo "Found match: {$line}<br />";
        }
    }
    fclose($handle);
}

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 3:28 pm
by Colucas
Hello again Celauran, thanks for helping!

So far, what we have is:

Code: Select all

<form action="" method="post"><input name="serial_box_div" type="text" /> <input name="" type="button" value="enviar"/></form>
<?
$serial = $_POST['serial_box_div']; //loads the var value 
$file = @fopen("assitenciatecnica.txt", "r"); //read file
if($file) { //validate file
$serial_exi = 0; //flag to validate serial.
while(!feof($file))
{
$line = fgets($file, 1024); //read line
if(!strpos($line, $serial)) { //look for the serial
$div_open = substr($serial,-1); //div value
echo "$serial existe e deve abrir o div $div_open ";
$serial_exi = 1;
}
//validate serial
if($serial_exi)
echo "Serial $serial found";
else
echo "Serial $serial not found";
}
}
?>
the .txt file is as follows:

Code: Select all

12345 0;
Still not working.. any help? :/

edit: the objective is to return a div with the id of the number after the serial, in this case, 0. Knowing that serials have 5 digits.

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 3:38 pm
by Celauran

Code: Select all

<input name="" type="button" value="enviar"/>
You want type="submit" here.

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 3:41 pm
by Colucas
Changed it, still not working! :banghead:

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 3:43 pm
by Celauran
Worked for me. I noticed you're using short opening tags. Could it be that your server doesn't accept them?

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 3:59 pm
by Colucas
Hmm don't know, I'm running MAMP.. Now downloaded to my server and still nothing, there is no div being created nor even echos showing.

http://joaocolucas.com/teste/assistenciatecnica.php

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 4:25 pm
by Celauran

Code: Select all

<?php

if (!empty($_POST))
{
    $serial = $_POST['serial_box_div']; //loads the var value
    $file = @fopen("assitenciatecnica.txt", "r"); //read file
    if ($file)
    { //validate file
        while (!feof($file))
        {
            $line = fgets($file, 1024); //read line
            if (preg_match('/^' . $serial . '/', $line))
            {
                echo "Found serial: {$serial}<br />";
            }
        }
    }
}

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        <title>Debug</title>
    </head>
    <body>
        <form action="" method="post">
            <input name="serial_box_div" type="text" />
            <input name="" type="submit" value="enviar"/>
        </form>
    </body>
</html>
Works fine for me

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 5:08 pm
by Colucas
Hmm tks again, now it worked! But need just two things.. First, the serials are 5-number only.. If we just type "1", it returns every serial that has 1 in it. Second, is that what i'm trying to retrieve is the number in front of the serial, not the serial itself, as in:

12345 0;
12344 1;
12343 2;
12342 3;

The numbers i seek when searching for "12345", "12344", "12343" and "12342" are "0", "1", "2" and "3", respectively. Sorry for all the mess up, I'm really lost ^^

About the 5-number minimum/maximum, it would be reliable to do via javascript?

Thanks again!

Re: Simple search form to get status from external file

Posted: Mon Feb 06, 2012 5:10 pm
by Colucas
or maybe use something like

Code: Select all

function validStrLen($str, $min, $max){
    $len = strlen($str);
    if($len < $min){
        return "Field Name is too short, minimum is $min characters ($max max)".
    }
    elseif($len > $max){
        return "Field Name is too long, maximum is $max characters ($min min).";
    }
    return TRUE;
}