PHP Foreach and Reference Variables

Here’s a crazy gotcha.  I was playing with references when I realized that I was getting the wrong values.

Here’re the values that I used:
$valuearray = array(‘%CTRL%’, ‘%2%’);

Here’s my original loop:

 

And here’s what the print_r($params) gave me.

Do you see the problem yet?

In a nutshell, I was trying to load references of each element in $valuearray into $params.  It wasn’t working too well because I was shoving the reference of $val itself into $params.  Whatever $val last points at is what’s going to show up for all elements in the array.  Interesting!  But ultimately not what I was aiming for.

http://www.php.net/manual/en/control-structures.foreach.php

The above link had part of the solution.  The next step of the debugging process would be this loop (I’ve highlighted the change in cyan):

And here’s what the print_r($params) gave me.

That’s correct.  But we’re not out of the woods yet.  If you’re trying to shove this into a mysql query that needs references, it should give you an error that a value was received when it was expecting a reference.

Here’s the final iteration (I’ve highlighted the change in cyan):

And now I’ve fulfilled my original goal, load references to an array’s elements into another array.

Enclosing MySQL Columns with Grave Accents

Now here’s the reason why I created this blog.  It’s an uncommon solution.  Uncommon because I had a hard time finding a solution by using google and ended up altering the method until it worked in a way that made some sense.

Let me start by saying that there are some strange nuances with SQL columns when you surround them with grave accents.

The problem?  I was  trying to enclose the column names with grave accents to keep my code readable and reliable even though I was using JOINS.

Let me start with what did not work…  `Table_Name.Column_Name`.  I immediately got a “unknown column in field list” error.

After some poking around, this is what did work Table_Name.`Column_Name`.  Note how only the column name is surrounded by the grave accents.  This style works, leading me to believe that Table_Name can be enclosed by a separate set of grave accents.

MySQL special characters

I was playing around with a certain query only to come up with 0 results every time.  After looking around, I found that, in MySQL, the samples that actually tried to surround the table column names used the the grave symbol (`), not to be confused by the single quotes (‘).  The grave symbol is found on the same button as the tilde (~).

For example, if there was a table column called Name

Using ‘Name’ in your query would probably create 0 results.

Using `Name` in your query would probably create the desired number of results.

http://stackoverflow.com/questions/7857278/what-is-the-meaning-of-grave-accent-aka-backtick-quoted-characters-in-mysql

Templating Engines

I was creating a website when I had the bright idea of pre-creating a webpage template and then filling in the values that I needed.  At first I was going to roll my own, and then I realized that there were many PHP templating engines in existence already.  I decided to give Twig a try since it seemed to be well-known.

http://devzone.zend.com/1886/creating-web-page-templates-with-php-and-twig-part-1/

  • Twig example.
  • Shows the basics.
  • Unfortunately, does not demonstrate a straightforward loop.

http://readwrite.com/2010/08/31/twig-templating-engine-quick-s#awesm=~opErZeZWdq7zLX

  • It’s almost crap.
  • Despite its claims, for a five minute guide, it doesn’t show the basics like how to include the dependencies.  In addition, the code throws an error if you put it in a try-catch block.
  • However this sample does show how to create a template with a loop.
  • The error disappeared when I replaced display/print with render/echo. I wonder if the error hid itself or did I really fix it.

Color Programming

This is for automation by screen capture

I have a program that picks out colors from a screen capture of an application.  Once I found the colors of interest, I tried comparing them against a previous capture of the image.

They didn’t match!

Did you know that the color grabbed can change depending on the monitor’s color?  I had one monitor configured for 16 bit color and the other monitor configured for 32 bit color.

Turns out that taking a screenshot of a game screen on both monitors yielded bitmaps with slightly different colors.