<?php echo $this->pagination->creaerte_links(); ?> Pagination not working in codeigniter - codeigniter

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

Related

Message: Undefine variable: exam

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.

Errors with foreach loop on the CodeIgniter tutorial

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.

Codeigniter model not loading

I'm a newbe in codeigniter.
something is going wrong with I think the model.
this is the controler:
<?php
class Fuel extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('html');
$this->load->library('table');
}
public function image() {
$data['title'] = 'test';
$data['main_content'] = 'imagetest';
$this->load->view("template", $data);
}
public function overview() {
$this->load->model('Get_DB');
$this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
when I load the image function, it works just fine, but the function overview is the problem.
this is my model:
<?php
class Get_DB extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function overzicht() {
$query = $this->db->query("SELECT * FROM invoer "
. "ORDER BY datum DESC");
$gen_query = $this->table->generate($query);
return $gen_query;
}
}
and this is my view:
<?php
echo $gen_query;
and if you want to know: my template is this:
<?php
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
now when I open my view I get this message:
A PHP Error was encountered
Severity: Notice Message: Undefined variable: gen_query Filename:
views/overzicht.php Line Number: 3
in the model you see that I have made a var $gen_query
so why is that undifined?
regards,
Ralph
Try:
public function overview() {
$this->load->model('Get_DB');
$data = array();
$data['gen_query'] = $this->Get_DB->overzicht() ; #corrected model function and save the result in `gen_query`
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
In Controler:
public function overview() {
$this->load->model('Get_DB');
$result = $this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$data['re'] = $result;
$this->load->view("template", $data);
}
And In view page you can retrive the result
like
foreach($re->result() as $row)
{
//You can get each row data here $row->your_field_names
}

magento - How to create page with pagination in magento 1.7

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');
}
}

passing data in model trough controller in codeigniter

I've heard about codeigniter couple of times so i was curious and thought why not. I took some tutorials and was very delighted to see how the framework worked.
Now i've the following problem i want to passing the data i've made in my model trough my controller and showing this in my view but i always run to the folowing error: Fatal error: Call to a member function query() on a non-object in C:\wamp\www\codeigniterTest\application\models\leden_model.php on line 9. The funny thing about this error is, when i google on this issue a lot of forum topics is about this issue but nowhere i get the right answer. my code looks like this.
codegniter version 2.03
class Leden extends CI_Controller {
function __construct(){
parent::__construct();
}
function index()
{
$this->load->model('leden_model');
$ledenModel = new Leden_model();
$data = $ledenModel->allLeden();
$this->load->view('leden_overzicht',$data);
}
}
<?php
class Leden_model extends CI_Model {
function __construct(){
parent::__construct();
}
function allLeden(){
$query = $this->db->query("SELECT * FROM leden");
foreach ($query->result_array() as $row)
{
echo $row['Naam'];
echo $row['Achternaam'];
echo $row['Email'];
}
return $query;
}
}
?>
When i'm doing the query in my controller then i'm getting the results i want, why not in my model?
my question is what am i doing wrong?
Leden_model.php
?php
class Leden_model extends CI_Model {
function __construct(){
parent::__construct();
}
function allLeden()
{
$data = array();
$this->db->select();
$query = $this->db->get('leden');
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Leden_controller.php
?php
class Leden extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('leden_model');
}
function index() {
$data['leden_data'] = $this->ledenModel->allLeden();
$this->load->view('leden_overzicht',$data); }
}
leden_overzicht.php
?php
if (count($leden_data))
{
foreach ($leden_data as $key => $list)
{
echo $list['Naam'] . " " . $list['Achternaam'] . " " . $list['Email'] . "";
} }
else {
echo "No data."; }
did you load database? example:
$this->load->database();

Resources