I am a noob in Zend and I would appreciate if you could help me to figure out how to use pagination in my case.
this is my view
and this is my controller
I am using APIs to access my models.
I researched and read a lot about pagination in Zend but I had/have trouble implementing it.
Thank you for your willingness to help me out.
in your controller in line 36 write : (assuming $resultq is a valid zend_paginator param)
$paginator = Zend_Paginator::factory($resultq);
$paginator->setCurrentPageNumber($this->getRequest()->getParam('page')); // page number
$paginator->setItemCountPerPage(20); // number of items to show per page
$this->view->paginator= $paginator;
now in your view you have to add pagination controls, either do it directly in the view or use a template (you can store templates in application/views/scripts/templates for example), here is an example of a pagination template : http://zendgeek.blogspot.com/2009/07/zend-pagination-example.html
then in your view you have to integrate the template (wherever you want the controls to appear) using:
<?php echo $this->paginationControl($this->paginator, 'Sliding', 'templates/pagination.phtml'); ?>
and instead of using <?php foreach ($this->basicBwDetails as $result): ?> use <?php foreach ($this->paginator as $result): ?>
Related
I would like to load pagination module on different module than article. I have found that
components\com_content\views\article\tmpl\default.php contains this:
<?php
if (!empty($this->item->pagination) && $this->item->pagination && $this->item->paginationposition && !$this->item->paginationrelative):
echo $this->item->pagination;
?>
<?php endif; ?>
unfortunately if I put this code for example to my template index file it does not work. Aparently I have to add something more to this.
Could you advice me what other part of the code is needed?
Thank you!
It's a little complicated to wrap your mind around but if you look in the plugins/content folder you will see the pagenavigation plugin. This is the plugin that creates the pagination you see in articles.
THis plugin is triggered by
$results = $dispatcher->trigger('onContentBeforeDisplay', array('com_content.article', &$item, &$item->params, $offset));
which you can find in `\components\com_content\views\article (and also archive and the tag view of com_tags). I have no idea why it's not triggered in other components except at some point probably someone thought there wasn't a usecase for it.
To trigger the plugin in another component you would need to add that same event or a different event that basically does what the onContentBeforeDisplay method in the plugin does. If it is your own component I would do it in the same place content and tags do. If you need it in one of the core components you could probably do it by using another event.
EDIT My question is how to make pagination on View generic, currently what i have to do is some what like this on every page where i need pagination variables set
<?php echo ( count($this->paginator) > 0 ) ? $this->paginationControl($this->paginator, 'Sliding', 'administration/pager.phtml', array('url' => $this->url('company'))) : ""; ?>
Dear which you want to do is clearly mentioned in zf2 documentation:
http://framework.zend.com/manual/2.1/en/tutorials/tutorial.pagination.html
"Let’s create the partail in the module/Application/view/partial/ folder, so that we can use the control in all our modules:"
Above mention line shows that if we want to make pagination generic which will be available for all module, then make it partial in Application module.
For my Codeigniter site, I started by making a view for each controller situation. This was impractical, as it would require going back to the code for each to make a change. So I changed approach and operated on a 'default' controller with optional fields. I then thought I could load special views as needed into it.
I put together this view with optional fields with fields for $title, $search_bar on/off etc. However, now came the content area. I was able to load more views into this default view using:
$data['content_views'][]='blocks/login';
$this->load->view('default/a', $data);
and in the 'default'view:
if(isset($content_views)&& (is_array($content_views)))
{
foreach($content_views as $content_view)
{
$this->load->view(&$content_view);
}
}
(and that works fine)
Two questions:
Am I making things to complex? Is this an accepted way of doing this? Or have I misunderstood the functioning of a view and how they are intended to work?
I want a way to mix the $content_view, i.e. a block of text, then a view. I'm not quite sure as to how to proceed. Say I want a message first, then a view, then more text. This method will only accept views.
Can anybody help me create this flexible approach?
Yeah I would say you're making things a little complex. While I may not be following your description well enough to know precisely how to respond, I can tell you how I do it:
First the whole site is run through a template so the header and footer are the container file and all views needed within the site are rendered as page type views - like an article page, a gallery page, etc. Components are loaded and passed to the views as strings:
$content['sidebar'] = $this->load->view('components/sidebar', $data, true);
That last true says to render as string.
Essentially, this means the page views are pretty much html with php echoing out the necessary elements. No views calling other views, which is how I read your example.
CI loads views progressively, so your controller can output like so:
$this->load->view('header', $header_data);
$view_data['sidebar'] = $this->load->view('components/sidebar', $sidebar_data, true);
$this->load->view('content', $view_data);
$this->load->view('footer', $footer_data);
and in content view, handle the sidebar like so:
<?php if(isset($sidebar)): ?>
<nav>
<?php echo $sidebar; ?>
</nav>
<?php endif; ?>
And, assuming you populate those arrays for each view it will render header, then content, then footer. And also render sidebar if it is present.
So combining everything, I'm basically saying you can load in sections in your controllers progressively, passing sub-views as strings to whichever section makes sense. That keeps your view controlling in the controller where it belongs and not in the view files themselves. In my experience, I have not had to write a site that was so complex that this construct wasn't perfectly suitable if the site is planned well.
I'm working on a site where there can be up to 3 levels of hierarchy in the navigation.
main sections in the top header
sub-navigation on the left
optional sub-sub navigation below that
In the past, when I've rolled my own with PHP, I would create a file navigation.php holding a class and arrays for all the sections and sub sections and a couple of functions to output the navigation. I'd set variables on every page in the site (current_section ='', current_sub_section='') so the navigation function would know what to highlight.
In CodeIgniter, I'm wondering if this is still a good approach to use?
I'm going to assume that your main nav sections almost directly map to your controllers, ie.
Home | News | Events
In your top bar, maps to:
/home
/news
/events
If this is the case, you already have a simple way of selecting your nav array.
You can put an array of nav item-link pairs in your controller constructor & pass them along to a sub-view in your output.
Example:
class HomeController extends CI_Controller
{
private $nav;
public function __construct()
{
parent::__construct();
$this->nav = array(
array('Browse', site_url('news/browse')),
array('Edit', site_url('news/edit'))
);
$this->load->vars(array('NavigationArray' => $this->nav));
}
// ...
}
Now what you've done is auto-registered a variable in all your views $NavigationArray that contains an array of Display Name - Link pairs.
You can then load a basic Navigation View that builds up your subnav from that variable (as it's availiable everywhere).
<? foreach($NavigationArray as $entry): ?>
<?=$entry[0];?>
<? endforeach; ?>
And below that you can look for the existance of a sub-nav array that you could optionally set in your controller, or whatever (the third optional nav you talked about)
<? if(exists($SubNavigationArray)): ?>
<? foreach($SubNavigationArray as $entry): ?>
<?=$entry[0];?>
<? endforeach; ?>
<? endif; ?>
Please remember, this example is very basic, but this is generally how we pass data around, I wouldn't want you to put global variables anywhere and try to queue off them. Simply "load" variables into the view engine, and they'll be availiable when you go to render your views/subviews.
This way the controller controls what nav items get displayed.
Also note:
You can pass the variables explicitly instead of trusting they will exist in the scope of your view.
$this->load->view('myview', array('NavigationArray' => $this->nav));
Hope this helps.
Here's basically what I do to determine "active" links in Codeigniter:
$active_class = '';
$url = site_url('your/link/url');
if ($url == current_url())
{
$active_class = ' class="active"';
}
$link = '<a href="'.$url.'"'.$active_class.'>Link Text</a>";
Keep in mind this is a basic example and usually run in a loop. The best way depends on what your navigation array looks like, and what you consider to be "active" (f you want to "activate" links whose href partially match the url).
The best way is to use 3 different views, a top view, a left view and a bottom view then in your controller you can pass the apporiate vars to each view so you would do something like
$top[current] = something;
$top[current_sub] = somethingelse;
$this->load->view('top_nav', $top);
$this->load->view('left_nav',$left);
...
Then in your views you can handle the variables passed to them.
I use codeigniter and need to display last 3 posts at footer from blog as blabla.com/blog located.
when I create a test.php file as below. it works well,
test.php
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
query_posts('showposts=3');
?>
<ul>
<?php while (have_posts()): the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
but when I copy same code to footer_view.php of codeigniter structre, it doesnt work and giving error as below:
error at codeigniter footer_view:
Fatal error: Call to undefined method
stdClass::set_prefix() in
/blabla/blog/wp-settings.php
on line 268
any idea what can be the problem? :/ appreciate helps!!
I've used 3 tricks for getting WordPress content into CodeIgniter:
Pull via XMLHttpRequest from a custom WP template (skip headers/footers/sidebars). I like this method as it is highly decoupled, and it makes for fast page loads.
Pull via CURL or get_file*. This is similar to using XMLHttpRequests, but server side.
Wrap WP in a library. This is more work, but the essence is calling the core WP object from a CI library. I prototyped this method last year, but found that #1 performed better (and it allowed me to move the content to another server later).
Note, you could also IFrame the page, but IFrames seem a bit hackish given #1 and #2.
Have you thought about using the RSS feed from wordpress to display the blog posts with codeigniter? It would be a more flexible solution.