You are here:  » Display Random Items from API Call


Display Random Items from API Call

Submitted by Rocket32 on Fri, 2015-07-31 01:37 in

Hello David. How would I randomize the return results from an api call from the following code? Also, I would like to put a limit on results per page with navigation included.

<?php
  
require_once("includes/MagicParser.php");
  
$url "http://www.example.com/";
  
MagicParser_parse($url,"myLSRecordHandler","xml|COUPONFEED/LINK/");
  function 
myLSRecordHandler($record)
  {
    print 
"<tr>";
    print 
"<td width='50'>&nbsp;</td>";
    print 
"<td valign='middle'>";
    print 
"<h4>";
    print 
"<a target='_BLANK' href='".$record["CLICKURL"]."'>".$record["ADVERTISERNAME"]."</a>";
    print 
"</h4>";
    print 
"<p>";
    print 
$record["OFFERDESCRIPTION"];
    print 
"</p>";
    print 
"<h4>Coupon Code: ";
    print 
$record["COUPONCODE"];
    print 
"</h4>";
    print 
"<p> Start Date: ";
    print 
$record["OFFERSTARTDATE"];
    print 
"</p>";
    print 
"<p> End Date: ";
    print 
$record["OFFERENDDATE"];
    print 
"</p>";
    print 
"<hr>";
    print 
"</td>";
    print 
"</tr>";
  }
?>

Submitted by support on Fri, 2015-07-31 08:09

Hi,

To add pagination to the script, the easiest approach is to parse the records into a global $records array, and then display using a foreach() loop calling the original record handler function with the addition of a $counter variable, incremented at each call, and then studied to determine if the current record should be displayed based on the current $page.

With the records parsed into $records, the $navigation["resultCount"] variable can be set using count($records) and the built-in navigation HTML module html/navigation.php used to paginate the results. Have a go with something like this:

<?php
  $page 
= (isset($_GET["page"])?intval($_GET["page"]):1);
  require_once(
"includes/MagicParser.php");
  
$url "http://www.example.com/";
  
$records = array();
  
MagicParser_parse($url,"myLSRecordHandler1","xml|COUPONFEED/LINK/");
  function 
myLSRecordHandler1($record)
  {
    global 
$records;
    
$records[] = $record;
  }
  
$counter 0;
  print 
"<table>";
  foreach(
$records as $record)
  {
    
myLSRecordHandler2($record);
  }
  print 
"</table>";
  function 
myLSRecordHandler2($record)
  {
    global 
$page;
    global 
$config_resultsPerPage;
    global 
$counter;
    
$counter++;
    if (
       (
$counter <= (($page-1)*$config_resultsPerPage))
       ||
       (
$counter > ($page*$config_resultsPerPage))
       )
       return;
    print 
"<tr>";
    print 
"<td width='50'>&nbsp;</td>";
    print 
"<td valign='middle'>";
    print 
"<h4>";
    print 
"<a target='_BLANK' href='".$record["CLICKURL"]."'>".$record["ADVERTISERNAME"]."</a>";
    print 
"</h4>";
    print 
"<p>";
    print 
$record["OFFERDESCRIPTION"];
    print 
"</p>";
    print 
"<h4>Coupon Code: ";
    print 
$record["COUPONCODE"];
    print 
"</h4>";
    print 
"<p> Start Date: ";
    print 
$record["OFFERSTARTDATE"];
    print 
"</p>";
    print 
"<p> End Date: ";
    print 
$record["OFFERENDDATE"];
    print 
"</p>";
    print 
"<hr>";
    print 
"</td>";
    print 
"</tr>";
  }
  
$navigation["resultCount"] = count($records);
  require(
"html/navigation.php");
?>

To completely randomise the results but keep pagination, you can simply shuffle() $records before the display loop, so where you have this line:

  print "<table>";

...REPLACE with:

  shuffle($records);
  print "<table>";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Mon, 2015-08-03 06:18

Hey David. It works well. How do I manage this with 1 or more additional api calls added with different field values within each api calls? I believe it would be split for each api call, while adding correct field values to print from each individual api call.

Submitted by support on Mon, 2015-08-03 07:47

Hi,

One way to approach this would be a global array of "field sets", then you can just use one record handler function with a global variable set to indicate which field set to use. Rather than demonstrate in the full script above consider the following example, let me know if you're not sure how to merge with your current file of course...

<?php
  
