Use Value Set by Parent?
2 posts by 2 authors in: Forums > CMS Builder
Last Post: May 28, 2012 (RSS)
By Perchpole - May 25, 2012 - edited: May 25, 2012
I have a system in place allowing a user to select 1 of a choice of menus to display on a category page. However, instead of setting this value on every page, it would be rather good if I could work out a way for sub-categories to use the value set by the parent category (if their own value is not set).
Here's the code I have so far...
What I need to work out is:
Does the selectedCategory have a parent?
Does that category have a ['selectMarginRight'] value?
If so, getRecords....
Can anyone help, please?
:0)
Perch
Here's the code I have so far...
if(isset($selectedCategory['selectMarginRight'])) {
list($marginRight, $marginRightMetaData) = getRecords(array(
'tableName' => 'margin',
'allowSearch' => false,
'where' => " num = '".intval($selectedCategory['selectMarginRight'])."' ",
'orderBy' => 'title',
));
$marginR = @$marginRight[0];
}
elseif {
......
}
What I need to work out is:
Does the selectedCategory have a parent?
Does that category have a ['selectMarginRight'] value?
If so, getRecords....
Can anyone help, please?
:0)
Perch
Re: [Perchpole] Use Value Set by Parent?
By Jason - May 28, 2012
Hi Perch,
You could try something like this:
A couple of assumptions where made in this code. First that your category section was called categories. You may need to update this value in the mysql_get() function call.
Second, it's assumed that if no value for selectedMarginRight can be found in the parent record, or that there is no parent record, an error should be produced (see the 2 die() statements). This can be replaced with something else if this is not really an error condition.
Hope this helps
You could try something like this:
if(isset($selectedCategory['selectMarginRight'])) {
list($marginRight, $marginRightMetaData) = getRecords(array(
'tableName' => 'margin',
'allowSearch' => false,
'where' => " num = '".intval($selectedCategory['selectMarginRight'])."' ",
'orderBy' => 'title',
));
$marginR = @$marginRight[0];
}
elseif ($selectedCategory['_hasParent']) {
//get parent record
$parentCategory = mysql_get("categories", $selectedCategory['parentNum']);
if ($parentCategory && @$parentCategory['selectMarginRight']) {
list($marginRight, $marginRightMetaData) = getRecords(array(
'tableName' => 'margin',
'allowSearch' => false,
'where' => " num = '".intval($parentCategory['selectMarginRight'])."' ",
'orderBy' => 'title',
));
$marginR = @$marginRight[0];
}
else {
die("Error - no value for selectMarginRight found!");
}
}
else {
die("ERROR - selectMarginRight not selected in either selected category or category parent!");
}
A couple of assumptions where made in this code. First that your category section was called categories. You may need to update this value in the mysql_get() function call.
Second, it's assumed that if no value for selectedMarginRight can be found in the parent record, or that there is no parent record, an error should be produced (see the 2 die() statements). This can be replaced with something else if this is not really an error condition.
Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/