Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
Hello,
I would like to create a user-defined tag to retrieve the value stored in the field 'param1' of table 'cms_content_props' in order to include it in a template : similar to {title} but for another field of the database.
It must be very simple, but I could not find the appropriate code...
(I use CMS Made Simple version 1.4.1)
Thanks for your help
I would like to create a user-defined tag to retrieve the value stored in the field 'param1' of table 'cms_content_props' in order to include it in a template : similar to {title} but for another field of the database.
It must be very simple, but I could not find the appropriate code...
(I use CMS Made Simple version 1.4.1)
Thanks for your help
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
What you stored this value?fxv_cms wrote: I would like to create a user-defined tag to retrieve the value stored in the field 'param1' of table 'cms_content_props' in order to include it in a template : similar to {title} but for another field of the database.
Alby
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
I stored a string of character in this field. Each page from my site will have a different value and I want to display it
François
François
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
Try this:
Store that code as an UDT named e.g. get_content_props.
In your Template call {get_content_props}.
Using no params will print out all fields of the database table content_props.
Available params:
fields="fieldname1,fieldname2,..." (optional)
- a list of fields that will be printed out separated by a comma. If not set all properties will be printed out.
content_id = "15" (optional)
- the id of the content that properties will be printed out. If not set the current contents id will be used.
spearator="
" (optional)
- a character or HTML-Tag that separates the fields from each other if multiple fields are displayed. If not set a line break (
) will be used.
smarty=true/false (optional)
- if set to "true" the field(s) will not be displayed instantly but smarty vars will be assigned to your template instead. (default is "false")
That means you will have an array called {$content_props} that can be accessed in a {foreach}-loop in your template.
E.g.:
You also can access the props directly using
in your template.
Hope that helps.
Code: Select all
global $gCms;
$db =& $gCms->GetDb();
$content_id = $gCms->variables['pageinfo']->content_id;
$fields = '*';
$separator = '<br />';
if( isset($params['content_id']) ) {
$content_id = trim($params['content_id']);
}
if( isset($params['fields']) ) {
$fields = trim($params['fields']);
}
if( isset($params['separator']) ) {
$separator= $params['separator'];
}
$query = "SELECT ". $fields ." FROM ".cms_db_prefix()."content_props WHERE content_id=".$content_id;
$dbresult = $db->Execute($query);
$i=0;
if($dbresult && $row = $dbresult->FetchRow()) {
$result = $row;
}
// assign vars to smarty
if( isset($params['smarty']) && ($params['smarty']==true || $params['smarty'] == "true")) {
$smarty->assign('content_props',$result);
foreach($result as $k=>$v) {
$smarty->assign('content_prop_'.$k,$v);
}
}
// or just print out the result
else {
$i=0;
foreach($result as $res) {
$i++;
echo $res;
if($i<count($result))
echo $separator;
}
}
In your Template call {get_content_props}.
Using no params will print out all fields of the database table content_props.
Available params:
fields="fieldname1,fieldname2,..." (optional)
- a list of fields that will be printed out separated by a comma. If not set all properties will be printed out.
content_id = "15" (optional)
- the id of the content that properties will be printed out. If not set the current contents id will be used.
spearator="
" (optional)
- a character or HTML-Tag that separates the fields from each other if multiple fields are displayed. If not set a line break (
) will be used.
smarty=true/false (optional)
- if set to "true" the field(s) will not be displayed instantly but smarty vars will be assigned to your template instead. (default is "false")
That means you will have an array called {$content_props} that can be accessed in a {foreach}-loop in your template.
E.g.:
Code: Select all
{foreach from=$content_props item=prop}
{$prop->FIELDNAME}<br />
{/foreach}
Code: Select all
{$content_props_FIELDNAME}
in your template.
Hope that helps.
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
1) Thank Alby - I tried the code you suggested, but without success : it provides some field names but not field values.
2) I expected the value of the "Extra Page Attribute 1" at the bottom of the "options" tab in the "Edit pages" of CMS [there are in fact 3 such extra attributes] to be stored in field "param1" of table content_props but
for the same content_id, 6 records are created in table content_props :
- one with prop_name="content_en"
- one with prop_name="extra3"
- one with prop_name="extra2"
- one with prop_name="extra2"
- one with prop_name="pagedata"
- one with prop_name="target"
and the value entered as "Extra Page Attribute 1" is stored in field "content_en" of record which has prop_name="extra1"
3) Then, retrieving this value is equivalent to retrieve the page content (which is stored in field "content_en" of record which has prop_name="content")... which is very well done by the standard {content} tag
4) Does anyone know how to create a {param1} tag based on the {content} tag ?
Thanks
François
it is stored in the "content_en" field of a record which
"param1" field which looks like one field of content_props
2) I expected the value of the "Extra Page Attribute 1" at the bottom of the "options" tab in the "Edit pages" of CMS [there are in fact 3 such extra attributes] to be stored in field "param1" of table content_props but
for the same content_id, 6 records are created in table content_props :
- one with prop_name="content_en"
- one with prop_name="extra3"
- one with prop_name="extra2"
- one with prop_name="extra2"
- one with prop_name="pagedata"
- one with prop_name="target"
and the value entered as "Extra Page Attribute 1" is stored in field "content_en" of record which has prop_name="extra1"
3) Then, retrieving this value is equivalent to retrieve the page content (which is stored in field "content_en" of record which has prop_name="content")... which is very well done by the standard {content} tag
4) Does anyone know how to create a {param1} tag based on the {content} tag ?
Thanks
François
it is stored in the "content_en" field of a record which
"param1" field which looks like one field of content_props
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
I don't understand... i believe i'm confused... no, wait! Maybe i'm not.fxv_cms wrote: 1) Thank Alby - I tried the code you suggested, but without success : it provides some field names but not field values.
It was me who posted that code. Not Alby

