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
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
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
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 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.
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.#!/bin/sh clear cat /path/to/phonelist.txt | grep -i $1
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? if( stristr($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.
if(strlen($s_str)>0)
{
//do the search
}