E-com DevBlog Spider-ball-vacuum

2Mar/110

Handy PHP Debugging Tools

I'm sure most folks who read this blog are already set in their ways of debugging things. And many probably already have a few custom built methods they use to look at raw data on the fly. However for those few who might be in need or are possibly looking for another approach this post is for you ;-) .

I used to rely solely on a step through debugger to help me debug code, and don't get me wrong stepping through code is a great way to see what the heck is going on (especially in code you are unfamiliar with and was written by monkeys), however for the last year or so I've been learning new techniques and tricks for getting my raw data visible without having to step through the code.

My all time favorite method is

echo "<pre>".print_r($array, true)."<pre>";

but there are times that is not good enough. like when you are trying to see data run in shell_exec() or when the page changes before you can see the echo. So to combat this I've written a method that drills through an array or an object and spits the data out in a file in a format similar to the aforementioned. I call it pre()

observe

function pre($array, $mode = 'w', $indent = "\r\n") {
	if(is_array($array) || is_object($array)) {
		$filename = 'C:\logs\dnate_pre.log';
		$handle = fopen($filename, $mode);
		fwrite($handle, getArrayKeysAndValues($array, $indent));
		fclose($handle);
	} else {
		writeToFile($array, $indent);
	}
}

and if you're wondering what the getArrayKeysAndValues() method does, here it is.

function getArrayKeysAndValues($array, $indent = "\r\n\t") {
        $txt = '';
	if( is_object($array) ) {
		$array = get_object_vars($array);
	}
	if(is_array($array)) {
		$txt .= " => Array (";
		foreach($array as $k => $v) {
			$txt .= $indent."\t[".$k."]";
			if( is_array($v) || is_object($v) ) {
				$txt .= getArrayKeysAndValues($v, $indent."\t");
			} else {
				$txt .= " => ".$v;
			}
		}
		$txt .= $indent.")";
	}
	return $txt;
}

Occasionally I've run into a limit of how many times getArrayKeysAndValues() can call itself (100) so in cases like that I've also got a method I call kill() that kills the script and spits the output out on the browser. Not very subtle but gets the job done.

Obviously you'll have to have the log files in the proper directory on your machine. Then just include the file and call the desired method.

The rest are in the download, feel free to offer suggestions or comments.

Debug Tools

Print This Post Print This Post
Tagged as: , No Comments
1Sep/108

Zend Framework vs. CodeIgniter

I hope nobody minds but this is going to be more of a rant than an actual performance comparison of the two frameworks. First of all I started using CodeIgniter almost two years ago, and while it was a bit of a thorn at first it was never super difficult to figure out how to do stuff. The documentation was easy to understand and full of examples of what to do, plus things worked when I did them. As a result I've used CodeIgniter on several projects since.

Recently at my current job the powers that be (hereafter referred to as "they") decided to go from a progressive coding approach to a more MVC type approach using a framework (either custom made or already built). I'd like to point out that I'm all for using a framework especially on the applications we've got right now. I did suggest CodeIgniter as the framework of choice but my suggestion was received with disgust, so I kept my mouth shut...big mistake.

After attempting to build their own MVC Framework "they" decided to go with Zend Server Community Edition. Now I didn't (and still don't, which is sad) know a lot about the Zend Framework so I was somewhat excited to see it in action, especially since my experience with CI has been so great. And I guess maybe that's one of the reasons I'm posting today.

For anyone out there wondering if Zend is a good match for you, STOP!
For anyone out there who recently implemented the Zend Framework, WHY? It's not to late to turn back.

I've been working with the Zend Framework for about two weeks now, and I don't expect to know everything there is to know about Zend but I do expect to know SOMETHING. Let's just take a trip down memory/nightmare lane.

Please excuse me if I can't remember all the details, I've tried so hard to block some of this from ever resurfacing.

