Zend framework mvc model and generating dynamic reports - model-view-controller

I am using zend framework to develop my php applications using mvc model.
I want to generate some reports from database. But I don't know how to separate models and views in this case.
I fetch information from db, then I should iterate through them and make a HTML table. Then pass this table to a class, making pdf file from the HTML table.
How should I make this HTML table in my view then pass it to my model and generating pdf file?!

//in controller
$this->view->data = $model->getData($parameter);
//in model
pulic function getData($parameter)
{
$sql = $this->select()->where('parameter = ?',$parameter);
return $this->fetchAll($sql);
}
//in view
...
foreach($this->data as $item)
{
echo '<tr>';
echo '<td>';
echo $item->name;
echo '</td>';
echo '</tr>';
}

Related

Retrive data from view to controler CodeIgniter via href

Ok my code is in view file
echo '<h4>Lista podataka</h4>';
br().br();
$this->db->select('info');
$query = $this->db->get('pages');
foreach ($query->result() as $q){
echo "<a href='update'>". $q->info .br()."</a>";
}
with href i am calling controler
How to grab $q->info data to controler
you're throwing mvc out of the window, but basically :
example.com/controller/method/param

Is it good to call a model function from a view

While working on codeigniter, I sorted out one thing that I can call model function from view page also.
For an example
here's my example model
<?php
class autoload_model extends CI_Model{
function __construct() {
parent::__construct();
}
/*---------data fetching-----------*/
function get_data_from_table($table,$data,$cond)
{
$this->db->select($data);
$this->db->where($cond);
$result= $this->db->get($table);
return $result;
}
/*---------ends-----------*/
}
?>
Now in my view page i have written this
<table cellspacing="1" cellpadding="0">
<tr>
<td>Product Title</td>
<td><Product Price</td>
</tr>
<?php
$product_list = $this->autoload_model->get_data_from_table("td_product","*",
"product_id > 0")->result_array();
if(count($product_list)>0)
{
foreach($product_list as $pl)
{?>
<tr>
<td><?php echo $pl['product_title'];?></td>
<td><?php echo $pl['product_price'];?></td>
</tr>
<?php
}
}
else
{?>
<tr>
<td colspan="2">No data Found</td>
</tr>
<?php
}
}?>
</table>
the whole things works fine, its just that I want to know whether its good to use in such fashion or not?
NOTE:
The autoload model is automaticaly loaded in the config/autoload.php file
Codeigniter is PHP Framework which built on top of the MVC (Model - View - Controller) design pattern. if you do not follow this then do not use framework
In one word: NO.
Why?
1) Since codeigniter is a MVC Framework, we should follow some defined protocols of MVC.
2) We've to see that how a MVC Framework works and accordingly we've to use it.
3) So how CI works?
- Firstly, we have a URL which loads a particular page.
- The routes checks the routes matching the URL and calls particular Controller.
- If no match found in routes, it then checks the matching Controller and Function
- Controller calls the model with addon data, and models uses that data for database operations.
- Models then returns required data back to the Controller and then Controller loads the view using that data.
CI Flow:
Request ---> Routing ---> Controller ---> Model/Libraries/Helpers/Plugins ---> Controller ---> Views
Your answer:
1) Calling models in views will work anyways (if model is autoloaded) but still it's wrong. Doing that means we're breaking MVC rules.
2) Also you can see in above flow that there is no connection between models and views.
3) So where can we call models: controllers, libraries, helpers.
so another way of looking at this - you are calling the view to render a product table - but you have not confirmed whether there are products.
the controller calls a product model and requests products. if products come back then the controller assigns an appropriate view for displaying products and passes the products_list object (or array) to the view. there should not be any code in the middle of a table in a view like
if(count($product_list)>0)
the only php code the view should have is things like
foreach($product_list as $pl)
to display the products. the value of $product_list comes from the model. so if for example your database table name changes -- that change is done in the model -- not the view.
and if NO products come back from the model - then there is no $product_list. the controller assigns a view where the user can search/browse for other products. we don't need an if(count) in the view because we already know what the search results are.