require_once("includes/MagicParser.php");
  
$fields["feed1"]["url"]="CLICKURL";
  
$fields["feed1"]["merchant"]="ADVERTISERNAME";
  
$fields["feed1"]["description"]="OFFERDESCRIPTION";
  
$fields["feed1"]["code"]="COUPONCODE";
  
$fields["feed1"]["startdate"]="OFFERSTARTDATE";
  
$fields["feed1"]["enddate"]="OFFERENDDATE";
  
$fields["feed2"]["url"]="OFFER_LINK";
  
$fields["feed2"]["merchant"]="MERCHANT";
  
$fields["feed2"]["description"]="DESCRIPTION";
  
$fields["feed2"]["code"]="CODE";
  
$fields["feed2"]["startdate"]="OFFER_VALIDFROM";
  
$fields["feed2"]["enddate"]="OFFER_VALIDTO";
  
$fieldset "feed1";
  
$url "http://{feed1 URL}/";
  
MagicParser_parse($url,"myRecordHandler","xml|COUPONFEED/LINK/");
  
$fieldset "feed2";
  
$url "http://{feed2 URL}/";
  
MagicParser_parse($url,"myRecordHandler","xml|OFFERS/OFFER/");
  function 
myRecordHandler($record)
  {
    global 
$fields;
    global 
$fieldset;
    print 
$record[$fields[$fieldset]["url"]];
    print 
$record[$fields[$fieldset]["merchant"]];
    print 
$record[$fields[$fieldset]["description"]];
    print 
$record[$fields[$fieldset]["code"]];
    print 
$record[$fields[$fieldset]["startdate"]];
    print 
$record[$fields[$fieldset]["enddate"]];
  }
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Sun, 2015-08-16 20:22

Thanks David for modification but I tried adding new global fieldsets to above code and it returned errors. Can you assist with adding this to the above code? Also is there a way to add this new fieldset code onto the vouchers.php page? You assisted me on another mod which added similar feeds to Vouchers.php page after the coupons array being already populated.

Submitted by support on Mon, 2015-08-17 07:08

Hi,

What error were you getting when adding an additional fieldset? The following would add a 3rd case e.g:

<?php
  
require_once("includes/MagicParser.php");
  
// field set 1
  
$fields["feed1"]["url"]="CLICKURL";
  
$fields["feed1"]["merchant"]="ADVERTISERNAME";
  
$fields["feed1"]["description"]="OFFERDESCRIPTION";
  
$fields["feed1"]["code"]="COUPONCODE";
  
$fields["feed1"]["startdate"]="OFFERSTARTDATE";
  
$fields["feed1"]["enddate"]="OFFERENDDATE";
  
// field set 2
  
$fields["feed2"]["url"]="OFFER_LINK";
  
$fields["feed2"]["merchant"]="MERCHANT";
  
$fields["feed2"]["description"]="DESCRIPTION";
  
$fields["feed2"]["code"]="CODE";
  
$fields["feed2"]["startdate"]="OFFER_VALIDFROM";
  
$fields["feed2"]["enddate"]="OFFER_VALIDTO";
  
// field set 3
  
$fields["feed3"]["url"]="URL";
  
$fields["feed3"]["merchant"]="ADVERTISER";
  
$fields["feed3"]["description"]="DETAILS";
  
$fields["feed3"]["code"]="VOUCHERCODE";
  
$fields["feed3"]["startdate"]="FROM";
  
$fields["feed3"]["enddate"]="TO";
  
// process feed 1
  
$fieldset "feed1";
  
$url "http://{feed1 URL}/";
  
MagicParser_parse($url,"myRecordHandler","xml|COUPONFEED/LINK/");
  
// process feed 2
  
$fieldset "feed2";
  
$url "http://{feed2 URL}/";
  
MagicParser_parse($url,"myRecordHandler","xml|OFFERS/OFFER/");
  
// process feed 3
  
$fieldset "feed3";
  
$url "http://{feed3 URL}/";
  
MagicParser_parse($url,"myRecordHandler","xml|VOUCHERS/VOUCHER/");
  function 
myRecordHandler($record)
  {
    global 
$fields;
    global 
$fieldset;
    print 
$record[$fields[$fieldset]["url"]];
    print 
$record[$fields[$fieldset]["merchant"]];
    print 
$record[$fields[$fieldset]["description"]];
    print 
$record[$fields[$fieldset]["code"]];
    print 
$record[$fields[$fieldset]["startdate"]];
    print 
$record[$fields[$fieldset]["enddate"]];
  }
