I’ve been writing PHP code for years now. I’ve written complete systems that I’ve forgotten I’ve written. I generally know what I’m doing.

But, sometimes I’m writing something and I need to make sure some kind of complicated logic will work correctly. Or I can’t remember exactly how a certain function will behave with certain inputs. Or I need to run some kind of one-off report for someone. How do I do all of those things without creating tons of files for each one?

Create a test.php file. In that file, include your common file so you have database access and all your standard classes. You can optionally include visuals or whatever else you need to perform your testing. Then go ahead and test your one-line items, or write one-time use reports, or test some concepts.

What I like to do is write my code, then put an “exit” at the end of it. I leave it on the development server I work on all the time so it’s always available. The next time I need to test some piece of code, I add my code above my previous test code and add another “exit” at the end of my current test code. All of my various tests and one-time reports are in this file and it’s really easy to grab examples from previous work or see how I did something later. It’s like a functional scratch pad.

Recently the file got so big it was taking a couple seconds to upload and download it to the server. Because I also have a /test/ directory, I duplicated my test file, named it test_archive.php, and then deleted everything except the required common includes out of my test.php file and started over.

I love having my test.php file. It lets me take complicated systems and test individual little components to verify they’re working without needing to run large complicated tests on the entire new set of code I might be writing. It also lets me do real time one-off reporting as needed without setting up and formalizing actual report files.

August 28th, 2013

Posted In: Shortcuts

Leave a Comment

One of my favorite tricks lately is storing an array of data in a serialized format and writing it to a database.  This is really handy when you have an arbitrary set of information that needs to be stored that doesn’t really fit anywhere else and does not need to be searchable or sortable.

An example I had recently required me to store whether or not 4 checkboxes were checked or not on a record.  I could have created 4 additional fields or a related table where it would create 4 additional records per original object, or I could just store this as an array and serialize it and be done with it.  Since I already had a “generic_data” table, I just added this to that table and in a couple minutes I was done.  Awesome.

How does it work?  Well, something like this (showing ideas here, not full code chunks):

if($command=='view') {
	$raw = $obj->getSerializedData(); // read data from the database
	if($raw!='') {
		$myArray = unserialize($raw);
	}
	if(!is_array($myArray)) {
		$myArray = array();
	}
	// do whatever display is needed with $myArray
}
if($command=='save') {
	$myArray = $_REQUEST['x']; // assuming $_REQUEST['x'] is an array
	$obj->setSerializedData(serialize($myArray)); // write to the database
	$obj->Save();
}

June 7th, 2013

Posted In: Shortcuts, Syntax

Leave a Comment

I write “report” pages all the time. These pages usually consist of some kind of filterable elements at the top with the results below. When the user fills out a filter element such as “status”, they expect that value to be retained when the page refreshes. And if he/she is going to proceed to the second “page” of results, that filter should still remain in place.

Pasted below is little code that helps maintain “sticky” filters, swap out bits and pieces as necessary for your reporting. This is obviously a really simple example that doesn’t have a lot of the UI elegance you’d put into a real report, but I think the point is made that storing the filtered elements into a session array is the easiest way to make them “stick” between page loads.

<?
$sesParams = &$_SESSION['session_reportName'];
if(!is_array($sesParams)) {
	$sesParams = array();
}
if(isset($_REQUEST['newParams']) || isset($_REQUEST['clear'])) {
	$sesParams = array();
	$fieldsToGet = array(	'date_from',
				'date_to',
				'status',
				);
	foreach($fieldsToGet AS $fieldName) {
		$sesParams[$fieldName] = $_REQUEST[$fieldName];
	}
}
?>
...

<form action="<?=$_SERVER['PHP_SELF']?>" method='post'>
	<input type='hidden' name='newParams' value='true'>
	<table class='body10'>
		<tr>
			<td>
				From: 
				<input type='text' name='date_from' value="<?=$sesParams['date_from']?>">
			</td>
			<td>
				To:
				<input type='text' name='date_to' value="<?=$sesParams['date_to']?>">
			</td>
			<td>
				Status:
				<input type='text' name='status' value="<?=$sesParams['status']?>">
			</td>
			<td>
				<input type='submit' value='Filter' class='input10'>
				<input type='button' value='Clear' class='input10' onclick="window.location='<?=$_SERVER['PHP_SELF']?>?clear'">
			</td>
		</tr>
	</table>
</form>

May 3rd, 2013

Posted In: Shortcuts, User Interface

Leave a Comment

So I have a little function I use all-the-time.

function encodeSQL($data) {
	return mysql_real_escape_string($data);
}

This little guy gets used whenever and wherever I need to be sure that whatever I’m using in my sql queries is safe.  Is it really all that helpful?  No.  Does it save me from writing mysql_real_escape_string a zillion times?  Yes.  Does it make my code writing a little easier, a little faster?  Yes.

My point here is that if you have things that you do a lot, go ahead and write little functions for them.  Then, re-use those functions across projects if they were really well written.  Just remember that whatever you write should be easily understandable.

March 29th, 2013

Posted In: Shortcuts

Leave a Comment