comtersoft » php
Call us today at US: 703 - 349 - 1168 | India: +91 471 - 301 0690

Archive for the ‘php’ Category

PHP Functions in Javascript - PHP.js

Posted on July 28th, 2009 in Developers Lounge, php | 1 Comment »

 

I found this wonderful site that ports commonly used, built in PHP functions into JavaScript.

Basically, just include the library and you should have all your common PHP functions like strlen, count, in_array, array_keys etc in Javascript. How cool is that!

I find the string manipulation functions most useful. The best part is you can choose to download only the functions that you need, individually, instead of the whole library.

Here is the list of functions currently ported to Javascript: http://phpjs.org/functions/index

Here is the link to the library homepage: http://phpjs.org/

Remember is this just a collection of functions built in PHP and not a framework like Jquery

Multiple display fields in find(’list’)

Posted on July 11th, 2009 in Developers Lounge, php | 2 Comments »

 

cakePHP’s find(’list’) function is used to generate a list of data (key=>value pairs) that can be used to populate select boxes easily.

A common requirement by most people is how to concatenate 2 fields in the display of the list (value) while still maintaining the key. For example, when displaying a list of users you may want to display the first name and last name while the key is the user id.
Currently, cakePHP’s find(’list’) function doesn’t support it. You can only specify a single field for a key and value.

I found this behaviour written by Daniel Albert (resshin) that will let you have multiple display fields http://bakery.cakephp.org/articles/view/multiple-display-field-3

I have used the customized version of this code (you can find it in the comments section of the link…written by Cosmin Cimpoi).

Just one tip: As Cosmin Cimpoi mentions you must ensure that recurive is atleast 0. In my case that didn’t work. For me recursive had to be set to atleast 1. But, I didn’t want recurive 1 to be used in other places so this is what I did:

$this->Address->find('list', array('recursive' => 1));

And it works like a charm!

PHP SimpleXML CDATA Issue - Solution

Posted on July 6th, 2009 in Developers Lounge, php | No Comments »

 

After hours of searching and using complicated functions to parse XML in PHP (simplexml) with CDATA I found this link that gives you the solution simply.

Normal XML files when passed to simplexml_load_file() will return an array with the tag name and value like this:

XML:
<GradeLevel>
	<GradeLevelItem>
		<GradeID type='Number'>1</GradeID>
		<Grade type='Text'>Primary K-3</Grade>
	</GradeLevelItem>
<GradeLevel>
SimpleXML output
SimpleXMLElement Object
(
    [GradeID] => 1
    [Grade] => Primary K-3
)

But, when the value is in CDATA you will not be able to access the value directly, like this:

XML:
<GradeLevel>
	<GradeLevelItem>
		<GradeID type='Number'><![CDATA[1]]></GradeID>
		<Grade type='Text'><![CDATA[Primary K-3]]></Grade>
	</GradeLevelItem>
<GradeLevel>
SimpleXML output
SimpleXMLElement Object
(
    [GradeID] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [type] => Number
                )
        )

    [Grade] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [type] => Text
                )
        )
)

To get the outut in the exact same way as the first case (to access the value) all you need to do is to pass a few extra params and the built in simplexml_load_file() function will do it for you automatically like this:

simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);

This is the link that saved me:
http://blog.evandavey.com/2008/04/how-to-fix-simplexml-cdata-problem-in-php.html

The secret of getting ahead is getting started. ComterSoft is the starting point