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

    I have a calendar on all pages and i want to highlight dates where news was posted(using Articles plugin).

    For now i'm getting all resources with same parent(Articles). Then i iterate over it and convert "publishedon" timestamp to date on wich it was published and push it to array and convert to json.
    Then on the browser i use this array to highlight dates in jQuery datepicker.

    This is the snippet i use.
    <?php
    $c = $modx->newQuery('modResource');
    $c->select(array('id','publishedon'));
    $c->where(array(
        'parent' => 12,
        'published' => true,
        'deleted' => false
    ));
    $resources = $modx->getCollection('modResource',$c);
    $output = array();
    foreach ($resources as $resource) {
        $publishedon = $resource->get('publishedon');
        $date = new DateTime($publishedon);
        $monthYear = date_format($date, "mY");
    	$day = date_format($date, "d");
        $output[$monthYear][] = $day;
    }
    return $modx->toJSON($output);

    Am i doing this right? Are this going to be only one single query for all resources or it will be many querys for each single resource?
    I know that if i get many news it will slow down my server but the only option i see here is to fetch resources monthly and send them by ajax.. and i dont know how to do this... = )

    Any help wuld be nice.
    Thank you. [ed. note: brazhnyk_yuriy last edited this post 14 years, 1 month ago.]
      • 39932
      • 483 Posts
      Am i doing this right?

      This is a very viable solution. Your query is made well and does exactly what you want it to. Gets the resources under a parent.

      Are this going to be only one single query for all resources or it will be many querys for each single resource?

      Your query will get multiples. How often it is called is dependent on the calling code/HTML/AJAX.

      I know that if i get many news it will slow down my server ...

      Only if you are getting it many times or don't put a limit on the data. You can limit the set (if you want) by adding $c->limit(#); where # is the number of resources you want to return.

      ... the only option i see here is to fetch resources monthly and send them by ajax.. and i dont know how to do this

      AJAX is actually quite simple, and can be even easier with MODx. There are lots of published techniques and tutorials out there. Learning AJAX is more about learning how to make page requests while you are still on a page. The JS is going to be similar almost every time. In MODx, it is as simple as making a partial HTML resource as having your JS request that page specifically. When it returns the results, then you process them.

      A lot of this is demonstrated in this tutorial. The techniques in this tutorial may be transferred and applied to a lot of different sites and other methods. This may require that you read the other tutorials attached to it, but you'll spend more time reading than actually doing the work. All said and told, you may have an AJAX setup in less than 30 min. This means that you can make a single AJAX page very quickly and easily. These won't tell you everything about AJAX, but may give you enough information to move forward.
        Website: Extended Dialog Development Blog: on Extended Dialog
        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
        • 40905
        • 7 Posts
        Thank you.
        Quote from: fuzzicallogic at Sep 02, 2012, 05:56 PM
        Are this going to be only one single query for all resources or it will be many querys for each single resource?
        Your query will get multiples. How often it is called is dependent on the calling code/HTML/AJAX.

        I know that if i get many news it will slow down my server ...
        Only if you are getting it many times or don't put a limit on the data. You can limit the set (if you want) by adding $c->limit(#); where # is the number of resources you want to return.

        What i mean by multiple querys: Is it going to be ONLY one query to database or my code will make multiple query for each resource because of some sort of "lazy loading".
        And i cant use limit becouse i nead to fetch ALL resources to make it ALL available in browser calendar.
        Again, thank you for your help.
          • 39932
          • 483 Posts
          Is it going to be ONLY one query to database or my code will make multiple query for each resource...

          I maintain: How often it is called is dependent on the calling code/HTML/AJAX.

          The deal is this. If you have an automated function that runs every time you "hover", it's called multiple times. If you have it run once onLoad, then it is only called once. It is all dependent on your code.

          Using jQuery, you can add a script to your page, and this script is only run once. This is done similar to the following (also includes your basic AJAX call)

          <script type="text/javascript">
          $(document).ready(function()
          {   $.ajax(
              {   type:'GET',
              // Replace /path/to/resource/
                  url:'/path/to/resource/',
                  success:function(data)
                  {//Replace divToAddTo with CSS ID
                      $('#divToAddTo').html(data);
                  }, 
                  error:function(XMLHttpRequest, textStatus, errorThrown)
                  {//Replace divToAddTo with CSS ID-- For debugging...
              	    $('#divToAddTo').html(errorThrown + ': ' + this.url);
                  }, 
              	dataType:'html'
          	});
          });
          </script>
          


          The above function only runs on the document load. This means it will only run whenever you refresh the page. It is a basic loader that has no additional functionality and simply adds the content of an AJAX Resource to the page. You would place this script element after the close of the HTML Element that you want to replace. [ed. note: fuzzicallogic last edited this post 14 years, 1 month ago.]
            Website: Extended Dialog Development Blog: on Extended Dialog
            Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
            Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

            Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
            • 40905
            • 7 Posts
            Yeah, i definately understand that if i invoke my snippet multiple times without caching my query will be executed multiple times. This is obvious. But i read somwhere in query documentation about some "magic" "lazy loading" technique that will load resources only if you invoke ->get() on the actual resource. So if i doing loop over 500 resources it will spawn 500 DB querys??? This is what i try to clarify...

            Thanks for your time. I realy appriciate your help.
              • 39932
              • 483 Posts
              You're welcome.

              ... some "magic" "lazy loading" technique that will load resources only if you invoke ->get()...

              These are non-Resource based AJAX Requests. If you make a single page with nothing but that Snippet call with a Template that has nothing but [ [*content] ]... and then make adjust the AJAX code I gave you to that URL, you don't have to worry about invoke->get(). Invoke->get() is for linking to "connectors". This is not a "connector". Its a real page with a real URL that returns real HTML (just not fully-templatized HTML). Again, the tutorial I linked above demonstrates this. Here it is again: AJAX Login

              So if i doing loop over 500 resources it will spawn 500 DB querys??

              This has nothing to do with AJAX. This is your Snippets are loaded every time the calling Resource is loaded... no more, no less. Your Query Snippet handles all of the child resources in one call.

              Note: Connectors are for the back-end Manager, not for a your front-end site. I have parameterized AJAX on my site (very very very fast) that uses no such trick. All it does is asks for a real "web page". This is the technique that my AJAX Login uses. Very simple for those who are learning how to perform AJAX in MODx. Highly extensible for many ways to do many other things.

              Further reading:
                Website: Extended Dialog Development Blog: on Extended Dialog
                Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                • 39932
                • 483 Posts
                I should mention that AJAX Login uses an additional technique with Cross Context resourcing. This isn't necessary for what you want. However, it has other benefits that you might (or might not) find useful.
                  Website: Extended Dialog Development Blog: on Extended Dialog
                  Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                  Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                  Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                  • 40905
                  • 7 Posts
                  Thank you for AJAX guide... Realy helpful...

                  Your Query Snippet handles all of the child resources in one call.
                  This is true but not answering my question. It will handle all resources in one snippet call wich is obvious. But will it use only one DB query in one snippet call?
                  Sorry if i asking dumb questions but i still not sure about this. Please can you clarify...

                  And may be you know some profiling tool for MODx... To know how many db qurys per page and how many time it took... etc etc
                  [ed. note: brazhnyk_yuriy last edited this post 14 years, 1 month ago.]
                    • 39932
                    • 483 Posts
                    Brazhnyk,

                    This is true but not answering my question.

                    I am answering the question you are asking. If that answer is not acceptable for you, then there is some other question you are seeking an answer to that is not coming across correctly. This is something that you need to address before asking again, not I. I will rephrase to make sure that you understand as best as possible.

                    One Query call, per Snippet Call per URL Request. Its as simple as that, and that will always be the answer, period. The only time this is not the case is when the page Snippet is calling a Query multiple times because you or someone else coded it that way. It's all going to come down to your AJAX and how you perform the URL Request (which I stated above). If your "parent" page only makes one URL Request. The answer is 1. If it does more, the answer is "more". The techniques I pointed you to only make one such call.

                    But will it use only one DB query in one snippet call?

                    Your Query only issues one query call and this is all that is important (based on the code you have provided. There is no way anyone on these forums can answer for yours or other Snippet, any other call, any other site code, without having the complete code, themselves. Other factors may include the browser (if it uses a technique that is nonstandard), the host (which may re-issue requests if it chooses), the caching method (which may actually reduce below 1 or increase it way above 1). Here's what we can tell you without knowing anything else about your site code.

                    There is the call to get the requested URL (this may be more than one call). Then the call for every associated MODx tag. If the tag uses other tags, there may be calls for each one of those. If the tag is a Snippet and performs a query, then there is another there. If the Snippet calls another Snippet, hence another.call,etc.

                    The issue isn't how many calls there are (and never is). As a database applications programmer with over 16 years experience, the question is always how many unnecessary calls there are. How much unused information is coming from the database. It doesn't matter if there is only one call, if that call returns a huge amount of data that you don't need. That's why you can $q->select() and $q->limit() to begin with.

                    And may be you know some profiling tool for MODx...

                    I know of no such tools, but I don't use profiling. I've never had a need in 16 years (and probably never will, but I'm crazy). I know my code and use techniques that instantly tell me what is going on with my code if something goes awry. The other gurus may be aware of such and they might chime in. [ed. note: fuzzicallogic last edited this post 14 years, 1 month ago.]
                      Website: Extended Dialog Development Blog: on Extended Dialog
                      Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                      Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                      Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
                      • 39932
                      • 483 Posts
                      Final Note: My site uses "lazy loading". It runs each snippet once per URL request. My snippets have a variable number of query calls (one as many as 15 query calls). This doesn't matter because I get as much data as I need. No more, no less. They are very fast calls.
                        Website: Extended Dialog Development Blog: on Extended Dialog
                        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".