0

I need help setting up ViewsPHP to evaluate 3 pieces of data? (I barely know any PHP)

ViewFields

I have a View with three embedded Viewfields:

Node count $row->view [views_field_view_view]

Time ago (1 unit) $row->view_1 [views_field_view_view_1]

Success rate $row->view_2 [views_field_view_view_2]

Value code

In the Value Code text area I want to call all the values. I currently have:

return $data;

Output code

In the Output Code I want to do something like this:

<?php
if ($row->view >= 5 AND $row->view_1 >= 5 AND $row->view_2 >= 80) { 
    echo "Amazing person";
}
elseif ($row->view >= 3 AND $row->view_1 >= 3 AND $row->view_2 >= 70) { 
    echo "Good person";
} 
elseif ($row->view >= 1 AND $row->view_2 >= 60) { 
    echo "Interesting person";
}
else {
    echo "Content needed";
}
?>

It's only outputting "Content needed" so what have I done wrong?

EDIT: Updating to 7.x-2.x-dev makes this work.

0

Structurally, your code looks fine, it's probably because

$row->view = <div id="views-view--view-name"><div>5</div></div> basically the 5 has HTML tags around it.

so <div id="views-view--view-name"><div>5</div></div> does not equal 5

You can use the PHP strip_tags() function to get rid off the tags.

if (strip_tags($row->view) >= 5 AND strip_tags($row->view_1) >= 5 AND strip_tags($row->view_2) >= 80) { 

So try this:

<?php

$a = strip_tags($row->view);
$b = strip_tags($row->view_1);
$c = strip_tags($row->view_2);

if ($a >= 5 AND $b >= 5 AND $c >= 80) { 
    echo "Amazing person";
}
elseif ($a >= 3 AND $b >= 3 AND $c >= 70) { 
    echo "Good person";
} 
elseif ($a >= 1 AND $c >= 60) { 
    echo "Interesting person";
}
else {
    echo "Content needed";
}
?>

PS: no need to have anything for value code return $data;.

  • Maybe it needs more than just strip_tags to parse the ViewField because I'm getting crazy results. EQUAL TO works but LESS THAN does not. $a = 3 is true, $a <= 3 is passed over even though the string is 3. – JohnD Jul 26 '16 at 11:59
  • @JohnD $a = 3 is true equal to needs to be two equals signs $a == 3 because if you use only 1 equal sign, it sets the variable to 3, rather than evaluate/test to see if it equals 3, which will always return true. – No Sssweat Jul 26 '16 at 13:14
  • @JohnD when you echo $row->view; what gets printed? just a number or do you see more stuff? – No Sssweat Jul 26 '16 at 13:18
  • echo $row->view; prints nothing, but print_r ($data); prints this stdClass Object ( [users_name] => Ben [uid] => 140 [users_created] => 1437618401 [views_field_view_view] => 3 [views_field_view_view_1] => 1 year [views_field_view_view_2] => 64 ) – JohnD Jul 27 '16 at 9:37
  • @JohnD "echo $row->view; prints nothing" well then that's your problem, your view is empty. $data is irrelevant here. – No Sssweat Jul 27 '16 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.