Codeular

RSS

PHP: Comparing parameters from 2 separate urls

During one of many late night coding sessions I was working on a project were I needed to find out if all the parameters from one url were present and had matching values to another url.  My first instinct was to try a regular expression but I quickly realized that it wouldnt work since the parameters could be placed in any order within the query string.  It did take me about 15 minutes to come to this conclusion so when I say ‘quickly’ I meant it was quick for 3:30 am.  Here is what I eventually did came up with:

$qs1 = “a=123&b=hello&c=world”;
$qs2 = “c=world&a=123&d=again&b=hello”;

parse_str($qs1, $qsParams1);
parse_str($qs2, $qsParams2);

$matchingParams = array_intersect_assoc($qsParams1, $qsParams2);

if (count($matchingParams) == count($qsParams1)) {
$allParamsMatch = true;
} else {
$allParamsMatch = false;

So in this example the match ends up being true, even though there are more parameters in the second string and they are out of order it still contained all the values from the first string.

nothing important: fellow developers: help a brother out

traviskuhl:

i need your help testing and improving a packaging and deployment script i’ve been working on for almost a year. it’s called drib and if i do say so myself it’s pretty awesome. i’ve been using it on a bunch of projects in the past year (teamcoco.com, dailyd.com, cdnimag.es, waroftruth.com) and it makes the process of developing and deploying sites a bit more manageable; especially if you’re working in a multi-tiered, multi-server development stack.

clientsfromhell:

Client: You told me, you’ve changed XY but you didn’t.

Me: Yes, I have. In order to see the changes you have to empty your cache and reload the site.

Client: Empty what? How do I do that?

Me: (explaining the way through the menus…)

Client: Stop. I can’t explain this to all my visitors. This is too complicated and I never had to do this before on any website. You have to make it work without this empty-cache-and-reload-thing.

Code Review

I came across this code a couple months ago. Can you see any problems with it?

var randNum = “”;

for(i=0; i < 11; i++) randNum += String(Math.floor(Math.random()*10));

May 4
codecanyon:

Building a 5 Star Rating System with jQuery, AJAX and PHP
“In this tutorial, you’ll learn how to build a rating system with AJAX, PHP, and jQuery. Votes will be recorded and updated in real-time with the magic of AJAX, and we’ll also leverage the power of PHP so that you don’t even need a database!”

codecanyon:

Building a 5 Star Rating System with jQuery, AJAX and PHP

“In this tutorial, you’ll learn how to build a rating system with AJAX, PHP, and jQuery. Votes will be recorded and updated in real-time with the magic of AJAX, and we’ll also leverage the power of PHP so that you don’t even need a database!”

Clients From Hell

codecanyon:

Scene: Trendy restaurant in NYC, we are sitting with our prospective client who is with her “marketing consultant” at a dinner meeting. Prospective client has already downed two drinks in rapid fashion.

Client: “Our product is going to become ubiquitous in the marketplace. We need millions of customers.”

Us: “That is great, how do you plan to achieve this goal?”

Marketing Consultant: “We are going to use the power of the web.”

Client: “Yes we want you to help us harness the ‘power of the web’ to get this product out there.”

Us: “Can you be more specific? There are many tactics…….”

Client: “The power of the web is it.”

Us: The “power of the web??”

Drupal Modules: Controlling hook execution

If you have ever had to work with Drupal you know it can be a very emotional journey,  you will love it one day and hate it the next and trust me this cycle is endless.  The truth is that it is a powerful framework which you can leverage to do quite a few interesting things.  It also has a ton of documentation to go along with some of the best community contributions I have seen for any open-source project.

In the project I was working on we had to create a handful of our own customized Drupal modules.   One of the things that stands out in this module system is it is based on the concept of hooks and the issue I was having was, how do I control when the hooks in my module run in relation to the rest of the system?  Basically I needed to be sure my hook ran either before or after another modules hook.

The answer ended up being really simple:


function modulename_install() {

     db_query("UPDATE {system} SET weight = 1 WHERE name = 'modulename'");

}

Using the install hook for your custom module you run a query to set the weight.  Drupal uses the weight and name of modules to set the priority in which module hooks are called, the lower the number the earlier the it will run. All of the core modules have a weight of 0 but if you need your hooks to run before the core modules you can just use a negative number.