I'm trying to add Mostviewed located here:
App/code/local/Mage/Catalog/Block/Product/Mostviewed.php
I have added the following code:
class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract
{
public function __construct(){
parent::__construct();
$storeId = Mage::app()->getStore()->getId();
$collection = Mage::getResourceModel('reports/product_collection')
->addViewsCount();
$collection->getSelect()->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
$this->setProductCollection($collection);
return parent::_beforeToHtml();
}
}
I'm added echo $this->getToolbarHtml() located here:
app/design/frontend/default/default/template/catalog/product/mostviewedlist.phtml
Can't show the pagination in mostviewedlist.phtml.
Can someone please help me solve this problem?
Any help would be greatly appreciated.
Your Block should be like this:
class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract
{
public function __construct(){
parent::__construct();
$storeId = Mage::app()->getStore()->getId();
$collection = Mage::getResourceModel('reports/product_collection')
->addViewsCount();
$collection->getSelect()->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
$this->setProductCollection($collection);
}
protected function _prepareLayout()
{
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'mostview.pager')
->setCollection($this->getProductCollection());
$this->setChild('pager', $pager);
$this->getProductCollection()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
}
Your template should be like this:
<?php $_collection = $this->getProductCollection(); ?>
<!-- top pagination -->
<?php echo $this->getPagerHtml(); ?>
<?php if($_collection->getSize()): ?>
...
<?php foreach ($_collection as $_item): ?>
...
<?php endforeach; ?>
<?php endif ?>
<!-- bottom pagination -->
<?php echo $this->getPagerHtml(); ?>
If you want to display toolbar. Your block should be like this:
(NOTE: I didn't test this code)
class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract
{
public function __construct(){
parent::__construct();
$storeId = Mage::app()->getStore()->getId();
$collection = Mage::getResourceModel('reports/product_collection')
->addViewsCount();
$collection->getSelect()->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
$this->setProductCollection($collection);
}
protected function _prepareLayout()
{
parent::_prepareLayout();
$toolbar = $this->getLayout()->createBlock('catalog/product_list_toolbar', microtime())
->setCollection($this->getProductCollection());
$pager = $this->getLayout()->createBlock('page/html_pager', microtime());
$toolbar->setChild('product_list_toolbar_pager', $pager);
$this->setChild('toolbar', $toolbar);
$this->getProductCollection()->load();
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('toolbar');
}
}
Related
I call the index() method of the collaborating controller, it automatically inserts 6 records into the database, and the action of logging into the database is not in the cadastrarquestao() method of the class, and not in the index method . I'm using a template library.
Controller Colaborador:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Colaborador extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('funcoes_helper');
$this->load->helper('file');
$this->load->helper('cookie');
$this->load->library('session');
$this->load->library('controle_acesso');
$this->load->model('resumoapp_model', 'resumo');
$this->load->model('homeapp_model', 'homeapp');
$this->load->model('simulado_model', 'simulado');
$this->controle_acesso->acesso();
$this->output->enable_profiler(TRUE);
}
public function index() {
$dados['teste'] = 1;
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function logout() {
session_unset();
redirect ('/entrar');
}
public function cadastrarquestao() {
$this->simulado->envioquestao(1);
$dados['mensagem'] = "dados cadastrados com sucesso!";
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function materia() {
}
}
part of the code
public function envioquestao($dados) {
$data = [
'Id_usuario' => $this->session->userdata('id_usuario'),
];
$this->db->insert('teste',$data);
}
librarie Template:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template {
var $template_data = array();
function set($name, $value)
{
$this->template_data[$name] = $value;
}
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{
$this->CI =& get_instance();
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
return $this->CI->load->view($template, $this->template_data, $return);
}
}
View template:
<html lang="pt-br">
<?php
$this->load->view('app/header');
?>
<body>
<div class="wrapper">
<?php
$this->load->view('app/topo');
?>
<?php
$this->load->view('app/menu');
?>
<?php echo $contents ?>
<footer class="footer-container"><span>© </span></footer>
</div>
<?php
$this->load->view('app/footer');
?>
</body>
</html>
the correct one would be to insert it in the database after clicking on 'register' which is in the cadastrarquestao() method
i am newbie in codeigniter and i want to apply pagination on my table. i followed a tutorial but on view my pagination is not seen. my table has already six rows of data. i am getting successfully all data but pagination not working.
here is my controller code:
class news_cont extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library("pagination");
$this->load->helper('url');
$this->load->model('Post_newspaperjob_model','pnm');
}
public function index()
{
$this->load->library("pagination");
$config['base_url'] = base_url().'news/news_cont/index';
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['total_rows'] = $this->db->get('pp_jobcategories')->num_rows();
$this->pagination->initialize($config);
$data['query'] = $this->db->get('pp_jobcategories',$config['per_page'],$this->uri->segment(3));
$this->load->view('newspapers/news',$data);
}
}
my model code :
public function getallcategories()
{
$query = $this->db->query('select * from pp_jobcategories');
return $query->result();
}
my view code :
<?php
foreach($query->result() as $row){
echo "<p>" . $row->j_name . "</p>";
}
?>
<?php echo $this->pagination->create_links(); ?>
controller: Test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url'));
$this->load->model('Fetch_data');
}
public function agriculture_exam()
{
$stream = 'agriculture';
$data['exam'] = $this->Fetch_data->top_agriculture_exams($stream);
$this->load->view('header',$data);
}
}
view: header.php
<ul class="list">
<?php
foreach($exam as $row)
{
echo "<li><a href='#'>".$row['exam_name']."</a></li>";
}
?>
</ul>
model: Fetch_data.php
<?php
class Fetch_data extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function top_agriculture_exams($stream)
{
$this->db->select('exam_name');
$this->db->from('all_exams_details');
$this->db->where('field',$stream);
$this->db->order_by('exam_name');
$this->db->limit('10');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
I am new in ci. In controller i.e. Test.php I am defining $stream='agriculture' and pass $stream variable to top_agriculture_exams model. Now, when I am fetching data on header.php file it show me a Message: Undefine variable: $exam I don't know why. So, how can I fix it ?please help me.
Thank You
Please check my below code. This will help you.
<ul class="list">
<?php
if ($exam !== NULL):
foreach ($exam as $row):
echo "<li><a href='#'>" . $row['exam_name'] . "</a></li>";
endforeach;
endif;
?>
</ul>
Also cna you please put this code in View and share result.
<?php
var_dump($exam);
?>
Also check your model code below.
<?php
class Fetch_data extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function top_agriculture_exams($stream)
{
$this->db->select('exam_name');
$this->db->from('all_exams_details');
$this->db->where('exam_name',$stream);
$this->db->order_by('exam_name');
$this->db->limit('10');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
instead of accessing it using the element array try accessing it using object.
change below code in your Fetch_data.php file.
//$result = $query->result_array();
$result = $query->result();
and access it by object in your header.php. here's an example below.
<ul class="list">
<?php foreach($exam as $row): ?>
<?= "<li><a href='#'>" . $row->exam_name . "</a></li>" ?>
<?php endforeach;
</ul>
where exam_name is equal to the column in your database.
This is the complete code (I want to select the view through a variable set on the MY_Controller):
I would like to pass that variable but it doesn't "reach" the view it gives me
$ses_group = "not_logged_in" with a test echo and I'm not setting anything on the MY_Controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
public function __construct()
{
parent::__construct();
}
function index() {
if (logged_in() == TRUE)
{
if (in_group('users'))
{
$this->special_data['ses_group'] = 'users';
}elseif (in_group('empresas'))
{
$this->special_data['ses_group'] = 'empresas';
}elseif (in_group('admin'))
{
$this->special_data['ses_group'] = 'admin';
}else{
// $this->special_data['ses_group'] = 'not_logged_in';
}
}
return $this->special_data;
}
The Main_Controller:
function index(
$data = array(
'ses_group' => $this->special_data
);
$this->load->view('auth/descricao_anuncio', $data);
)
The view:
<?php if($ses_group="not_logged_in"){ ?>
<li>Login</li>
<?php }elseif($ses_group="users"){ ?>
<li>Your Area</li>
<li>Logout</li>
<?php }elseif($ses_group="empresas"){ ?>
<li>Empresa</li>
<li>Logout</li>
<?php }?>
echo $ses_group;
Thanks againg!
Controller
<?php
class MY_Controller extends CI_Controller {
protected $special_data = array();
function MY_Controller () {
parent::Controller();
}
function special_data($val)
{
if(a){
$this->special_data['ses_group'] = 'users';
}elseif(b){
$this->special_data['ses_group'] = 'companies';
}else{
$this->special_data['ses_group'] = 'admin';
}
return $this->special_data;// return value of the function
}
}
You can send your value from controller to views by using $this-> in your MY_Controller
Controller
class Main_controller extends MY_Controller {
function __construct() {
parent::MY_Controller();
}
$this->data['group']= $this->special_data['ses_group'];// call function and pass parameter
$this->load->view('view_x', $this->data);
}
Views
<?php echo $group; ?>
Is this what you need
controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
function special_data($val)
{
if($val=="a"){
$this->special_data = 'users';
}elseif($val=="b"){
$this->special_data = 'companies';
}else{
$this->special_data = 'admin';
}
return $this->special_data;// return value of the function
}
}
class Main_controller extends MY_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$data = array(
'group' => $this->special_data('a')
);
$this->load->view('view_x', $data);
}
}
view
<?php echo $group; ?>
I am trying to work through the CodeIgniter tutorial and my news pages won't output data from the foreach loop. I get the following messages:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: pages/index.php
Line Number: 1
and
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/index.php
Line Number: 1
This is my model class:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
}
And my controller:
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
And the first view:
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
and the second:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
I know there are other questions about the tutorial but none seemed to help me.
Thanks.
in model you have closed your class after constructor. Should be closed after all function.
Also view() is initialized twice.