2009-11-02
For a while now, I've been keeping track of phone number by enter them into a plain text file on my laptop and searching for numbers with a simple shell script named phone
#!/bin/sh clear cat /path/to/phonelist.txt | grep -i $1
This is fine and dandy if I'm actually using my laptop, but I needed a way to access the phone numbers from all of my computers. If you are playing Buzzword Bingo, put a checkmark on "cloud". There is a low-power computer in my home network that will perform the task of "cloud based phone number listing" wonderfully.

Since "cloud" is a Web 2.0 buzzword, I figured I should make the user interface of my phone number finder behave in an AJAXy way, but without Javascript or XML. ( mark off Web 2.0,AJAX, Javascript, and XML )

Here is the HTML of the interface
<html>     <head>         <title>phone</title>         <style type="text/css">             #result_frame             {                 border:0px;                 width:50%;                 height:50%             }         </style>     </head>     <body>         <form name='p' action='phone.php' method='post' target='result_frame'>             <input type="text" name="search_string">             <input type="submit" value="find">         </form>         <iframe name="result_frame" id="result_frame">         </iframe>     </body> </html>

A simple form makes a request and the results are displayed in a targetted inline frame. Thus, there is not a full page refresh when the user selects a different name to search for. Oh shiney!

On to the searching code
<?php     //get the search string from the POST data     $s_str $_POST['search_string'];     //escape the string     $esc_str escapeshellcmd($s_str);     //where is the phone file?     $phone_file "phonelist.txt";     $results "";     //read the file into an array     $file_lines file($phone_file);     //loop through the array of lines     foreach($file_lines as $line)     {         //is the search string in the line?         ifstristr($line,$s_str) )         {             //add the line to the results.             $results.=$line;         }     }     //are there results?     if(!$results)     {         //if not, let the user know that there were no matches         $results "no matches found for \"$s_str\"";     }     //create some basic HTML to display to the viewer     echo "<html><head><title>Results for $s_str</title></head>";     echo "<body><pre>";     print_r($results);     echo "</pre></body></html>"; ?>

This is a very simple PHP script to search for matching lines in the text file.

A working example of this code is viewable at http://www.jezra.net/code_examples/phonenumbers

Things that could have been done different:
  • the numbers could be put into a database
  • a regular expression could search the text file

The key here was development time. Comparitive to the actual development time, creating a database, adding the numbers, and writing the code to retrieve the numbers would add a significant amount of time. Unfortunately, I didn't think of using a REGEX until after I had written the code, and since the code worked as expected, I felt no need to change it.

Now quit reading, and call a friend.
Comments
2009-11-05 digi_morass:
handy Web 2.0 tutorial, Jezra. may play with this in the future. You might want to handle the error when the user clicks find on empty search query, though.
2009-11-05 jezra:
HA! Thanks digi. The example PHP code as been edited to only search the text file if the length of the search string is greater than zero.

if(strlen($s_str)>0)
{
//do the search
}
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
6 plus 7
Answer:
required
  • Tags:
  • PHP
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008