2

I'm using Joomla Framework to create component that displays options from a MySQL table. Because each option has a lot of various parameters they are being set as json arrays in custparams field.

This function extracts these custom parameters from each option based on option id.

JSON data is decoded and written into stdObject:

$_oparams=null;
function _custParamsOpt($pid)
{       
    $query='SELECT custparams FROM #__mycomponent_options'
                .' WHERE id = '.(int)$pid;
    $this->_db->setQuery( $query);
    $paramdata=$this->_db->loadResult();

    if (!empty($paramdata))
    {
        $custom=json_decode($paramdata);
        foreach ($custom as $custom_params)
        {
            if ($custom_params->pkey=='elemvert') $this->_oparams->elemvert=$custom_params->pvalue;
            if ($custom_params->pkey=='elemhor') $this->_oparams->elemhor=$custom_params->pvalue;
            if ($custom_params->pkey=='minwidth') $this->_oparams->minwidth=$custom_params->pvalue;
            if ($custom_params->pkey=='maxwidth') $this->_oparams->maxwidth=$custom_params->pvalue;
            if ($custom_params->pkey=='minheight') $this->_oparams->minheight=$custom_params->pvalue;
            if ($custom_params->pkey=='maxheight') $this->_oparams->maxheight=$custom_params->pvalue;
        } 
        return true;    
    } else {
        return false;
    }                   
}

Is it possible to write this data as:

$this->_oparams->$pkey=$custom_params->pvalue;

so I can avoid listing all the parameters?

In the code later I check parameters in this way:

if (!empty($this->_oparams->maxheight)) $htmlout.="\t\t\t\t<input type=\"hidden\" name=\"maxheight".$rowx->id."\" id=\"maxheight".$rowx->id."\" value=\"".$this->_oparams->maxheight."\" />";
1
  • 1
    Please change the title of your question to something more describing so other might benefit. Commented Nov 24, 2011 at 12:17

1 Answer 1

3

I believe this is what you are looking for:

...
foreach ($custom as $custom_params)
    $this->_oparams->{$custom_params->pkey} = $custom_params->pvalue;
}
...
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much, square brackets - is one of many things I didn't know about php object programming, I will use it more right now!
They're actually called curly braces. You can also do something like ${$custom_params->pkey} = $custom_params->pvalue so you can access the vars like so: $minwidth, $maxwidth. Please accept the answer if it's the answer you're looking for. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.