?>

To incorporate into your mode to vouchers.php (node 5733), have a go with something like:

  require_once("includes/MagicParser.php");
  $fieldset = "feed1";
  $url = "http://{feed1 URL}/";
  MagicParser_parse($url,"myLSRecordHandler","xml|COUPONFEED/LINK/");
  $fieldset = "feed2";
  $url = "http://{feed2 URL}/";
  MagicParser_parse($url,"myLSRecordHandler","xml|OFFERS/OFFER/");
  function myLSRecordHandler($record)
  {
    global $fields;
    global $fieldset;
    print "<tr>";
    print "<td width='50'>&nbsp;</td>";
    print "<td valign='middle'>";
    print "<h4>";
    print "<a target='_BLANK' href='".$record[$fields[$fieldset]["url"]]."'>".$record[$fields[$fieldset]["merchant"]]."</a>";
    print "</h4>";
    print "<p>";
    print $record[$fields[$fieldset]["description"]];
    print "</p>";
    print "<h4>Coupon Code: ";
    print $record[$fields[$fieldset]["code"]];
    print "</h4>";
    print "<p> Start Date: ";
    print $record[$fields[$fieldset]["startdate"]];
    print "</p>";
    print "<p> End Date: ";
    print $record[$fields[$fieldset]["enddate"]];
    print "</p>";
    print "<hr>";
    print "</td>";
    print "</tr>";
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Sat, 2015-08-22 21:14

I am having an issue still. The results show up on voucher page, but I have a custom template which prints original registered vouchers in columns and also provides a button, & timer with each voucher. The results from the modified fields & fieldsets just shows links and descriptions not in the custom html format. I am trying to get the following fieldsets incorporated on vouchers.php along with original vouchers so they print out in same html format as registered vouchers. I am thinking maybe they have to be recorded as $coupons["vouchers"][] for this to work just like registered vouchers.

Below is an example of code added to vouchers.php right after

<?php
$coupons
["vouchers"] = $vouchers;
  }  
$coupons["vouchers"] = $vouchers;
  }
 
?>

<?php
   
require_once("includes/MagicParser.php");
  
$page = (isset($_GET["page"])?intval($_GET["page"]):"1");
  
// field set 1
  
$fields["feed1"]["href"]="CLICKURL";
  
$fields["feed1"]["merchant"]="ADVERTISERNAME";
  
$fields["feed1"]["description"]="OFFERDESCRIPTION";
  
$fields["feed1"]["code"]="COUPONCODE";
  
$fields["feed1"]["valid_from"]="OFFERSTARTDATE";
  
$fields["feed1"]["valid_to"]="OFFERENDDATE";
  
// field set 2
  
$fields["feed2"]["href"]="LINK-CODE-HTML";
  
$fields["feed2"]["merchant"]="ADVERTISER-NAME";
  
$fields["feed2"]["description"]="DESCRIPTION";
  
$fields["feed2"]["code"]="COUPON-CODE";
  
$fields["feed2"]["valid_from"]="PROMOTION-START-DATE";
  
$fields["feed2"]["valid_to"]="PROMOTION-END-DATE";
   
$fieldset "feed1";
   
$url "http://feed1.com";
   
MagicParser_parse($url,"myCJRecordHandler","xml|COUPONFEED/LINK/");
   
$fieldset "feed2";
   
$url "https://feed2.com";
   
MagicParser_parse("string://".$xml,"myCJRecordHandler","xml|CJ-API/LINKS/LINK/");
   function 
myCJRecordHandler($coupon)
  {
     global 
$fields;
     global 
$fieldset;
     print 
"<tr>";
     print 
"<td width='50'>&nbsp;</td>";
     print 
"<td valign='middle'>";
     print 
"<h4>";
     print 
"<a target='_BLANK' href='".$coupon[$fields[$fieldset]["href"]]."'>".$coupon[$fields[$fieldset]["merchant"]]."</a>";
     print 
"</h4>";
     print 
"<p>";
     print 
$coupon[$fields[$fieldset]["description"]];
     print 
"</p>";
     print 
"<h4>Coupon Code: ";
     print 
$coupon[$fields[$fieldset]["code"]];
     print 
"</h4>";
     print 
"<p> Start Date: ";
     print 
$coupon[$fields[$fieldset]["valid_from"]];
     print 
"</p>";
     print 
"<p> End Date: ";
     print 
$coupon[$fields[$fieldset]["valid_to"]];
     print 
"</p>";
     print 
"<hr>";
    print 
"</td>";
     print 
"</tr>";
   }
  
