Development Blog

PHP Broken Array from json_decode Object

I ran into an odd PHP thing (bug?) today. I had an object that had been extracted from a string using json_decode. After type-casting it to an array, any entries stored with numeric keys were inaccessible when calling by key, though iteration worked fine.

Here’s a simplified example:

// make an associative array
$myList = array(
// json encode to string
$myList = json_encode($myList);
// json decode to object
$myList = json_decode($myList);
// typecast back to array
$myList = (array) $myList;
// test output
$test1 = (array_key_exists(4, $myList))
	? $myList[4] : 'nothing!';
$test2 = (array_key_exists('nine', $myList))
	? $myList['nine'] : 'nothing!';
echo 'test1: '.$test1.', test2: '.$test2;

Read More »

Get Field Label Text in Joomla 2.5 JForm

I’m switching back and forth between projects in Joomla 3.X and Joomla 2.5… to get the field label text (not the HTML tags, just the text) from a JFormField object is simple enough in Joomla 3.X, since the class has a getAttribute() method, so:

$field = $form->getField('myfield');
$label = $field->getAttribute('label');

I could be mistaken, but didn’t see the same functionality in Joomla 2.5. The closest I’ve come up with is to get the full label, then strip out the HTML tags, like:

$field = $form->getField('myfield');
$label = strip_tags($field->__get('label'));

…which is ugly, but seems to work in the cases I’ve tested. Anyone know of a better way to access the element attributes directly in Joomla 2.5?

Joomla 2.5 – stuck redirecting to index.php for 404

I’ve just spent the last few hours troubleshooting why a Joomla 2.5 site I’m working on was redirecting to the home index.php page for bad URLs instead of loading my template’s custom error.php page. Instead of the 404 response code, it was returning a 303 response: 303 See other

From what I can tell, it was because in the Global Configuration settings, I had the site offline for testing. So apparently even if logged in as a user who can view the offline site, it still won’t load the error page until the site is back online. When I put it back online, the 404 response was loading as expected… now I know.

WordPress Custom Post Types and 404 Error

If you’re working on a custom post type in WordPress and make a change to the post type or category slug (or add a new one), and you’re getting 404 errors when visiting the pages at the new address, chances are you just need to refresh the WordPress rewrite rules for permalinks. You can do this in your code by calling the flush_rewrite_rules() function:

…or, go to the WordPress admin under Settings > Permalinks and hit the “Save Changes” button, which also triggers the function. This is usually the best and quickest fix if making changes while developing. If using the flush_rewrite_rules() function, be sure to only call it on plugin or theme activation and deactivation.

PHP: Round Number to Minimum Precision

While working on an e-commerce system, I needed to use PHP to round a number to a minimum number of decimal places, but allow for more if needed. For instance, if the value was 1.2, I’d want it rendered as $1.20, but if it was 1.256, I’d want it to keep the precision and render as $1.256. I ended up converting the number to a string, splitting by the decimal, enforcing the minimum precision and then using PHP’s number_format() function, like: Read More »

Joomla 2.5 Component Categories Sub-Nav

I recently found Joomla’s feature to easily add custom component-specific nested Categories. It makes for a quick way to create categories similar to what is used in com_content and an interface to manage those categories without having to build it all manually. You can use JForm to easily add a select menu of your custom categories. The categories interface can even include a custom sub-menu to easily get back to the rest of your component.

Read More »

Joomla! 2.5 Toolbar Popup Link Icon

Today I needed to add a new toolbar button to a custom Joomla! component. In the past, I’ve used a more convoluted method of overriding the submitbutton() (or Joomla.submitbutton) JavaScript function and triggering the modal popup manually, but stumbled upon a better solution. Read More »

ProcessForm/NateMail Patch Update

I’m still planning to develop new form processing scripts, but in the meantime, a patch update has been posted for ProcessForm to deal with possible error messages in PHP 5.3+ about the eregi() function being deprecated (has been replaced by the newer preg_match() function). Also, I’ve switched the encoding to utf8 to better support non-English characters in the email text, confirmation screen and MySQL storage.

ProcessForm info and download page ยป

Ajax Form Submission in Joomla 2.5 with jQuery

I’m developing a custom c-commerce component for Joomla 2.5, and today was struggling with using Ajax from my admin lists. I wanted to make small changes to the list via Ajax without having to reload the entire page – publish state, deletes, quick edits, etc. To handle the Ajax form submissions, I was using jQuery and the jQuery Form Plugin. Read More »

Custom Admin Quickicon in Joomla! 2.5

I recently had to dig up how to add a custom admin Quickicon (the icon links on the left side of the Administrator section’s control panel) in Joomla 2.5. In previous versions, I’d just built a custom admin module for each icon. Now, there seems to be a somewhat easier way. Read More »