3

I am using the function views_get_view and it's working fine if am using this once in a function. But when I am calling this function multiple times with different arguments in the same function the result is going to lose. In that case, the result always be same as coming with first-time arguments. Following are my code.

 $viewname = "profile_match_with_job";
  $display_id = "block_3";
  $view = views_get_view($viewname);
  $view->set_display($display_id);
  $view->set_arguments(array(138,70)); // views Contextual filter parameters
  $view->pre_execute();
  $view->execute();
  $myresult =  $view->result;
// here $myresult array showing the correct result

But I am calling the same thing with dfferent argument then the result is not corrct like this

 $viewname = "profile_match_with_job";
  $display_id = "block_3";
  $view = views_get_view($viewname);
  $view->set_display($display_id);
  $view->set_arguments(array(138,80)); // views Contextual filter parameters
  $view->pre_execute();
  $view->execute();
  $myresult =  $view->result;
// here $myresult array showing the same result but in that case I have chnaged the param values from 138,70 to 138,80

1 Answer 1

6

Yes, this issue arises because you have a current copy of view and you not destroy it. Kindly use below code for multiple use (suggested to be every-time).

$viewname = "profile_match_with_job";
$display_id = "block_3";
$view = views_get_view($viewname);
$view->set_display($display_id);|
$view->set_arguments(array(138,80)); // views Contextual filter parameters
$view->pre_execute();
$view->execute();
$myresult =  $view->result;
$view->destroy();
2
  • 1
    Awesome, never even knew that existed
    – Clive
    Commented Jul 22, 2016 at 9:16
  • Learn something new. Cool
    – Jimmy Ko
    Commented Jul 22, 2016 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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