We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23178
    • 22 Posts
    I am trying to figure out how to do a simple database query and loop over the result, normally I would do something like below, how do I do it in Revo? I have spent a ton of time going over the docs and I am lost. I have a template variable that puts a 0 or 1 into its value.

    I just want to do something like this:

    $output = ’’;
    @ $db = new mysqli(’localhost’,’root’,’root’, ’db’);
    if (mysqli_connect_errno()) {
    echo ’Could not connect to database’;
    exit;
    }
    $query = "SELECT site_tmplvar_contentvalues.tmplvarid, site_tmplvar_contentvalues.contentid, site_tmplvar_contentvalues.value
    FROM site_tmplvar_contentvalues
    WHERE value = 1
    AND tmplvarid = 19";
    $result = $db->query($query);
    $numrow = $result->num_rows;
    if($numrow) {
    for($i=0;$i<$numrow;$i++) {
    $row = $result->fetch_assoc();
    $ouput = $row[’contentid’];
    }
    }
    echo $output;

      • 3749
      • 24,544 Posts
      Are you connecting to the MODx DB or another one?

      The MODx DB is always connected, so that’s unnecessary.

      For another one, the best way is to create xPDO classes for it, instantiate a new xPDO object, and use xPDO for the queries.

      See this:
      http://bobsguides.com/custom-db-tables.html

      and this:
      http://bobsguides.com/revolution-objects.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 23178
        • 22 Posts
        I am just connecting to the modx DB. So I am just trying to figure out how to use the api to accomplish what I need to. I have gone though the api docs but there are no examples, so I am not sure what to use for this purpose. I was guessing it had something to do with modTemplateVarResource_mysql. But like I said I have no idea what to do with that.

        I was thinking something similar to this but for TVs?

        $chunks = $modx->getCollection(’modChunk’);
        foreach ($chunks as $chunk) {
        echo $chunk->get(’name’)."
        \n";
        }
        but what if I just want to run something like I first posted. This wouldn’t really be accomplishing the same thing. I am just not sure. huh
          • 3749
          • 24,544 Posts
          What information, exactly, do you want?
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 23178
            • 22 Posts
            I have a TV to decide if that resource should be summarized and listed out on another page (featured yes or no), it stores a 1 or 0. So I need to look in the tmplvar_contentvalues table look for all the tmplvarid’s that = 19, see if it has a value of 1, loop through the results to get its contentid so I can add it to the summary page.

            Doing that query outside of MODX is pretty straight forward. Just don’t know how to accomplish it here.

            I went and looked at the links you sent and I am guessing I do something like
            this
            $tvs = $modx->newQuery(’modTemplateVar’);
            $tvs->where(array(’tmplvarid’ => 19, ’value’ => 1));

            or this?
            $tv = $modx->getObject(’modTemplateVar’,array(’tmplvarid’ => 19, ’value’ => 1));
            $id = $tv->getValue(’contentid’);

            thanks for your help.
              • 3749
              • 24,544 Posts
              The easiest way is:

              $val = $modx->resource->getTVValue(19);


              Or using the name of the TV:

              $val = $modx->resource->getTVValue('MyTv');


              This would work also,
                $tv = $modx->getObject('modTemplateVar',19);
                $val = $tv->getValue($modx->resource->get('id'));
              


              If you want the value of the TV in another resource:

              $tv = $modx->getObject('modTemplateVar',19);
                $val = $tv->getValue($resourceId);
              




                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 23178
                • 22 Posts
                Thanks Bob,

                I will give this a go. Thanks again for the help.
                  • 23178
                  • 22 Posts
                  So I thought I would report back that when I was looking in the getResources source to find out how to do a few things for this snippet, I realized I was building something already there all along in getResources. I just needed to use the tvFilters.

                  One strange thing about this is that when I tried to use a drop down that was a yes or no storing a 0 or 1 it didn’t work. If I use a text field that yes or no was typed in it did work. Odd, not sure why thats the case.

                  Either way, this is the solution I needed. Would be nice if I could use that drop down though.
                    • 4172
                    • 5,888 Posts
                    can you show us your complete code what you did try:


                    • input-option-values for dropdown-tv
                    • chunk for getResources-tpl
                    • getResources snippet-call


                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 23178
                      • 22 Posts
                      Drop down was
                      Do not feature==0||Feature==1

                      template is
                      <li class="product_block">
                          <h5>[[+pagetitle]]</h5>
                          <p>[[+introtext:limit=`240`]]</p>
                          <img src="assets/snippets/phpThumb/phpThumb.php?src=/[[+tv.product_image]]&w=100" alt="Product Name"/>
                           
                            <form method="post" action="contact-us.html"name="order_btn">
                                    <input type="hidden" value="[[+tv.product_id]]" name="id" />
                                    <input type="hidden" value="[[+pagetitle]]" name="name" />
                                    <input type="image" src="assets/templates/img/btns/order-button.jpg" name="submit" />
                            </form>
                            <a href="[[+tv.product_image]]" class="button enlarge">enlarge photo</a>
                            <p class="product_id">
                                product id: [[+tv.product_id]]
                            </p>
                      </li>
                      


                      Call was
                      [[!getResources? &tpl=`product_List_tpl` &parents=`[[*id]]` &includeTVs=`1` &processTVs=`1` &tvFilters=`is_feature==1` ]]

                      not sure if I need both includeTV and processTV in there.