$navigation["resultCount"] = count($coupons);
  require(
"html/navigation.php");
?>

Submitted by support on Mon, 2015-08-24 13:40

Hi,

Sure - you could populate a $vouchers["coupons"] array and pass this to html/coupons.php. Have a look at the following which instead of generating the HTML output directly instead loads each of the fields into a $voucher array, which is then appended to $coupons["vouchers"] (as used by html/coupons.php) at the end of the record handler function.

Note that in the original version, the voucher code is contained as part of the description, so I've included that in the example, but note the comment regarding the fields not used in the original version. If your custom mod makes use of a "code" field specifically then with this code in place it will be available for display as required. Similarly with valid_from and valid_to, these are in the database as INT(11) stored as PHP time() values, so strtotime() should be able to convert the textual valid from / to in your feeds into values as they would be stored on the database.

<?php
  
require_once("includes/MagicParser.php");
  
$page = (isset($_GET["page"])?intval($_GET["page"]):"1");
  
// field set 1
  
$fields["feed1"]["href"]="CLICKURL";
  
$fields["feed1"]["merchant"]="ADVERTISERNAME";
  
$fields["feed1"]["description"]="OFFERDESCRIPTION";
  
$fields["feed1"]["code"]="COUPONCODE";
  
$fields["feed1"]["valid_from"]="OFFERSTARTDATE";
  
$fields["feed1"]["valid_to"]="OFFERENDDATE";
  
// field set 2
  
$fields["feed2"]["href"]="LINK-CODE-HTML";
  
$fields["feed2"]["merchant"]="ADVERTISER-NAME";
  
$fields["feed2"]["description"]="DESCRIPTION";
  
$fields["feed2"]["code"]="COUPON-CODE";
  
$fields["feed2"]["valid_from"]="PROMOTION-START-DATE";
  
$fields["feed2"]["valid_to"]="PROMOTION-END-DATE";
  
$coupons = array();
  
$fieldset "feed1";
  
$url "http://feed1.com";
  
MagicParser_parse($url,"myCJRecordHandler","xml|COUPONFEED/LINK/");
  
$fieldset "feed2";
  
$url "https://feed2.com";
  
MagicParser_parse("string://".$xml,"myCJRecordHandler","xml|CJ-API/LINKS/LINK/");
  function 
myCJRecordHandler($coupon)
  {
    global 
$fields;
    global 
$fieldset;
    global 
$coupons;
    
$voucher = array();
    
$voucher["merchant"] = $coupon[$fields[$fieldset]["merchant"]];
    
$voucher["text"] = $coupon[$fields[$fieldset]["description"]];
    
$voucher["text"] .= " Voucher code: ".$coupon[$fields[$fieldset]["code"]];
    
$voucher["href"] = $coupon[$fields[$fieldset]["href"]];
    
// following fields are not used by original html/coupons.php but
    // may be used by your custom version
    
$voucher["code"] = $coupon[$fields[$fieldset]["code"]];
    
$voucher["valid_from"] = strtotime($coupon[$fields[$fieldset]["valid_from"]]);
    
$voucher["valid_to"] = strtotime($coupon[$fields[$fieldset]["valid_to"]]);
    
$coupons["vouchers"][] = $voucher;
  }
  if (
count($coupons["vouchers"])) require("html/coupons.php");
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Tue, 2015-08-25 03:51

Thanks David. The new code in place works fine & looks good, but I have a problem with the navigation, feed2 & fieldset2. Feed2 & Fieldset2 requires header authorization with a api key to process. It won't appear on the page. Also, where can I place navigation within the code?
Below is an example of Feed2 in a single feed that I would like to be converted into fieldset2 of the new updated code.

<?php
  
require_once("includes/MagicParser.php");
  
$apiKey "APIKey";
  function 
