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

cibi.jacob@gmail.com - March 17, 2009 - 13:01
Project:Views embed
Version:6.x-1.x-dev
Component:Code
Category:support request
Priority:critical
Assigned:Unassigned
Status:active
Description

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

#1

zorroposada - April 22, 2009 - 17:59

I had the same question.

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

<?php
$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);
?>

#2

alienzed - July 9, 2009 - 21:19

$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!

#3

alienzed - July 29, 2009 - 01:28

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()

#4

Rob T - October 5, 2009 - 06:10

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

#5

qlmhuge - November 23, 2009 - 09:27

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?

 
 

Drupal is a registered trademark of Dries Buytaert.