My first task was to figure out how to ajax with zend, seems simple enough right? Shoot zend even created a helper that loads the (almost) latest jQuery libraries for you so you never have to worry about being up to date. Not only that but zend has a way to build your Ajax request so you don't have to know how to do that yourself either. What they don't tell you is that in order for the jquery libraries to be loaded on your page, you DO have to use at least one of their custom php jquery building tools (in other words, you can't make all your jquery by yourself), otherwise the libs just wont load. Instead they let you beat your head against your desk for hours and hours until you finally un-comment a test jquery builder you had previously used only to find out that was the missing piece.

Next our whole team has been tasked with creating a new application using zend, it's been over a week now, I still feel like I don't know anything. Now you might be saying to yourself, "did you try google dipstick?" Yup sure did, and as google does it pulled up all kinds of links. Almost always the top 5 go strait to the official zend documentation which is by far the worst documentation I've ever seen about anything. It's like when someone has a secret and they know you want to know, but they know that if they tell you then they'll lose the leverage they have on you, so they just hold on to it with that smirk on their face and never tell you anything. There's all kinds of words but they don't say much and the examples are horrific at best. One example I was looking at had a variable in it, and they'd conveniently cut out the part where they actually initialize that variable so you have no idea what values it is supposed to hold. Turns out it was an array, but I still don't know if it was a named array or if it can simply be numbered. I actually got a little excited when I stumbled onto the Programmers reference guide, I thought "yes if anything will help this is it", I was wrong to think that and I apologize.

Now here's the kicker, I do need to backtrack a bit. When you install zend there is a GUI that is installed to help you manage how the server works, I've recently lost this GUI, by lost I mean I know where it's supposed to be and the files are all there but the GUI doesn't show up when i put the correct URL in my browser. So I started researching this, only to find that a certain part of the server called lighttpd is not running, the GUI is the only thing that requires lighttpd. So i started googling lighttpd is not running and got a fair amount of hits on that one. None of which helped. Finally I decided to create an account with the zend forums and humbly ask the zend folks themselves what to do about my problem.

To my dismay, when I logged in I found a little counter at the bottom of the page that said "Users browsing this forum: disgruntled and 0 guests" (if you haven't guessed, I'm 'disgruntled'). You have got to be kidding me, I'm the only one on this forum? Now to be fair it was after 5:00 on the east coast so I imagine the 3 people using zend in that part of the world have gone home.

how many people on this forum?

how many people on this forum?

Needless to say, I'm a little disappointed, awe who am I kidding? I'm downright furious.

On the flip side, if anyone reading this has any idea on where I can get my hands on some useful examples of zend at work. Please leave them in the comments.

Print This Post Print This Post
14Jul/106

Use PHP to Build an Array of ‘Times of Day’

It's been a long time since I posted so I find it ironic that "time" is what brings me here today. Ok that was pretty corny. You'd think this would be something that would be pretty easy to find on the web, I was a little shocked there wasn't some post about it in the PHP manual but there wasn't so here it is. I was looking for a quick way to build an array of times of day starting with midnight and increasing every half hour until 11:30 PM (or 23:30 for you G.I. Joe's). PHP has an abundance of time functions that are all very useful in their own unique way. After trying various different ones i finally came up with the following.

//build array of times.
$times = array();
$time = strtotime("00:00:00");
$times["00:00:00"] = date("g:i a",$time);
for($i = 1;$i < 48;$i++) {
	$time = strtotime("+ 30 minutes",$time);
	$key = date("H:i:s",$time);
	$times[$key] = date("g:i a",$time);
}

To break it down, I set $time to start at Midnight or 00 Hours and then set my first $times array member to the lamens "12:00 am" using the date() function. Since there are 24 hours in a day I know there are 48 half hours in a day so I set my loop to run 48 times.

Lets break down the loop:

$time = strtotime("+ 30 minutes",$time);

This will add 30 minutes to the $time variable each time the loop runs.

$key = date("H:i:s",$time);

This sets $key to the military notation of the current $time (the why will become more obvious later).

Finally I set my next $times array member with:

$times[$key] = date("g:i a",$time);

Again the value of this member is set to the ordinary am/pm notation.

If you were to look at an array member in plain text it would look something like this:

$times["19:00:00"] = "7:00 pm";

Pretty simple, now just to display it. For my purposes I chose a select box.

<select class="required" name="CloseTime">
	<option value="">--Select--</option>
	<?php 
	foreach($times as $key => $value) {
	?>
	<option value='<?php echo $key; ?>'<?php if(isset($CloseTime) && $CloseTime == $key) echo " selected='selected'"?>><?php echo $value; ?></option>
	<?php
	}
	?>
</select>

Since I set up my $key as military notation I am able to use $key as the value of the <option>. I did this so I can easily put that value in the DB as a TIME and not have to worry about stripping the " am" and " pm". Alternatively since $value was set up with am/pm notation it displays nicely to the user as 7:00 PM rather than 19:00 thus allowing regular Joe to keep his fingers in his nose rather than use them to figure out what time that is.

Print This Post Print This Post
26Feb/100

Handy PHP trick I learned today.

Nice to see the Dev blog is still here, and that I can remember my login. Here is a useful debugging trick I learned for PHP, hopefully it's not in one of Phil's link posts.

echo '<pre>'.print_r($app,true).'</pre>';

this bad boy will give you all of the values of an array, variable etc.

more to come soon.

Print This Post Print This Post
Tagged as: , , No Comments
3Aug/093

Summer 2009 LinkPost Extravaganza!

Whoa, what happened? I'll tell you what happened, I blinked and it was August! But fear not! I have kept the links that I was going to post and have compiled them into what I like to call the Summer 2009 LinkPost Extravaganza! Cheesy? Yes, a little absurd? Absolutely, but hey at least you're getting the links right?

This Post features plenty of images (icons, textures, etc.), a large selection of jQuery goodness, some php, fonts, and plenty of tools, resources, and tutorials. I even threw in a few links for our growing mac-user community (yeah, you can't see it, but I'm patting myself on the back).

Enough drivel, time to give the peoples what they want, the links!

Print This Post Print This Post
18Feb/091

&& || and or

So it turns out you learn stuff when you read and I've noticed that you remember stuff when you write it down, so since this is the best tech blog ever I'm writing it down here so you all can look at it and learn it (it's gonna be up to you to write it down somewhere else if you wanna remember it). I'm not sure about JSTL or other languages but I'd wager they are the same. if you were wondering which is better to use && vs. "and" or || vs. "or" in your expressions it pretty much all depends because they have different precedences (is that a word? huh spell check likes it.). First of all && has a higher precedence than || (at least in php) and "and" has a higher precedence than "or" but || has a higher precedence than "and", thus || and && both have higher precedences than "or" and && also has a higher precedence than "and". Confused? k here is the breakdown, higher precedence listed first:

&&
||
and
xor //I know we didn't cover this but it's in there... trust me.
or

as a side note "xor" means one or the other is true but not both... I can see how that could be handy.

Print This Post Print This Post
18Feb/090

$i++ || ++$i

So for some time now I've wondered what the underlying difference is in writing a variable increment as $i++ (aka post-increment) vs. writing it as ++$i (aka pre-increment). I've seen it done both ways and even tried it both ways and have been unable to determine a difference.  Recently I was reading a php manual (thus the php code used in this post) and VIOLA! I stumbled into the profound answer. It is subtle yet significant. if you use the post-increment or $i++ it does just that, increments the variable after the expression that includes said variable has run. And as you may have guessed the pre-increment or ++$i does just the opposite. The book I was reading used the following scenario to explain:

$x = 3;
$x++ < 4;

This would evaluate to true as 3 is less than 4 and the expression is run before the variable $x is incremented. If you run the same using the pre-increment

$x = 3;
++$x < 4;

It would evaluate as false because the variable is incremented before the expression is run and 4 is not less than 4.

So unless I'm mistaking the following code:

for ($i = 1;$i < 8; $i++){ 
echo $i;
}

Should run the same as

for ($i = 0; $i < 8; ++$i){
echo $i;
}

I guess in that scenario it's just boils down to personal preference.

Print This Post Print This Post
Tagged as: , No Comments
11Jun/083

find your .ini

Sometimes I dabble in php a little so when I run onto something I wanna remember I like to tuck it away somewhere where I'll remember where it is. This handy little snippet will tell you where your php.ini file is loading from. This is especially useful if you are using xampp/wampp/lampp since they tend to have multiple php.ini files and it's anyones guess which one they are using (at least xampp does).

<?php
/*****CHECK TO SEE WHAT PHP.INI FILE IS LOADING*******/
$path = php_ini_loaded_file();
 
if ($path) {
    echo 'path to php.ini: ' . $path;
} else {
    echo 'A php.ini file is not loaded';
}
 
?>

Here's a quick game, who can tell me how many times the word "where" was used in this post (including "somewhere")?

Print This Post Print This Post
29May/080

Automatic FTP

A few weeks ago I was looking for a way to automate downloading thousands of files from an ftp site, I figured using php would be sufficient since that is what the site I was working with used. I tried google and some of the better php sites but to no avail. So I found some code that was similar to what I wanted and tweaked (and by tweaked i mean almost completely rewrote) it to suit my needs. Now I'm not claiming to be a php genius (or any kind of genius for that matter) but I'm pretty impressed with what I came up with. In a nutshell what this code does is open up a mysql session and an ftp session to a remote server, then it runs a query in the mysql database to create an array for php to use. Then it loops through each item in the array and checks with the remote ftp server for the file, updates to that file (time based), and if the file matches a certain size to be ignored.
This turned out to be quite handy because by the time I was done I could modify the code slightly and search in different locations on the server as well as store the files on a different spot on my local machine.
To test this I ran a 13k item array through this script and it was quite flawless...took about 3.5 hours to run but that's still way faster than downloading by hand even if you include the time it took to write the script.

So I decided to share this script since i couldn't find one myself, feel free to modify it in any way, if you find a way to make it better, more efficient or even hyperthread the downloads I'd like to know so please send an email with the code. Just make sure something like ftpgrabber is in the subject line...otherwise you'll get deleted as spam er somethin'.

Download ftpgrabber

Print This Post Print This Post