Example sources from systhread.net

From: systhread.net  read times: 129


Provided by yangyi at 2009-07-04 20:31:55


Some small tricks I have done, working with development tools and interesting code I have written. It is not a tutorial site as much just an interesting place to shuffle around. I do have a tiny software collection available (all of which are an order of magnitude larger than anything here).

Contents

Some Small PHP Tidbits

This site really only uses a little PHP at the moment and in three ways; to include common files/data, a feedback form, and a search form. Including files is pretty straightforward, but I found two articles on a feedback and search form thatI thought would be nice to put in the examples.

Simple Feedback Form in PHP

My form is similar to this one:

<?php
if ($_POST) {
	$subject	= $_POST['subject'];
	$email		= $_POST['email'];
	$comments	= $_POST['comments'];

	$message = Message from $name ($email)\n\nComments:\n\n$comments;
	mail(foo@bu.com, Feedback, $message);

	echo "<h2>Thank you!</h2>\n";
	echo "<p>Your feedback has been sent.</p>\n";
} else {
?>

<h2>Send Feedback</h2>
<form method = post action=<?= $PHP_SELF ?>>

Email Address:<br />
	<input type=text name=email /> <br />

Subject:<br />
	<input type=text name=subject /> <br />

<br />Subject:<br />

<textarea name=comments rows=5 cols=50> 
	</textarea><br /><br />

	<input type=submit value=Send Feedback />

Grep Search

The search PHP script while clever has one slight drawback, it is using a non portable grep syntax. Generally speaking, on any Open Source OS grep -r is supported. I found this method right here:

<Search>
<form action=<?=$PHP_SELF;?gt; method=post
	<input type=text name=searchstr
		value=<?php echo $searchstr; ?> size=20 maxlength=30 />

	<input type=submit value=Search />
</form>

<?php
	$cmdstr = grep -ir $searchstr dir1/* dir2/*.html about/*;
	$fp = popen( $cmdstr, r );
	$myresult = array();

	echo (<h3>Search Results</h3>\n);

	while( $buffer = fgetss ( $fp, 4096 )) {
		list( $fname, $fline ) = split( :, $buffer, 2 );
		if ( !defined( $myresult[$fname] ))
			$myresult[$fname] = $fline;
		}

		if ( count( $myresult )) {
			echo <ul>\n;
			while ( list( $fname, $fline ) = each( $myresult))
				echo <li><a href=\$fname\>$fname</a> : $fline </li>\n;
	} else {
		echo <p>Sorry. Search on <b>$searchstr</b> returned no results.</p>\n;
	}
	pclose( $fp );
}
?>

Note how the feedback uses a reverse logic, if we have not done a post then print the form yet the search just replaces part of the page on the fly.

......

Please access the below link to view the full content.

Original link: http://systhread.net/coding/exsr...