We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43621
    • 40 Posts
    Hi all,
    Currently I'm using "Articles" to publish "weekly special offers". I already made several articles which are "unpublished" and have a date when they appear. There is always only ONE offer which is published. I made a "counter" which displays when the next offer appears. That counter isn't dynamic. The current offers always appear weekly. Now I want it to be more dynamic so the counter should automatically get the date of the next upcoming article (offer) and starts counting to this date.

    Is there a simple way to get the date of the next to publish article (maybe with getResources or so?) or do I have to make my own database-call and filter it by own?

    I hope it's clear what I want to make smiley

    I'm using Revo 2.3.1, Articles 1.7.10

    Big thx in advance!

    This question has been answered by BobRay. See the first response.

      • 43621
      • 40 Posts
      Ok I have it, but don't know if this is a nice way to get what I want:

      [[!getResources:default=`0`? &parents=`78` &showUnpublished=`1` &showHidden=`1` &tpl=`@INLINE [[+pub_date]]` &limit=`1` &sortby=`{"pub_date":"ASC"}` &where=`{"pub_date:!=":0}`]]
      • discuss.answer
        • 3749
        • 24,544 Posts
        I think this might do it a little faster (untested). It should get the resource with a future pub_date closest to today.

        [[!MyPubDate]]


        /* MyPubDate snippet */
        $query = $modx->newQuery('Article');
        
        $query->where(
            array(
                'parent' => '78',
                'pub_date:>=' => time();
            )
        );
        
        $query->sortby('pub_date', 'ASC);
        $query->limit('1');
        $query->select('pubdate');
        
        return $modx->getValue($query->prepare());


        You may get a raw timestamp back, in which case you'd want to convert it with strftime() in the snippet.
          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
          • 43621
          • 40 Posts
          Hi BobRay,
          thanks for your help. Works absolutely as it should and it directly returns the timestamp I want (before I used strToTime).


          One last question, how do I handle the return when I have a query with more than one entry?
            • 43621
            • 40 Posts
            Ok after some research and "trial and error" I got a solution for my last "problem" (it's only for learning)

            <?php
            $query = $modx->newQuery('Article');
            
            $query->where(
                array(
                    'parent' => '78',
                )
            );
            
            $query->limit('10');
            
            $resultObjects = $modx->getCollection('Article',$query);
            
            foreach($resultObjects as $entry)
            {
            	$result.= '<br> Title: '.$entry->get('pagetitle');
            }
            
            return $result;
            


            I have the feeling that this could work with less lines of code. Currently I fill "$resultObjects" with the whole database data from Article (up to the set up limit), right? So I have to explicity select the entry I want inside the foreach ($entry->get('pagetitle').

            Isn't it possible that the $resultObjects only is an array which contains the 'pagetitles'? So I could only write $entry inside the foreach.

            Thanks again.
              • 3749
              • 24,544 Posts
              I'm not sure I understand what you're doing, but if you know the pagetitle you want, you can do this:

              $query->where(
                  array(
                      'parent' => '78',
                      'pagetitle' => 'somePageTitle',
                  )
              );


              You could also do it by alias, or id (which would be more reliable, since pagetitles can change).
              You can also have an array of pagetitles and use in: to get just those pages.

              $pagetitles = array(
                  'somePageTitle',
                  'someOtherPageTitle',    
              );
              
              $query->where(
                  array(
                      'parent' => '78',
                      'pagetitle:in' => $pagetitles,
                  )
              );




              You can limit the number of fields returned with $query->select(), but you're still going to have to use $entry->get() to get a value because $entry will be an object, not an array. Using get() is the right way anyway, since some resource fields need conversion (e.g., the date fields).
                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
                • 43621
                • 40 Posts
                Ok I try to explain it different:

                The first thing you've shown me was the following:

                /* MyPubDate snippet */
                $query = $modx->newQuery('Article');
                 
                $query->where(
                    array(
                        'parent' => '78',
                        'pub_date:>=' => time();
                    )
                );
                 
                $query->sortby('pub_date', 'ASC);
                $query->limit('1');
                $query->select('pubdate');
                 
                return $modx->getValue($query->prepare());
                


                this code definitely can only return one "content" (what is the ideal solution for that problem) but now I want to know how to handle the return, if the query->limit is not 1 and I want to output every pub_date the query returns.

                /* MyPubDate snippet */
                $query = $modx->newQuery('Article');
                 
                $query->where(
                    array(
                        'parent' => '78',
                        'pub_date:>=' => time();
                    )
                );
                 
                $query->sortby('pub_date', 'ASC);
                $query->limit('10');
                $query->select('pubdate');
                
                /*
                -Where is the array in which the results are stored?
                -Does that array only contain the 'pub_date's cause I said "$query->select('pubdate');"  before?
                */
                 
                $result = ... //$result should contain every pub_date followed by "<br>"
                
                return $result;
                


                Now you said that it doesn't matter if I $query->select cause I still have to use the ->get().

                So those two snippets do exactly the same?:

                <?php
                $query = $modx->newQuery('Article');
                 
                $query->where(
                    array(
                        'parent' => '78',
                    )
                );
                 
                $query->limit('10');
                 
                $resultObjects = $modx->getCollection('Article',$query);
                 
                foreach($resultObjects as $entry)
                {
                    $result.= '<br> '.$entry->get('pub_date');
                }
                 
                return $result;


                <?php
                $query = $modx->newQuery('Article');
                 
                $query->where(
                    array(
                        'parent' => '78',
                    )
                );
                 
                $query->limit('10');
                $query->select('pub_date'); // this line only limits the "arraysize" where the whole informations are stored .. now only pub_date will be stored
                 
                $resultObjects = $modx->getCollection('Article',$query);
                 
                foreach($resultObjects as $entry)
                {
                    $result.= '<br> '.$entry->get('pub_date'); //still have to use get
                }
                 
                return $result;


                Am I right?

                  • 3749
                  • 24,544 Posts
                  Yes, though if you want the pagetitles, you'll have to make it:

                  $query->select('pagetitle,pub_date');


                  You might need to include the parent field as well, since it's part of your query. I'm not sure.


                  And don't forget the sorting:

                  $query->sortby('pub_date', 'ASC);
                    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
                    • 43621
                    • 40 Posts
                    Okay, thanks again BobRay. Much appreciated smiley