Paul's Dev Blog – iGonzo.net

So I says to myself… Self?

Archive for the ‘programming’ tag

ASP hates XML

without comments

This is what I thought. For the last day or so I have been trying to get a silly little (well, quite large actually) XML string to import into a database table.

After my previous troubles with just getting the data file, I thought I was in the clear. However, it seems that some of the nodes within the taget XML file contained characters that XML did not like at all. Mostly those characters that have squiggly little lines and flourishes above them. And despite my efforts, I just could not get them to import or to simply skip that data node.

So have spending more hours than I care to admin banging my head against the desk, I stepped back to examine the problem from its most basic form.

The simplified problem was this: I have a string of text that I need to parse and put into the database. There is a character or three preventing that.

The simplified solution was this: Remove the offending character.

Trouble was, I could not get the character targeted for a replace, as it was being processed according to whatever format the MS XMLHTTP object uses. Then it hit me. No matter what, information has to get passed, and the only want to ensure this was to convert certain characters to their HTML equivalents, such as   for spaces, < for brackets, etc. This is basic to every form of data transmission on the web.

So the simplified plan of attack now was this: Convert the entire XML string to and HTML encoded string; Find the HTML encoded character and replace it; Decode the string back to an XML string.

Once I had that plan it was stupidly simple to convert, replace, and then convert back to XML. I piped that into my DB input script and it worked.

So kids, the moral of the story is this: Step back once in a while to examine the problem. And then break it down (or convert it) to its most basic workable form. After that, solutions tend to pop up faster than just mucking about with fancy object functions.

-Cheers

Written by iGonzo

July 20th, 2011 at 1:20 pm

Javascript hates spaces

without comments

It’s the basics that someone often forgets. This is true in any profession once you progress to a certain level. The basics usually just come naturally, so people often don’t think that it could be something simple that is causing erros.

Javascript, for example, hates spaces. I knew this since I started programming Javascript. If you do not accomodate for them, it can and will lead to problems. Which is what I spent an hour trying to figure out last night.

See, the user can upload images into the gallery. The script replaces the background of a div with the image that was clicked. Pretty simple and clean, if I do say so myself. However, the user could upload images with just about any naming convention they wanted. This is what was causing the issues. The spaces in the names of the file that were uploaded. The script would come across this and crap out because I did not accomodate for spaces.

The solution was simple enough; replace the offending character with its HTML encoded equivalent, in this case using “%20”. After this was done server side, the script worked just like it should, and I commenced with beating myself about the head with my Javascript Bible book.

So the lesson of the day? If something isn’t working, and you know that it should be, go back to the basics. Are my quotes in the right place? Are there any offending characters that JS doesn’t like? Did I include the file with the correct path? These things will often be the cause of the issues and you will be greatly relieved that you spent the five minutes checking the basics (and often solving the issue), rather than wasting an hour ignoring the stuff you knew was right.

Cheers

Written by iGonzo

January 24th, 2011 at 10:39 am

Authorize.net and Classic ASP

with 2 comments

You know, a lot of people are moving off to other, cooler programming languages. PHP, ASP.NET, Ruby, all of these are seen as superior to Classic ASP; at one time the greatest language to use for dynamic data driven sites.

More often than I care to admit, I get asked “Why don’t you do it in [insert current language here] to do that? It’s easier”. True, but I have been using ASP for 11 years and am quite good at it if I do say so myself. Besides, as long as it can process XML, I see little reason to change our entire code base. Which brings me to the actual crux of this post: Using Classic ASP with Authorize.net.

More specifically, you can use any programming language with the Authorize API as long as you can process XML. But since I use primarily Classic ASP, that is what we have to use.

Authorize, in their wisdom, has seen fit to provide examples of accessing their API. One of these examples includes an ASP version. At first I totally ignored these, assuming that they could not accomplish what the client needed (which is integration with the Authorize CIM system). And so I toiled for many an hour creating functions and testing. Eventually I came across a problem I could not bull my way through, so I humbled myself and looked at their example code.

My first reaction was, of course, chagrin. Their ASP examples were performing functions that I needed. Precisely what I needed. *urg*. So, over the next 20 minutes, I functionalized their examples, added in our data points, and low and behold I got a positive response without any errors. I even tried to create errors and still the data went through.

