You are here:  » Sorting feed based upon variable criteria


Sorting feed based upon variable criteria

Submitted by rcasey on Thu, 2016-08-11 20:48 in

Hey David,

I've been struggling with this for a week or so now, and I figured that you've always been so helpful, I might as well come back to the source.

I'm using the following code to return select records, and it works great:

<?php
 
if ($record["TEAM-DIVISION"]<>"Division 5A") return; 
?>

However, there are instances where I need to use a variable in place of a string, such as:

Example 1

<?php
 
if ($record["TEAM-DIVISION"]<>$division) return; 
?>

That never returns a match.

I have also tried something like this:

Example 2

<?php
 
if ($division == "5A") {
        if (
$record["TEAM-DIVISION"]<>"Division 5A") return; }
    if (
$division == "4A")  {
        if (
$record["TEAM-DIVISION"]<>"Division 4A") return; }
    if (
$division == "3A")  {
        if (
$record["TEAM-DIVISION"]<>"Division 3A") return; } 
?>

That does not work, either. Any thoughts?

My preference would be to have Example 1 work, but I'm totally OK if it is easier to get Example 2 to work.

Submitted by support on Fri, 2016-08-12 08:37

Hi,

Example 1 should work fine - I suspect it may just be down to not having $division declared as global at the top of your myRecordHandler() function - have a go with:

<?php
 
global $division;
 if (
$record["TEAM-DIVISION"]<>$division) return;
?>

Cheers,
David
--
MagicParser.co

Submitted by rcasey on Fri, 2016-08-12 14:46

Ah thank you, I forgot to do that. However, it is still not working. Here is my full code (it's a WordPress plugin). Is there anything that would cause it not to work?

{code saved}

Submitted by support on Fri, 2016-08-12 14:56

Hi,

WordPress plugins are executed outside of the global scope, so you would also need to declare your variables as global at the point at which you initialise them, so where you have this code:

  $count = 0;
  $classification = "Division {$atts['class']}";

...REPLACE with:

  global $count;
  global $classification;
  $count = 0;
  $classification = "Division {$atts['class']}";

That should be all it is, but something I would also advise, is to use a namespace unique to your plugin for any global variables required so that you don't interfere with anything else going on globally, e.g.

  global $myPluginName_count;
  global $myPluginName_classification;
  $myPluginName_count = 0;
  $myPluginName_classification = "Division {$atts['class']}";

(and the same changes within the record handler function of course)

Cheers,
David
--
MagicParser.com

Submitted by rcasey on Fri, 2016-08-12 20:53

That did it. Thank you SO much!