myCJRecordHandler($coupon)
  {
    print 
"<tr>";
     print 
"<td width='50'>&nbsp;</td>";
    print 
"<td valign='middle'>";
    print 
"<h4>";
     print 
$coupon["ADVERTISER-NAME"];
      print 
"</h4>";
    print 
"<h4>";
    print 
$coupon["LINK-CODE-HTML"];
    print 
"</h4>";
    print 
"<p>";
    print 
$coupon["DESCRIPTION"];
    print 
"</p>";
     print 
"<h4>Coupon Code: ";
     print 
$coupon["COUPON-CODE"];
    print 
"</h4>";
    print 
"<p> Start Date: ";
     print 
$coupon["PROMOTION-START-DATE"];
    print 
"</p>";
    print 
"<p> End Date: ";
     print 
$coupon["PROMOTION-END-DATE"];
    print 
"</p>";
    print 
"<hr>";
    print 
"</td>";
    print 
"</tr>";
  }
  
$headers[] = "authorization: ".$apiKey;
  
$ch curl_init($cj["url"]);
  
curl_setopt($ch,CURLOPT_HEADER,0);
  
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  
curl_setopt($chCURLOPT_HTTPHEADER$headers);
  
$xml curl_exec($ch);
  
MagicParser_parse("string://".$xml,"myCJRecordHandler","xml|CJ-API/LINKS/LINK/");
?>

Submitted by support on Tue, 2015-08-25 08:19

Hi,

It's no problem to mix fopen() and CURL fetching of the feed, see below for an example which also shows how to enable pagination using this method. The process is to loop over the $coupons["vouchers"] extracting only the items required for the current $page and passing thise to html/coupons.php:

<?php
  
require_once("includes/MagicParser.php");
  
$page = (isset($_GET["page"])?intval($_GET["page"]):"1");
  
// field set 1
  
$fields["feed1"]["href"]="CLICKURL";
  
$fields["feed1"]["merchant"]="ADVERTISERNAME";
  
$fields["feed1"]["description"]="OFFERDESCRIPTION";
  
$fields["feed1"]["code"]="COUPONCODE";
  
$fields["feed1"]["valid_from"]="OFFERSTARTDATE";
  
$fields["feed1"]["valid_to"]="OFFERENDDATE";
  
// field set 2
  
$fields["feed2"]["href"]="LINK-CODE-HTML";
  
$fields["feed2"]["merchant"]="ADVERTISER-NAME";
  
$fields["feed2"]["description"]="DESCRIPTION";
  
$fields["feed2"]["code"]="COUPON-CODE";
  
$fields["feed2"]["valid_from"]="PROMOTION-START-DATE";
  
$fields["feed2"]["valid_to"]="PROMOTION-END-DATE";
  
$coupons = array();
  
$fieldset "feed1";
  
$url "http://feed1.com";
  
MagicParser_parse($url,"myCJRecordHandler","xml|COUPONFEED/LINK/");
  
$fieldset "feed2";
  
$headers[] = "authorization: ".$apiKey;
  
$ch curl_init($cj["url"]);
  
curl_setopt($ch,CURLOPT_HEADER,0);
  
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  
curl_setopt($chCURLOPT_HTTPHEADER$headers);
  
$xml curl_exec($ch);
  
MagicParser_parse("string://".$xml,"myCJRecordHandler","xml|CJ-API/LINKS/LINK/");
  function 
myCJRecordHandler($coupon)
  {
    global 
$fields;
    global 
$fieldset;
    global 
$coupons;
    
$voucher = array();
    
$voucher["merchant"] = $coupon[$fields[$fieldset]["merchant"]];
    
$voucher["text"] = $coupon[$fields[$fieldset]["description"]];
    
$voucher["text"] .= " Voucher code: ".$coupon[$fields[$fieldset]["code"]];
    
$voucher["href"] = $coupon[$fields[$fieldset]["href"]];
    
// following fields are not used by original html/coupons.php but
    // may be used by your custom version
    
$voucher["code"] = $coupon[$fields[$fieldset]["code"]];
    
$voucher["valid_from"] = strtotime($coupon[$fields[$fieldset]["valid_from"]]);
    
$voucher["valid_to"] = strtotime($coupon[$fields[$fieldset]["valid_to"]]);
    
$coupons["vouchers"][] = $voucher;
  }
  if (
count($coupons["vouchers"]))
  {
    
$navigation["resultCount"] = count($coupons["vouchers"]);
    
$pageVouchers = array();
    
$counter 0;
    foreach(
$coupons["vouchers"] as $voucher)
    {
      
$counter++;
      if (
         (
$counter <= (($page-1)*$config_resultsPerPage))
         ||
         (
$counter > ($page*$config_resultsPerPage))
         )
         continue;
      
$pageVouchers[] = $voucher;
    }
    
$coupons["vouchers"] = $pageVouchers;
    require(
"html/coupons.php");
    require(
"html/navigation.php");
  }
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Thu, 2015-08-27 03:48