Pagination on Pyrocms pages

I am currently displaying a list of products on pyrocms page. Using plugin i get the products from db tables and display them as a list i want to use pagination but have no idea how to use it and where to start from. Does any one know any tutorial or have some suggstion?
You won't need to load pagination library or initialize it. It is a little different from you do in regular codeigniter, also you can surely use the way you do it in codeigniter pagination class
I usually do this for pagination.
in your controller use this
....
// Create pagination links
$total_rows = $this->products_m->count_all_products();
$pagination = create_pagination('products/list', $total_rows);
//notice that product/list is your controller/method you want to see pagination there
// Using this data, get the relevant results
$params = array(
'limit' => $pagination['limit']
);
$this_page_products = $this->product_m->get_all_products($params);
....
//you got to pass $pagination to the view
$this->template
->set('pagination', $pagination)
->set('products', $this_page_products)
->build('products/view');
obviously, you will have a model named products_m
At your model this would be your get_all_products function, I am sure you can build the count_all_products() function too
private function get_all_products($params){
//your query to get products here
// use $this->db->limit();
// Limit the results based on 1 number or 2 (2nd is offset)
if (isset($params['limit']) && is_array($params['limit']))
$this->db->limit($params['limit'][0], $params['limit'][1]);
elseif (isset($params['limit']))
$this->db->limit($params['limit']);
//rest of your query and return
}
Now at your view you have to foreach in your passed $products variable and to show pagination links use this line in your view
<?php echo $pagination['links'];?>
I use this in my works, hope it help.
Pyrocms used codeigniter framework this code works perfectly in case of module but i have never tried this on a plugin but still you can try this.
Load pagination library in Controller
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
Use this code in view to genrate the links
echo $this->pagination->create_links();

CodeIgniter - Select data from db, common to all views

I'm very new to CI. :)
In my project, I have separated the page into header, footer and body. And the body portion(view) is loaded based on the controller. The header and footer are common to all pages.
For example, for a login page it would be like this:
$this->load->view('header');
$this->load->view('login');
$this->load->view('footer');
But now my concern is, how to generate the "category" section (which will list several category names on the left of the body portion). Upon clicking a category, the corresponding details page would be show to the right(ie. in content portion). So, in all views(all pages) I need to display the list of categories.
Visual example:
----------------------------
Header Portion of Page
----------------------------
Body Portion
============
Cat1 |
Cat2 |
Cat3 | Content
Cat4 |
Cat5 |
----------------------------
Footer
----------------------------
These categories are populated from the data in db.
I have just done some searching. So, I am thinking about creating a helper class and autoload it. So, in all views, I would call the function and and echo the result.
For eg:
function hlp_getCategories()
{
$ci =& get_instance();
$q = $ci->db->query('SELECT cat_name FROM tblCategories');
return $q;
}
And in the view:
<?php
$q = hlp_getCategories();
foreach ($q->result_array() as $row)
{
echo anchor('cat/' . $row['cat_name'], $row['cat_name']) ;
}
?>
Is this the correct approach ?
Am I in the right track ?
Thanks in advance :)
That is one way to solve it - although if you follow a strict MVC approach - the helper would call the model $this->category->select_cat(), and put the SQL query in the model. Furthermore, the SQL query should use active record selections, not a text SQL query.
The other way to solve it is to use some CSS that has a DIV for the left menu (i.e. categories), and a DIV for the right (i.e. content).
Then you could do
$this->load->view('header');
$this->load->view('categories');
$this->load->view('login');
$this->load->view('footer');
Then inside your categories view
<div class = "left">
// show categories here
</div>
and inside your content views
<div class = "right">
// show content here
</div>

how to load view into another view codeigniter 2.1?

Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>

Resources