We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44437
    • 74 Posts
    Hello community,

    I'm using migx with the latest version of modx revo to build out a faqs page. Currently, my MIGX (named faqs) has 3 fields. These are:

    • question
    • answer
    • published


    The tpl that I will be using the MIGX has a thumbs up and thumbs down counter. Example below


    Is it possible to create an additional 2 fields (yes and no) in Migx and increment those fields' values when either the yes and no button is clicked? If yes, how would I go about doing this?

    I do welcome any suggestions or recommendations.
      • 4172
      • 5,888 Posts
      I think, I would go a step further, and create a custom-table for the faqs.
      Then its easy to set the yes/no - click-counts at the faq - record.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44437
        • 74 Posts
        Ok, I have created my custom table like you had suggested. The 'yes' and 'no' are of type integer. How would I increment those fields' values from the front end of the website?
          • 4172
          • 5,888 Posts
          do you have created a xpdo-schema for the table?
          If so, could you post it here?
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 44437
            • 74 Posts
            Sure no problem

            <?xml version="1.0" encoding="UTF-8"?>
            <model package="faqs" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
             <object class="faqs" table="faqs" extends="xPDOSimpleObject" >
             <field key="question" dbtype="varchar" precision="255" phptype="string" null="false" default="" index="index" />
             <field key="answer" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
             <field key="yes" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
             <field key="no" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
             <field key="published" dbtype="tinyint" precision="1" attributes="unsigned" phptype="integer" null="false" default="" />
             </object>
            </model>
              • 4172
              • 5,888 Posts
              I think, something like that should work:

              <?php
              $packageName = 'faqs';
              $packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
              $modelpath = $packagepath . 'model/';
              $prefix = null;
              if (is_dir($modelpath)) {
                  $modx->addPackage($packageName, $modelpath, $prefix);
              }
              $classname = 'faqs';
              
              $object_id = (int) $modx->getOption('object_id',$_GET,0);
              $add_yes = (int) $modx->getOption('add_yes',$_GET,0);
              $add_no = (int) $modx->getOption('add_no',$_GET,0);
              
              $field = !empty($add_yes) ? 'yes' : '';
              $field = !empty($add_no) ? 'no' : $field;
              
              if (!empty($field) && !empty($object_id) && $object = $modx->getObject($classname,$object_id)){
                  $count = $object->get($field) + 1;
                  $object->set($field,$count);
                  $object->save();    
              }
              
              return '';
              


              with buttons like

              <a href="[[~[[*id]]? &object_id=`[[+id]]` &add_yes=`1`]]">Yes</a>
              <a href="[[~[[*id]]? &object_id=`[[+id]]` &add_no=`1`]]">No</a>


              you might also want to set a cookie or SESSION - var and check, if the user allready has clicked the button.

              everything untested! [ed. note: Bruno17 last edited this post 10 years, 11 months ago.]
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!