Forum for discussing general topics related to Couch.
5 posts Page 1 of 1
Hi everybody,

I'm trying to set some persistent variables but I didn't figure out how to do that with Couch.
I know how to use DataBound Forms to record some data from a form, but here I'm trying to automatically update a field on a clonable template, with no action from the user.

I'm trying to set up a kind of URL shortener, let's say a "redirector" that will record referrer and counts. I want to know how many people used the shortened URL and from which referrer URL they come from.
Each shortened URL is a unique page of the clonable template. Here what I've got for now :
Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title="URL Shortener" clonable="1">
  <cms:editable name="redirect_to" type="text"/>
  <cms:editable name="counter" label="Counter" type="text"/>
  <cms:editable name="referrers" label="Referrers" type="text"/>
</cms:template>
<cms:if k_is_page>
  <cms:redirect url=redirect_to permanently="1" />
</cms:if>
<?php COUCH::invoke(); ?>


The "name" of the page represents my shortened URL. When the page is called, the user is correctly redirected.

Do you know how to update "counter" and "referrers" for each call of this page?
Or perhaps do you a better way to do what I want?

Thanks.
Hello and welcome, @spidou :)

Since you are conversant with DataBound Forms, you already know that it is the cms:db_persist_form tag that saves the submitted values into the bound fields.

The cms:db_persist_form, incidentally, has a little-known sister tag named cms:db_persist that works without a form.

It accepts fields to be updated as direct parameters where the name of the parameter is the field-name.

The values, as with all tags, can be literal strings, variables or code (regular single quotes, no quotes, double quotes thing).

To disambiguate this tag's self parameters and those representing the fields to be updated,
the bonafide parameters are prefixed by underscore (see example below).

db_persist tag (as opposed to db_persist_form tag) requires specifying the masterpage.

Code: Select all
<cms:db_persist 
    _masterpage=k_template_name
    _mode='create'
    _separator='|'
    _invalidate_cache='0'
    _auto_title='0'
   
    k_page_title='test'
    my_text='hello'
    min_amount='423'
    k_publish_date='2009-05-03'
    to_movies='35,26'
    my_categories='|one|two|three'


    <cms:if k_error >
        <font color='red'>ERROR:
        <cms:each k_error >
            <cms:show item /><br>
        </cms:each>
        </font>
    <cms:else />
        <cms:show k_last_insert_id />
        <cms:show k_last_insert_page_name />
    </cms:if>
   
   
</cms:db_persist>

I think you can use cms:db_persist to do waht you mentioned.

Do let us know if something is unclear or you require assistance with anything.

Thanks.
Hi,

I used this for my step by step form and it works like a charm.

But im having one small issue!

I placed the db_persist in last step like this: BUT THE PAGE THEN GETS SAVED 2 TIMES?! Any idea why?

Code: Select all
      <cms:if k_current_step='3' >
         <fieldset>
            <legend>Page Three (3 of 3)</legend>
            <p><label for='element_5'>Element 5:</label><cms:input type='text' name='element_5' /></p>
           
         </fieldset> 
                <cms:db_persist
    _masterpage='users/ansatt/time-test.php'
    _mode='create'
    _invalidate_cache='0'
    _auto_title='0'
    ansatte = k_user_id

    test1 = frm_element_1
    test4 = frm_element_4
    test5 = frm_element_5
   
    k_page_title='test'



    <cms:if k_error >
        <font color='red'>ERROR:
        <cms:each k_error >
            <cms:show item /><br>
        </cms:each>
        </font>
    <cms:else />
        <cms:show k_last_insert_id />
        <cms:show k_last_insert_page_name />
    </cms:if>
   
   
</cms:db_persist>
</cms:if>
Kim, maybe you need to take action only after the last page -
Code: Select all
<cms:if k_current_step gt total_pages >..

Details at viewtopic.php?p=9596#p9596

Does doing that change anything?
Thanks.

Perfect now its working correct :)
5 posts Page 1 of 1