Thanks David. You're awesome. It all works fine. I am not sure why I can't get logos of registerd merchants to show from the parsed feeds. Also the merchant's name in parsed feed is spelled the same as name registered for merchant. Also, if possible for merchant's names that are spelled a little different would that be difficult to fix or adjust and allow logos to be displayed?

Submitted by support on Thu, 2015-08-27 08:40

Hi,

To set the logo value (assuming firstly direct 1-1 mapping between site merchant name and voucher code feed merchant name), where you have;

    $voucher["merchant"] = $coupon[$fields[$fieldset]["merchant"]];

...REPLACE with:

    $voucher["merchant"] = $coupon[$fields[$fieldset]["merchant"]];
    global $config_baseHREF;
    if (file_exists("logos/".$voucher["merchant"]))
    {
      $voucher["logo"] = $config_baseHREF."logos/";
      $voucher["logo"] .= str_replace(" ","%20",$voucher["merchant"]);
    }

Where there isn't a direct mapping, you can create a simple translation table e.g.

    $voucher["merchant"] = $coupon[$fields[$fieldset]["merchant"]];
    global $config_baseHREF;
    $siteMerchants["Voucher Feed Merchant 1"] = "Site Merchant 1";
    $siteMerchants["Voucher Feed Merchant 2"] = "Site Merchant 2";
    if (isset($siteMerchants[$voucher["merchant"]]))
    {
      $logoMerchant = $siteMerchants[$voucher["merchant"]];
    }
    else
    {
      $logoMerchant = $voucher["merchant"];
    }
    if (file_exists("logos/".$voucher["merchant"]))
    {
      $voucher["logo"] = $config_baseHREF."logos/";
      $voucher["logo"] .= str_replace(" ","%20",$voucher["merchant"]);
    }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Mon, 2015-08-31 03:49

Thanks David. The first part works fine. The 2nd part where there isn't direct mapping does not work on 1 feed that has several merchants which includes some with correct names and others without. I tried entering incorrect feed merchant name and correct site's merchant name in the transition table with no success on the logos. The incorrect feed merchant name continues to show.

Submitted by Rocket32 on Tue, 2015-09-01 06:11

Hello David. I cannot get 2nd part with no direct mapping and a transition table to work with logos. The example feed I used has several Merchants within the same feed which contains some merchant names spelled correctly and others not. Also some are registered and others are not.

Also after all is done, can the new vouchers be registered into the database thru automation tool or they have to be setup once for registered merchants?
Last question is how do I create an rss feed for the vouchers / vouchers.php?

Submitted by support on Tue, 2015-09-01 12:04

Hi,

My apologies, the second replacement with option 2 should be using the $logoMerchant variable after creation but I forgot to update the subsequent code; have a go with:

    $voucher["merchant"] = $coupon[$fields[$fieldset]["merchant"]];
    global $config_baseHREF;
    $siteMerchants["Voucher Feed Merchant 1"] = "Site Merchant 1";
    $siteMerchants["Voucher Feed Merchant 2"] = "Site Merchant 2";
    if (isset($siteMerchants[$voucher["merchant"]]))
    {
      $logoMerchant = $siteMerchants[$voucher["merchant"]];
    }
    else
    {
      $logoMerchant = $voucher["merchant"];
    }
    if (file_exists("logos/".$logoMerchant))
    {
      $voucher["logo"] = $config_baseHREF."logos/";
      $voucher["logo"] .= str_replace(" ","%20",$logoMerchant);
    }

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Wed, 2015-09-02 02:03

Thanks David. It's working great now due to your assistance.

Last question is how do I create an rss feed for the vouchers pages / vouchers.php?

Submitted by support on Wed, 2015-09-02 10:42

Hi,

It's straight forward to generate RSS format output, as a starting point, edit vouchers.php and look for the following code at line 63:

  require("html/header.php");