That code i posted does exactly provide the content of any field of the content_props table.
E.g in your template (or content):
{get_content_props fields="param1"}
That will print out the content of the column named "param1" of the currently shown page.
I tried it by myself and it works fine.
I expected the same thing...fxv_cms wrote:
2) I expected the value of the "Extra Page Attribute 1" at the bottom of the "options" tab in the "Edit pages" of CMS [there are in fact 3 such extra attributes] to be stored in field "param1" of table content_props...
So, that is why my code doesn't work properly for your purposes.fxv_cms wrote:
...but
for the same content_id, 6 records are created in table content_props :
- one with prop_name="content_en"
- one with prop_name="extra3"
- one with prop_name="extra2"
- one with prop_name="extra2"
- one with prop_name="pagedata"
- one with prop_name="target"
and the value entered as "Extra Page Attribute 1" is stored in field "content_en" of record which has prop_name="extra1"
We just need to modify the code:
Code: Select all
global $gCms;
$db =& $gCms->GetDb();
$content_id = $gCms->variables['pageinfo']->content_id;
$fields = array();
$separator = '<br />';
$result = array();
if( isset($params['content_id']) ) {
$content_id = trim($params['content_id']);
}
if( isset($params['fields']) ) {
$fields = explode(',',trim($params['fields']));
}
if( isset($params['separator']) ) {
$separator= $params['separator'];
}
$i=0;
$query = "SELECT prop_name,content FROM ".cms_db_prefix()."content_props WHERE content_id=".$content_id;
if(!empty($fields)) {
$query .= " AND (";
foreach($fields as $f)
{
$i++;
$query .= "prop_name='".$f."'";
if($i<count($fields))
$query .= " OR ";
}
$query .= ")";
}
$dbresult = $db->Execute($query);
while($dbresult && $row = $dbresult->FetchRow()) {
$result[$row['prop_name']]['fieldname'] = $row['prop_name'];
$result[$row['prop_name']]['data'] = $row['content'];
}
// assign vars to smarty
if( isset($params['smarty']) && ($params['smarty']==true || $params['smarty'] == "true")) {
$smarty->assign('content_props', $result);
}
// or just print out the result
else {
$i=0;
foreach($result as $res) {
$i++;
echo $res['data'];
if($i<count($result))
echo $separator;
}
}
The param fields must contain a list of the prop_names as they where stored in the database.
Examples...
This will print out all content properties of the current page separated by a linebreak:
Code: Select all
{get_content_props}
Code: Select all
{get_content_props content_id="15"}
Code: Select all
{get_content_props content_id="15" separator=","}
Code: Select all
{get_content_props fields="extra1,extra2"}
Code: Select all
{get_content_props smarty=true}
{$content_props.content_en.data}
{$content_props.target.data}
{$content_props.extra1.data}
Code: Select all
{get_content_props smarty=true}
{foreach from=$content_props item=prop}
{$prop.fieldname}:{$prop.data}
{/foreach}
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
That's great : it's exactly what I wanted !
Thank you very much NaN for your help
François
Thank you very much NaN for your help
François
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
Not that I was looking for this kind of functionality, but thanks to persons like NaN, this forum has a far higher quality level then others that I have looked into.
Great work NaN,
Duketown
Great work NaN,
Duketown
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
awesome udt nan!
this should be integrated into cmsms core!
thanks alot
this should be integrated into cmsms core!
thanks alot
-
- Forum Members
- Posts: 106
- Joined: Thu Oct 05, 2006 11:27 am
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
Hi
Excellent tag. Thank you
Though I need it to do one more thing.
Does anyone know how to achieve the following quickly?
________________
If extra1 is empty
don't do anything
else
do something and show extra1
/if
If extra2 is empty
don't do anything
else
do something and show extra2
/if
I have seperate divs and styling around extra1 and extra 2.
The only way I can get it to work for me at the moment means the result has empty divs.
Any guidance really appreciated.
thanks
E
Excellent tag. Thank you

