business of the day rss help required please

4 posts by 3 authors in: Forums > CMS Builder
Last Post: January 10, 2019   (RSS)

By willydoit - January 8, 2019

Hi all,

I have created a rss feed to select a random business as the business of the day, the idea being that I want to create an IFTTT applet to post the rss feed to a facebook page once a day

I can get everything working so far as the rss page and the IFTT side of things however I only want the business-of-day-rss.php to run once a day, I dont want the content to change each time the page is accessed.

How can I achieve it so that;

a) The page is automatically run at a specific time each day?

b) once run the page values stay the same until the page is recreated by the above schedule

I have included an example of my rss code below in case it helps.

Thanks in advance for any help provided.



  // load records from 'advertisers'
  list($advertisersRecords, $advertisersMetaData) = getRecords(array(
    'tableName'   => 'advertisers',
    'limit'       => '1',
    'orderBy'     => 'RAND()',
    'loadUploads' => false,
    'allowSearch' => false,
	  'where' => 'listing LIKE "%Standard Listing%"',
  ));

?>
<?PHP
function maxWords($textOrHtml, $maxWords) {
$text=str_replace("<p>","*P*",$textOrHtml);
$text= str_replace("</p>","*/P*",$text);
$text = strip_tags($text);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="...</p>";

return $output;
}
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Business of the Day</title>
    <link>https://www.mysite.net</link>
    <description>Business of the Day</description>
    <pubDate><?php echo date('r') ?></pubDate>
    <language>en-us</language>

    <?php foreach ($advertisersRecords as $record): ?>
    <item>
      <title><?php echo htmlencode($record['title']) ?></title>
      <link>https://<?php echo $_SERVER['HTTP_HOST']; ?>/<?php echo $record['_link'] ?></link>
      <description>  <![CDATA[<?php echo maxWords ($record['description'],60) ?><br> <a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/<?php echo $record['_link'] ?>" target="_blank"> Read more..</a>]]></description>
      <pubDate><?php echo date('r', strtotime($record['createdDate'])) ?></pubDate>
      
    </item>
    <?php endforeach ?>
  </channel>
</rss>

By daniel - January 9, 2019

Hi willydoit,

As far as I'm aware, there's not strictly a way to get a PHP script to only "run" once per day; by its nature, it will execute every time it gets accessed. Instead what you'll need to do is store some information each day so you can produce a consistent result.

My recommendation would be to create a new CMSB section called "Business of the Day", which can store a business's num. Each time you access business-of-the-day.php, check if there's a record in this section that was created on today's date. If yes, retrieve the stored num and use it to get the data from the advertiser table. If no, select a random business and use that data, and also insert a record into the "Business of the Day" section to save the num.

Let me know if you have any questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By willydoit - January 10, 2019

Hi Jerry,

not being a programmer I have used the cookbook for a number of solutions over the years, I will take a look and see if it could be adapted by someone with my knowledge level which is pretty much cut and paste and hope for the best :-)