...and REPLACE with:

  if (isset($_GET["format"]) && ($_GET["format"]=="rss"))
  {
    print "<?xml version='1.0' encoding='UTF-8'?>";
    print "<rss version='2.0'>";
    print "<channel>";
    print "<title>Voucher Codes</title>";
    print "<link>http://".$_SERVER["HTTP_HOST"].$config_baseHREF."vouchers.php</link>";
    print "<description>Check out the latest voucher codes!</description>";
    foreach($coupons["vouchers"] as $voucher)
    {
      print "<item>";
      print "<title><![CDATA[".$voucher["merchant"]."]]></title>";
      print "<link>http://".$_SERVER["HTTP_HOST"].$voucher["href"]."</link>";
      print "<description><![CDATA[".$voucher["text"]."]]></description>";
      print "</item>";
    }
    print "</channel>";
    print "</rss>";
    exit();
  }
  require("html/header.php");

Then, to view the feed request your Voucher Codes page as follows;

http://www.example.com/vouchers.php?format=rss

And since there is a standard for indicating the URL of an RSS version of a page in the HTML header, you could add this as follows. Edit html/header.php and look for the closing </head> tag around line 26:

  </head>

...and REPLACE with:

  <?php if (strpos($_SERVER["PHP_SELF"],"vouchers.php")!==FALSE): ?>
    <link rel='alternate' type='application/rss+xml' title='Voucher Codes'
          href='<?php print $config_baseHREF?>vouchers.php?format=rss' />
  <?php endif; ?>
  </head>

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Mon, 2015-09-07 00:50

Hello David everything is working fine with above codes. One problem I have is when trying to add the rss code to a modified vouchers.php page. I get a feed code error when trying to access vouchers.php?format=rss. The rss page works fine on vouchers.php with no modifications. I tried adding rss feed code to bottom of my modified vouchers.php page, however that's when feed error occurs when trying rss page. Do I need to add code into another location on this page? Below is the modified code added to vouchers.php.

<?php
  
require_once("includes/MagicParser.php");
  
$page = (isset($_GET["page"])?intval($_GET["page"]):"1");
  
// field set 1
  
$fields["feed1"]["href"]="CLICKURL";
  
$fields["feed1"]["merchant"]="ADVERTISERNAME";
  
$fields["feed1"]["description"]="OFFERDESCRIPTION";
  
$fields["feed1"]["code"]="COUPONCODE";
  
$fields["feed1"]["valid_from"]="OFFERSTARTDATE";
  
$fields["feed1"]["valid_to"]="OFFERENDDATE";
  
// field set 2
  
$fields["feed2"]["href"]="LINK-CODE-HTML";
  
$fields["feed2"]["merchant"]="ADVERTISER-NAME";
  
$fields["feed2"]["description"]="DESCRIPTION";
  
$fields["feed2"]["code"]="COUPON-CODE";
  
$fields["feed2"]["valid_from"]="PROMOTION-START-DATE";
  
$fields["feed2"]["valid_to"]="PROMOTION-END-DATE";
  
$coupons = array();
  
$fieldset "feed1";
  
$url "http://feed1.com";
  
MagicParser_parse($url,"myCJRecordHandler","xml|COUPONFEED/LINK/");
  
$fieldset "feed2";
  
$headers[] = "authorization: ".$apiKey;
  
$ch curl_init($cj["url"]);
  
curl_setopt($ch,CURLOPT_HEADER,0);
  
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  
curl_setopt($chCURLOPT_HTTPHEADER$headers);
  
$xml curl_exec($ch);
  
MagicParser_parse("string://".$xml,"myCJRecordHandler","xml|CJ-API/LINKS/LINK/");
  function 
