0

I'm trying to use a view inside a loop but it's not working, I'm using "views_get_view($name)" but "clone_view()" / "copy()" is not working, I can't create another reference.

I have a view with a contextual filter and by settings different arguments on each loop with "set_arguments" the result should be different. I get the result for the first loop but all the others are empty.

I'm trying to use something like this:

for($i=2; $i<=4; $i++){

    $view = views_get_view('view_name');
    $view->set_display($i);
    $view->set_arguments(array($i));
    $view->execute();

    dpm($view->result);
}

and the result is:

... (Array, 4 elements)

... (Array, 0 elements)

... (Array, 0 elements)

The first result is working great (4 elements inside the array) but the others are empty.

I know the above code is not correct as this is not the way to handle objects but at least you will know what I want, a different result for each set of arguments, arguments which are connected to a contextual filter inside view.

Can someone help me with a method to use the view inside a loop ? I only managed to generate the result once no mather what.

Thanks.

0

The $view->set_display($i); will not work unless you have an integer as the display name of your view (which is something I'm not sure if possible or if you need it).

try changing the $i in $view->set_display($i); to the display name of your view:

for($i=2; $i<=4; $i++){

    $view = views_get_view('view_name');
    $view->set_display('display_name'); //Example: page, page_1, block_1, custom_display_name ..etc
    $view->set_arguments(array($i));
    $view->execute();

    dpm($view->result);
}
  • Tried that allready, same result. The function will work also without set_display so that step is not important. – NeoHQ Aug 4 '14 at 21:31
  • Have you tried: $view = views_get_view('view_name', TRUE); – raffi Aug 5 '14 at 7:43
  • Yes, same result. I've also tried "unset($view)" at the end of each iteration but with no luck. It seems I can't use a view multiple times on a page and I can't understand why. – NeoHQ Aug 5 '14 at 9:14
0

The problem was/is "$view->execute", it seems that I can run this method only once no matter what I do, so I had to approach the issue different. I've found another solution:

for($i=7; $i<=10; $i++){

        $view = views_get_view('edc_menu', TRUE);
        $view->set_arguments(array(2, $i));
        $view->use_pager();
        $view->set_items_per_page(0);
        $view->build();

        $q = edcmenu_qc($view->build_info['query'], TRUE);
        $q = db_query($q);

        foreach($q AS $key => $item){
            // Generate array with results.
        }
    }

I extracted the view query from each iteration then I used db_query() to run it.

edcmenu_qc() is a method based on "devel" method dpq() to convert the object query into a string so I can pass it further to db_query()

function edcmenu_qc($query, $return = FALSE) {

    if (method_exists($query, 'preExecute')) {
        $query->preExecute();
    }
    $sql = (string) $query;
    $quoted = array();
    $connection = Database::getConnection();
    foreach ((array) $query->arguments() as $key => $val) {
        $quoted[$key] = $connection->quote($val);
    }
    $sql = strtr($sql, $quoted);
    if ($return) {
        return $sql;
    }

    return ($return ? NULL : $query);
}

Problem solved and eveyrthing is working perfect.

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.