Bectin
  1. You are here:  
  2. Home
  3. Random Insights
  4. PHP

Sort array of objects based on one of the object parameter values

$Skaters is an array of objects which have one parameter skatername and this will sort the array basedon teh skatername parameter.

usort($Skaters, function($a, $b)
        {
            return strcmp($a->skatername, $b->skatername);
        });

SimpleXMLElements

When you have a data object that contains SimpleXMLElements they can be tricky to access because they look like objects but they aren't accessed in the same way. In this case they appeared to be objects that had the @ symbol in the object name.

For example, a complex object that is used by Form.php $form->xml->fieldset[0]->field[1]->{'@attributes'}['name] but the field elements are actually SimpleXMLElements and not objects.

So, you can view them using: 

$form->xml->fieldset[0]->field[1]->attributes()->name;

and write to them using

$form->xml->fieldset[0]->field[$i]['name']

 

Batch rename of files

This code was part of the demo on creating a joomla extension http://www.slideshare.net/TimPlummer/how-to-create-a-joomla-componet-from-scratch?next_slideshow=2

It is used to rename all of the files in the helloworld example extension so that you can create a new extension from that code more quickly. In this case it creates a new extension named newthing.

@echo off

setlocal enabledelayedexpansion

for /R %%j in (*.*) do (

set filename=%%~nj

set filename=!filename:helloworld=newthing!

if not !filename!==%%~nj ren %%j

!filename!%%~xj

)

If you're continuing down the path of creating a new extension, you'll also need to do the following global search and replace.

com_helloworld = com_newthing

HelloWorld = NewThing

COM_HELLOWORLD = COM_NEWTHING

helloworld = newthing

HELLOWORLD = NEWTHING

Hello World = New Thing

hello = newthing

Hello = Newthing

World = Newthing

Consider using grepWin which is a free tool that uses grep to help with search and replace.

Creating left hand menu in admin component

The right hand menu that often appears in admin components is configured in the /com_name/helpers/name.php file.

For example:

public static function addSubmenu($submenu)
    {
        JSubMenuHelper::addEntry(
            JText::_('COM_REGCAN_SUBMENU_CANSKATESESSIONS'),
            'index.php?option=com_regcan&view=organizers',
            $submenu == 'canskatesessions'
        );

                JSubMenuHelper::addEntry(
            JText::_('COM_REGCAN_SUBMENU_MESSAGES'),
            'index.php?option=com_regcan',
            $submenu == 'messages'
        );

        JSubMenuHelper::addEntry(
            JText::_('COM_REGCAN_SUBMENU_CATEGORIES'),
            'index.php?option=com_categories&view=categories&extension=com_regcan',
            $submenu == 'categories'
        );

        // set some global property
        $document = JFactory::getDocument();
        $document->addStyleDeclaration('.icon-48-regcan ' .
                                       '{background-image: url(../media/com_regcan/images/tux-48x48.png);}');
        if ($submenu == 'categories')
        {
            $document->setTitle(JText::_('COM_REGCAN_ADMINISTRATION_CATEGORIES'));
        }
    }

Flatten data in nested arrays

I was getting some data out of RSForms that wasn't structured in a way that was easy to manage, so I used the following routine to flatten all the data into a single array of variables and their values. It takes checkbox, radio button and list box selections and turns them into simpler variable.

function flatten_data($Data, $KeepKey='')
// takes whatever is passed to it an turns it into a flat array of parameters
{
    $Parameter = array();
    foreach($Data as $key=>$item)
    {
        $Type = gettype($item);
        if($Type == "array")
        {
            if(sizeof($item)==1) // if the array only has one item
            {
                $NewParameter = flatten_data($item, $key);
            }
            else $NewParameter = flatten_data($item);
        }
        else
        {
            if($KeepKey!='') $Parameter[$KeepKey]=$item;
            else $Parameter[$key]=$item;
        }
        if($NewParameter) $Parameter = array_merge($Parameter, $NewParameter);
    }
    return $Parameter;
}

  1. Joomla Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in \libraries\joomla\database\driver\mysql.php on line 464
  2. array_unique on a Multi-dimensional array
  3. Referencing an object variable that has a space in its name
  4. PHP Form calls itself

Page 2 of 7

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Main Menu

  • Home
  • Services
  • Portfolio
  • About
    • Bectin's Founder
  • Contact
  • Random Insights
    • Joomla
    • Joomla Extensions
    • Web Sites
    • Windows Computers
    • Zen Cart
    • Magento
    • MySQL
    • Business
    • Technology
    • Google Analytics & Webmaster Tools
    • PayPal
    • PHP