Archive

Archive for the ‘Programming’ Category

PHP DOM method gotcha!

July 2nd, 2009

Ok, so here is a “gotcha” with PHP’s DOM… if you use the method createTextNode(), and the string you pass happens to have HTML chars, the method will AUTOMATICALLY convert to html special chars. Guess they should call the method createTEXTnode instead!

Chris Fontes Programming

MySQL and a NULL index walk into a bar….

June 9th, 2009

Did you know that in MySQL, if you have a NULL field in your “unique” index, you can have multiple duplicates of that row and MySQL will turn a blind eye? Oh yes, that is a pain. Example: If you have a unique index that consists of 2 fields (ID and secondID), you “could” end up with (1, NULL), (1, NULL), (1,NULL). Sure, it looks to be identical, but all 2 identical rows can exists in perfect harmony. This is particularly problematic when realying on a unique key and using INSERT ON DUPLICATE KEY UPDATE queries.

Trust me. 3 days of frustration to learn this. MySQL does NOT classify this as a bug, but the community has made it clear that it acts like one. Hopefully they will fix this in an upcomnig release.

Chris Fontes Programming

Following your own advice

June 2nd, 2009

Well I have been pulling my hair out trying to debug a Javascript issue, and it turns out that not only do I know the solution, but I JUST blogged about it! You see, in Javascript, an empty string will return false in an if statement, so:

val=”";

if (val){ …

will return false. Although it shouldn’t, because it IS technically a value (the value of nothing). SO, using a !== fixes this, as earlier in the code I explicitly set it to false, then change it to a value if I need. Long story short,

if (val !== false){ …..

is what I should have done in the first place!

Chris Fontes Programming

PHP functions that return Boolean

June 2nd, 2009

OK, so there are several function in PHP that return Boolena true or false. So, if you want to test against them, us coders get in the habit of doing something like this:

if (!isarray($myArray)){ …..

Problem. What is that function returns an interger, OR boolen, such as strpos? You will see tons of people doing this:

if (!strpos($haystack, $needle)){ ……

Bad bad bad. This will cause all kinds of problems. What if the “needle” is found at position Zero? Zero is also interpereted as False, although in this case what you want is a True… SOOO… be good boys and girls and use the mysterious triple equals.

if (stripos($needle, $haystack) === false){ ….

or

if (stripos($needle, $haystack) !== false){ ….

You can also use a greater than -1, but I see this as sloppy. Is it actaully sloppy? Maybe not, just my feelings on the matter. I feel that if you should be as specific as you can get in your logical expression testing.
Hope that helps ya!



Chris Fontes Programming

PHP array key’s and the period (dot) character

March 31st, 2009

I found today that you cannot have a period in your string key. For example, this is invalid:

$array['my.name'] = “some name”;

This is most important to note when you are posting (or getting for that matter) into a script. I found this out while attempting to Post data from an AJAX call to my server. PHP automatically turns periods (dots) into underscores. Check out the reference below, it took me FOREVER to find supporting material to this theory…

http://php.net/manual/en/language.variables.external.php#language.variables.external.dot-in-names

Happy coding

Chris Fontes Programming

Preparation, direction and common sense

March 28th, 2009

OK, so this post is all about the importance of the aforementioned title. Whether you are a first timer or a seasoned veteran, creating a new website is a huge task. Just like home renovation, a new site will take longer than you want, be more involved that you imagined and cost more than you had hoped.

Also just like home renovation, preparation is vital. If you don’t do your homework, and allow PLENTY of time for the research and planning stages, buy the time you start developing the stitching will fall out of the design. I cannot stress this enough… starting to develop a site without an ENTIRE scope and direction is foolish and unprofessional. I’m just sayin’….. :)

Chris Fontes Programming

MySQL Conditional Left Joins

March 27th, 2009

Ok,  so the title is a bit misleading… I am not going to show you necessarily how to only LEFT JOIN a table based on a condition, but I will show you how to get the same results as you would expect.

First, you may be asking…. why would I ever need to? Well, let ’s say you have 3 tables. Table One is acting as your primary index relation table. Then, based on the info IN that table, you need to join either table Two or table Three. How would you do it?

SELECT `one`.first_name, `one`.male_female,
IF(`one`.male_female=’boy’,boyTable.meaning,girlTable.meaning) AS name_meaning
FROM `one`
LEFT JOIN `two` AS boyTable ON boyTable.first_name=`one`.first_name
LEFT JOIN `three` AS girlTable ON girlTable.first_name=`one`.first_name

This will let you, for example, select the meaning of someones name if the meanings where in two different tables based on gender. This is of course, is just an example, and this particular situation would be handled better by just setting a flag in a singular table that specifies male of female, but I needed to come up with some way illustrate the concept, so her you are!

hope this helps someone

Chris Fontes Programming , , ,

Christian Web Design?

March 27th, 2009

For those of you who are Christian and either are web developers, web designers or somewhere in between, you need to read this….

http://mirificampress.com/permalink/christians_would_rather_copy_than_create

The entire Blog is great, and I am a subscriber… but this post stuck out to me. Matt, the gentleman who produces this Blog, has some GREAT things to say, er, I mean write. ;)

Chris Fontes Christianity, Programming ,

Code Jedi Master

March 25th, 2009

So, (in reagards to web applications), if learning HTML makes you a padawan, and becoming a solid programmer (PHP, javascripts, CSS, etc) makes you a Jedi, than learning Regular Expressions makes you a Jedi Master! I still have to use a reference about half the time, but regular expressions make life easy… and you feel like a rock star when they work properly! Look at http://www.regular-expressions.info for a GREAT reference and tutorial.

Chris Fontes Programming ,

Javascript nodeType

March 24th, 2009

have you ever looped through your page to grap inputs to post somewhere? If you have, then surely you have used something like:

for (var i=0;i<elem.childNodes.length;i++)

if so, then you might have run into a little issue in which some browsers (including firefox) will render the “n” as a childNode (this is correct actually, just frustrating).

In comes the solution ….. Node.nodeType. You can check elem.childNodes[i].nodeType to see what it is (https://developer.mozilla.org/En/DOM/Node.nodeType). If it is a 1, it is an element (li, div, etc). skip everything that is != 1, and tada! You are in business!

hope this helps someone

Chris Fontes Programming , , ,