myCJRecordHandler($coupon)
  {
    global 
$fields;
    global 
$fieldset;
    global 
$coupons;
    
$voucher = array();
    
$voucher["merchant"] = $coupon[$fields[$fieldset]["merchant"]];
     global 
$config_baseHREF;
     
$siteMerchants["Voucher Feed Merchant 1"] = "Site Merchant 1";
     
$siteMerchants["Voucher Feed Merchant 2"] = "Site Merchant 2";
     if (isset(
$siteMerchants[$voucher["merchant"]]))
     {
       
$logoMerchant $siteMerchants[$voucher["merchant"]];
     }
     else
     {
       
$logoMerchant $voucher["merchant"];
     }
     if (
file_exists("logos/".$logoMerchant))
     {
       
$voucher["logo"] = $config_baseHREF."logos/";
       
$voucher["logo"] .= str_replace(" ","%20",$logoMerchant);
     }
    
$voucher["text"] = $coupon[$fields[$fieldset]["description"]];
    
$voucher["text"] .= " Voucher code: ".$coupon[$fields[$fieldset]["code"]];
    
$voucher["href"] = $coupon[$fields[$fieldset]["href"]];
    
// following fields are not used by original html/coupons.php but
    // may be used by your custom version
    
$voucher["code"] = $coupon[$fields[$fieldset]["code"]];
    
$voucher["valid_from"] = strtotime($coupon[$fields[$fieldset]["valid_from"]]);
    
$voucher["valid_to"] = strtotime($coupon[$fields[$fieldset]["valid_to"]]);
    
$coupons["vouchers"][] = $voucher;
  }
  if (
count($coupons["vouchers"]))
  {
    
$navigation["resultCount"] = count($coupons["vouchers"]);
    
$pageVouchers = array();
    
$counter 0;
    foreach(
$coupons["vouchers"] as $voucher)
    {
      
$counter++;
      if (
         (
$counter <= (($page-1)*$config_resultsPerPage))
         ||
         (
$counter > ($page*$config_resultsPerPage))
         )
         continue;
      
$pageVouchers[] = $voucher;
    }
    
$coupons["vouchers"] = $pageVouchers;
    require(
"html/coupons.php");
    require(
"html/navigation.php");
  }
  if (isset(
$_GET["format"]) && ($_GET["format"]=="rss"))
   {
     print 
"<?xml version='1.0' encoding='UTF-8'";
     print 
"<rss version='2.0'>";
     print 
"<channel>";
     print 
"<title>Voucher Codes</title>";
     print 
"<link>http://".$_SERVER["HTTP_HOST"].$config_baseHREF."vouchers.php</link>";
     print 
"<description>Check out the latest voucher codes!</description>";
     foreach(
$coupons["vouchers"] as $voucher)
     {
       print 
"<item>";
       print 
"<title><![CDATA[".$voucher["merchant"]."]]></title>";
       print 
"<link>http://".$_SERVER["HTTP_HOST"].$voucher["href"]."</link>";
       print 
"<description><![CDATA[".$voucher["text"]."]]></description>";
       print 
"</item>";
     }
     print 
"</channel>";
     print 
"</rss>";
     exit();
   }
   require(
"html/header.php");
?>

Submitted by support on Mon, 2015-09-07 08:08

Hi!

All it will be, is that the RSS output needs to be exclusive, and not preceded by any HTML generation, e.g. anything created by calling require("html/....");.

Start by removing all lines that include any html/ files, e.g.

    require("html/coupons.php");
    require("html/navigation.php");

and

   require("html/header.php");

Then test your page with ?format=rss which should then work fine.

Finally, re-instate the calls to include html/coupons.php and html/navigation.php within the remainder of the script, after the call to html/header.php as required, using the same IF condition e.g.

  if (count($coupons["vouchers"]))
  {
    require("html/coupons.php");
    require("html/navigation.php");
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by Rocket32 on Fri, 2015-09-25 00:07

I added a shuffle command which shuffles all results when navigating from one page of results to other. I would like it to count all results, then shuffle them all, and assign shuffled results to pages are shuffled again on that page. I am trying to keep results shuffled on same page as visitor navigates to page 2, 3 or other. Also each page results to stay shuffled. I am using following code:

<?php
  
if (count($coupons["vouchers"]))
  {
    
shuffle($coupons["vouchers"]);
    
$navigation["resultCount"] = count($coupons["vouchers"]);
    
$pageVouchers = array();
    
$counter 0;
    foreach(
$coupons["vouchers"] as $voucher)
    {
      
$counter++;
      if (
         (
$counter <= (($page-1)*$config_resultsPerPage))
         ||
         (
$counter > ($page*$config_resultsPerPage))
         )
         continue;
      
$pageVouchers[] = $voucher;
    }
    
$coupons["vouchers"] = $pageVouchers;
?>

Submitted by support on Fri, 2015-09-25 07:40

Hi,

Check out the comments on the PHP page for the shuffle() function where you'll find an seoShuffle function which will produce a repeatable order based on a seed value. One way to produce a repeatable order that only changes daily would be to use the strtotime() value of today's date, so in place of:

    mt_srand(strlen($string));

...use:

    mt_srand(strtotime(date("Y-m-d")));

Cheers,
David.
--
PriceTapestry.com