Begin head banging on desk.

So then, after my headache went away, I grabbed every code example they had and was able to add in a whole mess of features and functions that not only added robustness to the site, but some serious value for the client. The transmission functions I created are able to take any form of XML request and sent it to Authorize and get a response back. After a quick check my reply data is added to the database and its on to the next thing: which for the client is making money (businesses are funny that way).

The lesson here, trolls and girls, is to not scoff at API example code. We can all look at the PDF of how to form this or that request, and what to expect back in response, but seeing a bit of sample code execute without errors is, in my opinion, a very uplifting thing to experience.

Now, if I can just figure out how this FBML crap, I think I’ll be golden.

Cheers

Written by iGonzo

November 5th, 2010 at 1:34 pm

The bane of JavaScript

without comments

So I just spent the last hour wondering why my GMaps implementation was not working on a particularly complex site. More to the point, I was wondering why it was working on the live site, but not on the testing site. That is, until I looked at the live site and saw it wasn’t working there either.

Uh oh.

So I started looking at the usual suspects when my JS isn’t working; forgot a semi-colon, misfiring functions, garden gnomes, etc. I then started looking at various doc-types, versions of jQuery and my GMaps API, even my content-type settings, and nothing was working.

Then it hits my like a lightning bolt: 90% of problems I deal with are from content. Specifically, content that has a lot of strange characters that HTML generally, and javascript in particular, doesn’t at all like. So I go through the dynamically rendered markers for my google maps’ points and find, in the 50 or so points I am rendering, there is one name that has an apostrophe in it.

Grrrrr.

A quick replace function in the code and, low and behold, shit starts working again. To make sure it doesn’t happen again I am now running the content through our nifty illegal characters replace function because, in the future, someone may enter a name or something that has a tilde character in it or something else equally strange.

The point is, boys and girls and others, that if you are dealing with javascript, especially if it is very large and complex javascript, be sure that there are no floating apostrophes out there.

On a side note, I am not against apostrophes  in general. I just believe that languages can do without them. I mean seriously; when was the last time you texted a properly formatted message to someone?

Thats what I thought.

K Thx Bia

L8r

Written by iGonzo

October 26th, 2010 at 1:06 am

Search Arrays without server code (sorta)

without comments

So it happens that I have a database field value that is a comma separated string of numbers. Classically, I would grab the field, turn it into an array, and search the array. Somewhat cumbersome and not really worth the effort of another function. That, and I would have to grab every record in the database, search through the applicable fields, and filter out the record(s) I need.

Me, being lazy, want an easier way to do it. Then I found this:

Then I came across this: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

All I have to do is construct a statement like this:

SELECT * FROM table_name WHERE FIND_IN_SET(‘thevalue‘,Field_To_Search)

You can also use a variable in place of ‘thevalue‘ if you need to search for, say, a passed parameter.

In the end, I was given a list of one or two records that I can easily do the rest of the work for, instead of grabbing every record in the table and filtering after the search.

For the lazy folks, this is quite a good thing. For the non-lazy folks, this is still a good thing. The above query not only executes faster, the code weight is greatly reduced and also runs faster (tests show about a 40% increase in speed and a 50% reduction in code execution time).

Final thoughts; this now leaves me more time to either work on something else, or go grab some food. I vote food.

Cheers

Written by iGonzo

October 15th, 2010 at 11:53 am

Posted in Database,Programming

Tagged with , ,

Check Yourself (and your code)

without comments

So I just spent a large-ish amount of time trying to figure out why, when I updated one data table from an XML feed, it updated a completely separate table that was not even related to the updated one.

The answer was on one tiny little line of code. That line of code executed a function in the other XML feed processor for the other table and dataset. The problem was that I copied the function and changed a few variables (since the feeds were almost identical) but didn’t go through and check to see if anything else was going on.

The lesson for today: Check your freaking code. If you want to copy/modify a function to use elsewhere, make sure that there aren’t any little items that would make for a not fun programming day.

Cheers

Written by iGonzo

August 20th, 2010 at 3:27 pm

Posted in Programming

Tagged with , ,