Though I need it to do one more thing.
Does anyone know how to achieve the following quickly?
________________
If extra1 is empty
don't do anything
else
do something and show extra1
/if
If extra2 is empty
don't do anything
else
do something and show extra2
/if
I have seperate divs and styling around extra1 and extra 2.
The only way I can get it to work for me at the moment means the result has empty divs.
Any guidance really appreciated.
thanks
E
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
This is easy:
Code: Select all
{get_content_props smarty=true fields="extra1,extra2,extra3"}
{if $content_props.extra1.data!=""}
<div>$content_props.extra1.data</div>
{/if}
{if $content_props.extra2.data!=""}
<div>$content_props.extra2.data</div>
{/if}
{if $content_props.extra3.data!=""}
<div>$content_props.extra3.data</div>
{/if}
-
- Forum Members
- Posts: 106
- Joined: Thu Oct 05, 2006 11:27 am
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
Nan
Thank you! Absolutely bloomin champion..
just needed to add {} in the div..
{get_content_props smarty=true fields="extra1,extra2,extra3"}
{if $content_props.extra1.data!=""}
{$content_props.extra1.data}
{/if}
{if $content_props.extra2.data!=""}
{$content_props.extra2.data}
{/if}
{if $content_props.extra3.data!=""}
{$content_props.extra3.data}
{/if}

Thank you! Absolutely bloomin champion..
just needed to add {} in the div..
{get_content_props smarty=true fields="extra1,extra2,extra3"}
{if $content_props.extra1.data!=""}
{$content_props.extra1.data}
{/if}
{if $content_props.extra2.data!=""}
{$content_props.extra2.data}
{/if}
{if $content_props.extra3.data!=""}
{$content_props.extra3.data}
{/if}



