My main goal is to build a custom view that is a table of user data (name, email, phone), and I need to be able to sort by the columns created by Views PHP (a column that displays "location")

I'll paste what I've punched in, and perhaps someone can help me?

First off, I have my setup code that has a function called GetLoc(x) where you can pass a phone number and it returns a string representing a state (i.e. "California", "New York")

The output works great and I get the column I need, and here is my code:

<?php
// if address is non-empty, return state (ie CA)
if (!empty($data->field_field_address[0]['rendered']['#markup'])) {
   
$state = $data->field_field_address[0]['rendered']['#markup'];
    print
$state;
} elseif (isset(
$data->_field_data['uid']['entity']->field_invest_text_phone['und'][0]['value'])) {
// else if phone number is set, return GetLoc($phone_number)
   
$pn = $data->_field_data['uid']['entity']->field_invest_text_phone['und'][0]['value'];
   
$loc = GetLoc($pn);
    print
$loc;
} else {
// else return "unknown"
   
print "Unknown";
}
?>

How can I make it so that I can click the table headers to order the results alphabetically? Bonus points: how can I create a filter that will allow me to display only users where the output = what I'm looking for?

I tried using the same code in the "Value Code" section, replacing "print" with "return", minus the php tags, and then selecting "alphabetic" from the "enable click sort" dropdown. No luck.

Any help is much appreciated. I've read a bunch of posts and most of it sails over my head...

Comments

It it important to note that the system sorts on the result of the "Value" field, not the "Output" field.

If you put that code in the "Value" field and replace your print statements with return like so:

<?php
// if address is non-empty, return state (ie CA)
if (!empty($data->field_field_address[0]['rendered']['#markup'])) {
   
$state = $data->field_field_address[0]['rendered']['#markup'];
    return
$state;
} elseif (isset(
$data->_field_data['uid']['entity']->field_invest_text_phone['und'][0]['value'])) {
// else if phone number is set, return GetLoc($phone_number)
   
$pn = $data->_field_data['uid']['entity']->field_invest_text_phone['und'][0]['value'];
   
$loc = GetLoc($pn);
    return
$loc;
} else {
// else return "unknown"
   
return "Unknown";
}
?>

then put the following in the value field your sort should work.

<?php
print $value;
?>