Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
fxv_cms
Forum Members
Forum Members
Posts: 13
Joined: Wed Sep 03, 2008 7:47 am

Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by fxv_cms »

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
alby

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by alby »

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.
What you stored this value?

Alby
fxv_cms
Forum Members
Forum Members
Posts: 13
Joined: Wed Sep 03, 2008 7:47 am

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by fxv_cms »

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
NaN

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by NaN »

Try this:

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;
        }
}

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.:

Code: Select all

{foreach from=$content_props item=prop}
{$prop->FIELDNAME}<br />
{/foreach}
You also can access the props directly using

Code: Select all

{$content_props_FIELDNAME}

in your template.

Hope that helps.
fxv_cms
Forum Members
Forum Members
Posts: 13
Joined: Wed Sep 03, 2008 7:47 am

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by fxv_cms »

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
NaN

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by NaN »

fxv_cms wrote: 1) Thank Alby - I tried the code you suggested, but without success : it provides some field names but not field values.
I don't understand... i believe i'm confused... no, wait! Maybe i'm not.

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.

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...
I expected the same thing...

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"
So, that is why my code doesn't work properly for your purposes.
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;
        }
}

Same procedure as before.
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} 
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 content_id="15"} 
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" separator=","} 
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 fields="extra1,extra2"} 
This will provide smarty variables that you can access after calling the UDT:

Code: Select all


{get_content_props smarty=true}

{$content_props.content_en.data}
{$content_props.target.data}
{$content_props.extra1.data}

or this one:

Code: Select all


{get_content_props smarty=true}

{foreach from=$content_props item=prop}
{$prop.fieldname}:{$prop.data}
{/foreach}

I hope this time it is what you're looking for.
fxv_cms
Forum Members
Forum Members
Posts: 13
Joined: Wed Sep 03, 2008 7:47 am

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by fxv_cms »

That's great : it's exactly what I wanted !

Thank you very much NaN for your help

François
Duketown

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by Duketown »

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
nicmare
Power Poster
Power Poster
Posts: 1150
Joined: Sat Aug 25, 2007 9:55 am
Location: Berlin

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by nicmare »

awesome udt nan!
this should be integrated into cmsms core!
thanks alot
lainyrache
Forum Members
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'

Post by lainyrache »

Hi
Excellent tag. Thank you ;D

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
NaN

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by NaN »

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}

lainyrache
Forum Members
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'

Post by lainyrache »

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}

;D ;D ;D
NaN

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_

Post by NaN »

lainyrache wrote: Thank you! Absolutely bloomin champion..
just needed to add {}  in the div..
Doh!

:D
User avatar
gemini
Forum Members
Forum Members
Posts: 19
Joined: Mon Nov 17, 2008 4:17 pm

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by gemini »

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;
        }
}
Tried to improve this code. But it doesn't seem to work for me.. any thoughts, what i have made wrong?
NaN

Re: Create a user-defined tag to retrieve a field value from table 'cms_content_pro'

Post by NaN »

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:

Code: Select all

{get_content_props}
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 content_id="15"}
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" prop_separator=","} 
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 props="extra1,extra2"}
This will provide a smarty variable named "content_props" that you can access after calling the UDT:

Code: Select all


{get_content_props assign=content_props}

...

{$content_props}

To access the properties directly use the dot-syntax:
$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:

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}
To access all properties using a foreach-loop you need two nested loops:

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}

I hope this will be usefull.
Post Reply

Return to “Developers Discussion”