jump to navigation

using JSON to pass JavaScript arrays to PHP via Ajax November 26, 2007

Posted by chris in : ajax/dom/javascript, php , 1 comment so far

We don’t use PHP 5.x at work so I needed to install JSON via PEAR. In your PHP script include the JSON.php file and instaniate the object.

include_once('JSON.php')
$json = new Services_JSON();

In your javascript include a reference to json.js minus the comment marks:

(<)script src="json.js" type="text/javascript"(>)(<)/script(>)

Now create a simple javascript method that will perform an HTTP GET and pass an array broken down into JSON to your PHP script:

	function testJSON()
	{
		var testArr = new Array();
		testArr[0] = 0;
		testArr[1] = 1;
		testArr[2] = 2;
		testArr[3] = 3;
		testArr[4] = 4;
		var testJSONArr = testArr.toJSONString();
		var url = "mypage.php?func=testJSON&jsonArr=" + testJSONArr;
		if(XMLHttpRequestObject)
		{
			XMLHttpRequestObject.open("GET", url, true);
			XMLHttpRequestObject.onreadystatechange = function()
			{
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
				{
					try
					{
						var txtDocument = XMLHttpRequestObject.responseText;
						txt.innerHTML = txtDocument;
					}
					catch(err)
					{
						txt.innerHTML = "Description not found.";
					}
				}
			}
			XMLHttpRequestObject.send(null);
		}
	}

At the top of mypage.php you should have this:

	switch ($_GET[func])
	{
		case 'testJSON':
			echo $myPHPObj->testJSON(urlencode($_GET[jsonArr]));
			break;
	}
	return;

Your PHP object will have a method in it like this:

	function testJSON($jsonArr)
	{
		$json = new Services_JSON();
		$jsonArr = $json->decode(urldecode($jsonArr));
		foreach($jsonArr as $element)
		{
			echo "js array element: $element
"; } }

Thats it. My first use of JSON. I was really getting tired of packing data in long strings with strange explode points and felt XML was to bloated for what I was doing.

MySQL Case Statement November 8, 2007

Posted by chris in : SQL , add a comment

The database I was working on the other day is FAR from normalized. It stores shipping types as regular VARCHAR strings. We needed to orderby Overnight and International orders so they print first in the batch. Using a case statement I was able to create a temporary sane column in this query, using the integer values in this temporary shipType column I was then easily able to orderby and it only added 1/10 of a second to the query’s execution time. Here is an example of the MySQL case statement:

				CASE SUBSTRING_INDEX( shipping, '|', 1 )
				     WHEN 'Overnight' THEN 1
				     WHEN 'Overnight Delivery' THEN 2
				     WHEN 'International' THEN 3
				     WHEN 'USPS International Shipping' THEN 4
				     WHEN '' THEN 5
				     WHEN ' * Product(s) qualify for Free shipping * ' THEN 6
				     ELSE SUBSTRING_INDEX( shipping, '|', 1 )
				    END AS shipType 

Querying MS SQL database looking for a specific column November 2, 2007

Posted by chris in : SQL , add a comment

At work we interact with a MS SQL server that our shipping software runs on. We are a MySQL shop so don’t use MS SQL too much and the database in this proprietary shipping system is pretty large (about 100 tables). I needed to find a table that another table had a relation too and only knew the column name. So I ran this

SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name = ‘Alias_ID’ )

in MS SQL 2000 Query Analyzer and returned a list of 6 tables.

A lot better to troll through 6 tables than 100 tables.