How can we set the items per page to a view, programatically?

The similar code we are using in drupal 5 is as follows:
$view = views_get_view('visual_articles');
print (views_build_view('embed', $view, array(0 => '1', 6 => 'video'), $view->use_pager, $view->nodes_per_block));

Can u please give me the corresponding code in druapal 6
Thanks in advance.
Cibi

Comments

zorroposada’s picture

I had the same question.

I found useful info here:
http://pedroposada.com/blog/embed-views-2-nodes.html

$view = views_get_view($viewname);
$view->set_arguments( array( $arg ) );
$view->set_display($displayId);
$view->set_items_per_page($number);
$view->execute();
$count = count( $view->result );
print $view->preview($display_id, $args);
alienzed’s picture

$args = array();
$view = views_get_view('viewName');
$view->set_display('default');
$view->display_handler->set_option('items_per_page', 1);
print $view->preview('default', $args);

nothing but the above code here worked for me

replace viewName by your view name and voila!

alienzed’s picture

if you want to do the same thing without embedding the view, you'll need to create a module (trust me, there's no other way)
then use hook_views_pre_execute()

Rob T’s picture

Thanks so much, alienzed. After a few hours of frustration, stumbling into your solution in #2 worked perfectly.

qlmhuge’s picture

And where does one put this snippet of code? In a block, in a node, in a view, in a template? Sorry, I'm not strong on the coding side, but I would really like my users to set the number of results per page in a view. Can someone give me the "Dummies" version?

daboo’s picture

Well, I can tell you, don't put it into a block and apply it to the whole site. It will take your site down if it does not work. Believe me, I just learned the hard way. Thank goodness it was as simple as replacing the field content in the db to get it back.
If I figure out where to put it that makes logical sense and works, I'll be the first to share my results.

Best Regards,

Daboo

dkinzer’s picture

I found what I think is a much easier way of pulling this off if Views_3 is not an option yet.

using hook_form_alter apply the following to the appropriate $form:

//Add select number of pages filter.
$pager_options = array(20, 30, 50);
$form['views_page_filter'] = array(
'#type' => 'select',
'#options' => $pager_options,
'#default_value' => 1,
'#title' => t('Select items per page'),
);

$pages = $pager_options[$form_state['input']['views_page_filter']];
$form_state['view']->set_items_per_page($pages);

Though this will only work if you have an exposed filter for the view.

Vendict’s picture

It works, thank you!