我正在使用Joomla Framework来创建显示MySQL表中选项的组件。 因为每个选项都有很多不同的参数,所以它们被设置为 custparams 字段中的json数组。
此函数根据选项ID从每个选项中提取这些自定义参数。
JSON数据被解码并写入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;
}
}
是否可以将此数据写为:
$this->_oparams->$pkey=$custom_params->pvalue;
所以我可以避免列出所有参数?
稍后在代码中我以这种方式检查参数:
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."\" />";
答案 0 :(得分:3)
我相信这就是你要找的东西:
...
foreach ($custom as $custom_params)
$this->_oparams->{$custom_params->pkey} = $custom_params->pvalue;
}
...