Re: Create a user-defined tag to retrieve a field value from table 'cms_content_
Doh!lainyrache wrote: Thank you! Absolutely bloomin champion..
just needed to add {} in the div..

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
Code: Select all
global $gCms;
$db =& $gCms->GetDb();
[b]
$content_alias = $gCms->variables['pageinfo']->content_alias;
[/b]
$content_id = $gCms->variables['pageinfo']->content_id;
$fields = array();
$separator = '<br />';
$result = array();
[b]
if( isset($params['alias']) ) {
$content_alias = trim($params['alias']);
}
elseif( isset($params['content_id']) ) {
$content_id = trim($params['content_id']);
}
[/b]
if( isset($params['fields']) ) {
$fields = explode(',',trim($params['fields']));
}
if( isset($params['separator']) ) {
$separator= $params['separator'];
}
$i=0;
[b]
if( isset($params['alias']) ) {
$query_get_id = "SELECT content_id FROM ".cms_db_prefix()."content WHERE content_alias=".$content_alias;
$dbresult = $db->Execute($query_get_id);
if ($dbresult) {
$row = $dbresult->FetchRow();
}
$content_id = $row['content'];
}
$dbresult = null;
[/b]
$query = "SELECT prop_name,content FROM ".cms_db_prefix()."content_props WHERE
content_id=".$content_id;
if(!empty($fields)) {
$query .= " AND (";
foreach($fields as $f)
{
$i++;
$query .= "prop_name='".$f."'";
if($i<count($fields))
$query .= " OR ";
}
$query .= ")";
}
$dbresult = $db->Execute($query);
while($dbresult && $row = $dbresult->FetchRow()) {
$result[$row['prop_name']]['fieldname'] = $row['prop_name'];
$result[$row['prop_name']]['data'] = $row['content'];
}
// assign vars to smarty
if( isset($params['smarty']) && ($params['smarty']==true || $params['smarty'] == "true")) {
$smarty->assign('content_props', $result);
}
// or just print out the result
else {
$i=0;
foreach($result as $res) {
$i++;
echo $res['data'];
if($i<count($result))
echo $separator;
}
}
Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'
Hi, its a bit late i guess but the error is here:
gemini wrote: if( isset($params['alias']) ) {
$query_get_id = "SELECT content_id FROM ".cms_db_prefix()."content WHERE content_alias=".$content_alias;
$dbresult = $db->Execute($query_get_id);
if ($dbresult) {
$row = $dbresult->FetchRow();
}
$content_id = $row['content']; // ".
(optional) props - a list of properties separated by a comma that will be printed out. (this has been the param fields before)
(optional) assign - the name of a smarty variable you can assign the ouptput to instead of print it out. this variable will contain the whole output.
(optional) assign_as_array - the name of a smarty variable you can assign the ouptput to instead of print it out. this variable will contain the whole database result as an array.
And here some examples:
(notice the changed usage of the smarty array)
This will print out all content properties of the current page separated by a linebreak:This will print out all content properties of the page with the content_id "15" separated by a linebreak:Code: Select all
{get_content_props}
This will print out all content properties of the page with the content_id "15" separated by a comma:Code: Select all
{get_content_props content_id="15"}
This will only print out the properties named "extra1" and "extra2" of the current page separated by a linebreak:Code: Select all
{get_content_props content_id="15" prop_separator=","}
This will provide a smarty variable named "content_props" that you can access after calling the UDT:Code: Select all
{get_content_props props="extra1,extra2"}
To access the properties directly use the dot-syntax:Code: Select all
{get_content_props assign=content_props} ... {$content_props}
$content_props . page_alias_of_a_page_you_got_the_properties_from . the_props_name . prop_name (will print out the prop name)
$content_props . page_alias_of_a_page_you_got_the_properties_from . the_props_name . data (will print out the prop data)
Notice: since smarty don't like a minus in a variable name the UDT changes "-" to "_" of the page alias.
E.g:
To access all properties using a foreach-loop you need two nested loops:Code: Select all
{get_content_props assign_as_array=content_props} {$content_props.home.content_en.data} {$content_props.home.target.data} {$content_props.home.extra1.data}
I hope this will be usefull.Code: Select all
{get_content_props assign_as_array=pages} {foreach from=$pages item=page} ID: {$page.content_id}<br /> ALIAS : {$page.content_alias}<br /> {foreach from=$page item = prop} {$prop.prop_name}: {$prop.data}<br /> {/foreach} {/foreach}