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

    I am trying to create a plugin that will set the three most recently posted news articles to my modx site. I am currently not getting any output.

    If someone could help me that would be great! Or if there is another way to achieve this that will take less work please let me know.

    Below is the code for my plugin that runs on WebPagePrerender.

    /* This plugin sets a placeholder that displays the three latest articles to be posted to my modx site.
     * Author: Sarah Vardy
     * Date: Wed 7th November, 2007
    */
    
    //This plugin that checks the database to see what articles have been posted the most recently
    
    //get the timestamp from the server
    if(!isset($timestamp)) {
        $timestamp=time();
    }
    
    
    //select the modx table to use
    $table = $modx->getFullTableName("site_content");
    
    
    //An array to store the new articles that have been created 
    $newArticlesList = new array();
    
    //An array to store the amount of time since the article has been published
    $newArticlesTime = new array();
    
    //A limit to limit the size of the array by 
    $maxArrayLength = 3;
    
    //Index incrementer for the newArticlesList array
    $indexIncrementer = 0;
    
    //Most recent article
    $recentArticleOne = '';
    
    //Second most recent article
    $recentArticleTwo = '';
    
    //Third most recent article
    $recentArticleThree = '';
    
    //iterate through the table and see how close the articles createdBy time is to the timestamp 
    $result = $modx->db->select('*', $table); 
    
    //while there are still more rows in the table check to see whether the document is published
    //if the value stored at the array index equals NULL store the 
    while ($r = $modx->db->getRow($result))   
    { 
         $createdby = $r['createdby'];
         $published = $r['published'];
         $publishedon = $r['publishedon'];
         
         //if published does not equal false
         if ($published != 0)
         {
            //subtract the current timestamp from the publishedon date and store it in a temporary variable
            $tempTimeStore = $publishedon - $timestamp;
    
            //if the value stored in the newArticlesList array at the index of indexIncrementer equals NULL
            if ($newArticlesList($indexIncrementer) == NULL)
            {
                    //store createdby in the newArticlesList array at the index of indexIncrementer
                    $newArticlesList($indexIncrementer) = $createdby;
    
                    //store createdby in the newArticlesTime array at the index of indexIncrementer
                    $newArticlesTime($indexIncrementer) = $tempTimeStore;
                    
                    //if indexIncrementer equals 2 then reset it to 0
                    if ($indexIncrementer == 2)
                    {
                            //reset indexIncrementer to 0 
                            $indexIncrementer = 0;
                            //return to the while loop
                            return;
                    }
                    else
                    {
                            //increment indexIncrementer by 1
                            $indexIncrementer++;
                            return;
                    }
                    
            }
            else if ($newArticlesList($indexIncrementer) != NULL)
            {
                    
                  //iterate through the array of values stored in the newArticlesTime array
                  for ($i = 0; $i < $maxArrayLength; $i++)
                  {
                          //if the value stored in the newArticlesTime array is more than array the value of tempTimeStore
                          if ($newArticlesTime($i) > $tempTimeStore)
                          {
                                  //replace the value stored in both arrays at this index with the current result 
                                  $newArticlesList($i) = $createdby;
                                  $newArticlesTime($i) = $tempTimeStore;
                                  return;
    
                          }
                          else
                          {
                                  return;
                          }
    
                  }
    
            }
    
         }
    }//end while
    
    //set variables to set modx placeholders
    $recentArticleOne = $newArticlesList(0);
    $recentArticleTwo = $newArticlesList(1); 
    $recentArticleThree = $newArticlesList(2); 
    
    
    //set modx placeholders
    $modx->setPlaceHolder('recentArticleOne', $recentArticleOne);
    $modx->setPlaceHolder('recentArticleTwo', $recentArticleTwo);
    $modx->setPlaceHolder('recentArticleThree', $recentArticleThree);
    
    
    
    
    
    
    
    


    Thanks in advance,

    Sarah
      • 26182
      • 164 Posts
      Ok I noticed a few little errors with square brackets and stuff but this still isn’t helping. Here’s the code again:

      /* This plugin sets a placeholder that displays the three latest articles to be posted to my modx site.
       * Author: Sarah Vardy
       * Date: Wed 7th November, 2007
      */
      
      //This plugin that checks the database to see what articles have been posted the most recently
      
      //get the timestamp from the server
      if(!isset($timestamp)) {
          $timestamp = time();
      }
      
      
      //select the modx table to use
      $table = $modx->getFullTableName("site_content");
      
      
      //An array to store the new articles that have been created 
      $newArticlesList = new array();
      
      //An array to store the amount of time since the article has been published
      $newArticlesTime = new array();
      
      //A limit to limit the size of the array by 
      $maxArrayLength = 3;
      
      //Index incrementer for the newArticlesList array
      $indexIncrementer = 0;
      
      //Most recent article
      $recentArticleOne = '';
      
      //Second most recent article
      $recentArticleTwo = '';
      
      //Third most recent article
      $recentArticleThree = '';
      
      //iterate through the table and see how close the articles createdBy time is to the timestamp 
      $result = $modx->db->select('*', $table); 
      
      //while there are still more rows in the table check to see whether the document is published
      //if the value stored at the array index equals NULL store the 
      while ($r = $modx->db->getRow($result))   
      { 
           $createdby = $r['createdby'];
           $published = $r['published'];
           $publishedon = $r['publishedon'];
           
           //if published does not equal false
           if ($published != 0)
           {
              //subtract the current timestamp from the publishedon date and store it in a temporary variable
              $tempTimeStore = $publishedon - $timestamp;
      
              //if the value stored in the newArticlesList array at the index of indexIncrementer equals NULL
              if ($newArticlesList[$indexIncrementer] == NULL)
              {
                      //store createdby in the newArticlesList array at the index of indexIncrementer
                      $newArticlesList[$indexIncrementer] = $createdby;
      
                      //store createdby in the newArticlesTime array at the index of indexIncrementer
                      $newArticlesTime[$indexIncrementer] = $tempTimeStore;
                      
                      //if indexIncrementer equals 2 then reset it to 0
                      if ($indexIncrementer == 2)
                      {
                              //reset indexIncrementer to 0 
                              $indexIncrementer = 0;
                              //return to the while loop
                              return;
                      }
                      else
                      {
                              //increment indexIncrementer by 1
                              $indexIncrementer++;
                              return;
                      }
                      
              }
              else if ($newArticlesList[$indexIncrementer] != NULL)
              {
                      
                    //iterate through the array of values stored in the newArticlesTime array
                    for ($i = 0; $i < $maxArrayLength; $i++)
                    {
                            //if the value stored in the newArticlesTime array is more than array the value of tempTimeStore
                            if ($newArticlesTime[$i] > $tempTimeStore)
                            {
                                    //replace the value stored in both arrays at this index with the current result 
                                    $newArticlesList[$i] = $createdby;
                                    $newArticlesTime[$i] = $tempTimeStore;
                                    return;
      
                            }
                            else
                            {
                                    return;
                            }
      
                    }
      
              }//end if /else
           }
           else
           {
              return;
           }//end if
      }//end while
      
      //set variables to set modx placeholders
      $recentArticleOne = $newArticlesList[0];
      $recentArticleTwo = $newArticlesList[1]; 
      $recentArticleThree = $newArticlesList[2]; 
      
      
      //set modx placeholders
      $modx->setPlaceHolder('recentArticleOne', $recentArticleOne);
      $modx->setPlaceHolder('recentArticleTwo', $recentArticleTwo);
      $modx->setPlaceHolder('recentArticleThree', $recentArticleThree);
      
      
      
      
      
      
      
        • 26182
        • 164 Posts
        I replaced the return statement with a break and then took both alternatives our altogether in a new snippet. No change... sad
          • 26182
          • 164 Posts
          This is the snippet that I wrote for it, and still no result:

          <?php
          /* This snippet displays the three latest articles to be posted to the RCS IT Newsletters site.
           * Author: Sarah Vardy
           * Date: Wed 7th November, 2007
           * Research Computing Services, Griffith University, Nathan QLD
          */
          
          //This plugin that checks the database to see what articles have been posted the most recently
          
          //get the timestamp from the server
          if(!isset($timestamp)) {
              $timestamp = time();
          }
          
          
          //select the modx table to use
          $table = $modx->getFullTableName("site_content");
          
          
          //An array to store the new articles that have been created 
          $newArticlesList = new array();
          
          //An array to store the amount of time since the article has been published
          $newArticlesTime = new array();
          
          //A limit to limit the size of the array by 
          $maxArrayLength = 3;
          
          //Index incrementer for the newArticlesList array
          $indexIncrementer = 0;
          
          //Most recent article
          $recentArticleOne = '';
          
          //Second most recent article
          $recentArticleTwo = '';
          
          //Third most recent article
          $recentArticleThree = '';
          
          //iterate through the table and see how close the articles createdBy time is to the timestamp 
          $result = $modx->db->select('*', $table); 
          
          //while there are still more rows in the table check to see whether the document is published
          //if the value stored at the array index equals NULL store the 
          while ($r = $modx->db->getRow($result))   
          { 
               $createdby = $r['createdby'];
               $published = $r['published'];
               $publishedon = $r['publishedon'];
               
               //if published does not equal false
               if ($published != 0)
               {
                  //subtract the current timestamp from the publishedon date and store it in a temporary variable
                  $tempTimeStore = $publishedon - $timestamp;
          
                  //if the value stored in the newArticlesList array at the index of indexIncrementer equals NULL
                  if ($newArticlesList[$indexIncrementer] == NULL)
                  {
                          //store createdby in the newArticlesList array at the index of indexIncrementer
                          $newArticlesList[$indexIncrementer] = $createdby;
          
                          //store createdby in the newArticlesTime array at the index of indexIncrementer
                          $newArticlesTime[$indexIncrementer] = $tempTimeStore;
                          
                          //if indexIncrementer equals 2 then reset it to 0
                          if ($indexIncrementer == 2)
                          {
                                  //reset indexIncrementer to 0 
                                  $indexIncrementer = 0;
                                  //return to the while loop
                                  //return;
                                  //break;
                          }
                          else
                          {
                                  //increment indexIncrementer by 1
                                  $indexIncrementer++;
                                   //return;
                                   //break;
                          }
                          
                  }
                  else if ($newArticlesList[$indexIncrementer] != NULL)
                  {
                          
                        //iterate through the array of values stored in the newArticlesTime array
                        for ($i = 0; $i < $maxArrayLength; $i++)
                        {
                                //if the value stored in the newArticlesTime array is more than array the value of tempTimeStore
                                if ($newArticlesTime[$i] > $tempTimeStore)
                                {
                                        //replace the value stored in both arrays at this index with the current result 
                                        $newArticlesList[$i] = $createdby;
                                        $newArticlesTime[$i] = $tempTimeStore;
                                         //return;
                                         //break;
          
                                }
                                else
                                {
                                         //return;
                                         //break;
                                }
          
                        }
          
                  }//end if /else
               }
               else
               {
                  //return;
                  //break;
               }//end if
          }//end while
          
              for ($j = 0; $j < 3; $j++)
              {
                  print $newArticlesList[$j];
                  print "<br>";
              }
          
          ?>
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Um... why not use Ditto, give it the IDs of all your news article containers, and have it sort by create date, and only show 3 (which it does by default anyway)?
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 7231
              • 4,205 Posts
              Did you select a system event to trigger the plugin?
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don&#39;t know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 26182
                • 164 Posts
                yeah i just discovered that i can use newsmanager lol

                is there any existing stuff for showing the three hottest items in css star rating? ie snippet or something?

                lipsrsealed