Basically Im using Bootstrap themes with Codeigniter
Here is my controller code. tutors is the table from database.
public function index() {
$this->load->library('pagination');
$config['base_url'] = 'http://localhost:8888/bootstrap/index.php/frontpage/index';
$config['total_rows'] = $this->db->get('tutors')->num_rows();
$config['per_page'] = 6;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$data['records'] = $this->db->get('tutors', $config['per_page'], $this->uri->segment(3));
$this->load->view('include/header');
$this->load->view('frontpage', $data);
$this->load->view('include/footer');
}
I want to customize the view as the followings
<div class="container" style="text-align: center;">
<?php
$i=0;
foreach($records as $row) :
if ($i%3==0) { ?>
<div class="row">
<div class="col-md-4">
<h1><?php echo $row->title; ?></h1>
<h2><?php echo $row->id; ?></h2>
</div>
<?php
} else {
?>
<div class="col-md-4">
<h1><?php echo $row->title; ?></h1>
<h2><?php echo $row->id; ?></h2>
</div>
<?php
if ($i%3==2) {
echo "</div>";
}
}
$i++;
endforeach;
?>
</div>
<div class="container">
<?php echo $this->pagination->create_links(); ?>
</div>
But it gets an error. How can I modify it?
Thanks guys.
Try to use
foreach ($query->result() as $row)
instead of foreach($records as $row)
Related
I tried to use the codeigniter pagination library and it doesn't work for me. I'm trying to have 24 products on the all products page and when there's more products it should add a link to the next page. So right now I have more than 24 products on the site and no new page is added.
with this line I'm getting all the products from product table:
$data['products'] = $this->Product_model->selectProducts();
This is my controller function:
public function pagination() {
//load pagination library
$this->load->library('pagination');
//laad Products_model voor pagination
$this->load->model('Products_model');
$this->load->model('Product_model');
$config = array();
$config["base_url"] = base_url() . "AlleCadeausController/pagination";
$config["total_rows"] = $this->products->record_count();
$config["per_page"] = 24;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['products'] = $this->Product_model->selectProducts();
$data["links"] = $this->pagination->create_links();
$this->load->view("allecadeaus", $data);
}
My model (Products_model):
<?php
class Products extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("products");
}
public function fetch_products() {
$query = $this->db->get("products");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
This is my view file (I don't know if I need to add anything in the view file for the pagination to work):
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="all-content">
<div class="row">
<div class="col-lg-12 bg-warning header-text" style="font-size:25px;font-weight: bold;">
<center>Alle cadeaus</center>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="row">
<div class="menu">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<div class="categorieen">
<?php foreach (get_categories_h() as $category) : ?>
<input id="box1" type="checkbox" />
<label for="box1"><a class="listcat" href="<?php echo base_url().'Product/category/' . $category->id; ?>"> <?php echo $category->name; ?></a></label>
<?php endforeach; ?>
</div>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="main">
<?php foreach($products as $product) : ?>
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>"> <div class="main-products">
<img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
<div class="product_naam"><h3><?php echo $product["product_naam"]; ?></h3></div>
<div class="ophaal_plaats">
<?php echo $product["ophaal_plaats"]; ?>
</div>
</a>
<div class="aangeboden_door">
<?php
//Here is the active record query which is getting the 'voorname' and other data
$userarray = $this->db->get_Where('users', array('user_id'=>$product["user_id"]))->row_array();
// you can print_r($userarray); for see the array you get
?>
<a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
<td><?php echo $userarray['voornaam'];?></td>
</tr>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php');?>
Below is the fine working pagination code, hope it will fix the issue you have.
Model
public function fetch_oldusers($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get_where('dc_user',array('Is_Hidden'=>0));
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
View
<div class="text-center"><nav aria-label="Page navigation">
<ul class="pagination">
<li><?php echo $links; ?></li>
</ul>
</nav></div>
Controller
public function Users()
{
$config = array();
$config["base_url"] = base_url() . "master/Users";
$config["total_rows"] = $this->bm->record_countolduser();
$config['use_page_numbers'] =True;
$config['cur_tag_open'] = '<a><b class="text-success">';
$config['cur_tag_close'] = '</b></a>';
$config["per_page"] =10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['title'] = "Users List";
$data["posts"] = $this->bm->fetch_oldusers($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('Admin/header',$data);
$this->load->view('Admin/nav');
$this->load->view('Admin/sidebar');
$this->load->view('Admin/old_user', $data);
$this->load->view('Admin/footer');
}
It is working fine for me.
I'm trying to make pagination using Codeigniter builtin pagination library but do not get results from database in view.
I dont know where I'm wrong.
If someone can help me, I will post my code below. Thanks
Model
public function showAll($limit, $offset){
$this->db->limit($limit);
$this->db->offset($offset);
return $this->db->get('jobs')->result();
}
public function countAll($value='')
{
return $this->db->get('jobs')->num_rows();
}
Controller
$total = $this->JobsM->countAll();
$limit = 3;
$offset = $page;
$config['base_url'] = base_url('jobs/');
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$data['myData'] = $this->MusiciansM->myProfile();
$data['jobList'] = $this->JobsM->showAll($offset, $limit);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('user/header', $data);
$this->load->view('public/job-list', $data);
$this->load->view('public/footer');
View
<div class="row">
<?php if(isset($jobList)):?>
<?php foreach($jobList as $job): ?>
<div class="col-md-12 job-item">
<a href="<?php echo base_url().'jobs/'.$job->id.'/'.url_title($job->title);?>" class="thumbnail">
<h3 class="job-title"><?php echo word_limiter($job->title, 5);?> <small> <?php echo $job->musicianType;?> </small></h3>
<p class="description"><?php echo character_limiter($job->description, 200);?></p>
<?php $date = date('d-m-Y', strtotime($job->date));?>
<p><small class="dateandplace"> <?php echo $date;?> </small></p>
</a>
</div>
<?php endforeach;?>
<?php endif;?>
</div>
<!-- Pagination -->
<div class="row pagination">
<div class="col-md-12">
<?php print_r($pagination) ;?>
</div>
</div>
<!--/ Pagination -->
Routes
$route['jobs/(:any)'] = 'jobs';
$route['jobs/(:num)/(:any)'] = 'jobs/preview/$1';
Thank you
$this->pagination->create_links(); returns pagination in form of string.
Printing the string in print_r is wrong.
<!-- Pagination -->
<div class="row pagination">
<div class="col-md-12">
<?php print_r($pagination) ;?>
</div>
</div>
<!--/ Pagination -->
The correct form is:
<!-- Pagination -->
<div class="row pagination">
<div class="col-md-12">
<?php echo $pagination; ?>
</div>
</div>
<!--/ Pagination -->
Normally when I get the data from my database I there are arranged in the order of their id.However, I have created a column called 'sales' which is also an integer
and I would like my data to be arranged in ascending order according to the sales.Here is my code:
My Model
public function get_bestseller($id = FALSE) {
if ($id === FALSE)
{
$query = $this->db->get('submit');
return $query->result_array();
}
$query = $this->db->get_where('submit', array('id' => $id));
$this->db->order_by('sales', 'DESC');
return $query->result_array();
}
My View
<div class="tab-pane fade" id="faq-cat-2">
<div class="panel-group" id="accordion-cat-2">
<?php foreach ($bestseller as $bestseller_item): ?>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img class="lazy" src="<?php echo base_url() ?>Images/last.gif"
data-original="<?php echo base_url() ?>uploads/<?php echo $bestseller_item['imgname']; ?>"
style="width: 350px; height: 150px">
<div class="caption">
<?php if ($bestseller_item['Price'] == 0): ?>
<h4 class="pull-right"><p class="text-success">Free</p></h4>
<?php else: ?>
<h4 class="pull-right">$<?php echo $bestseller_item['Price']; ?></h4>
<?php endif; ?>
<h4>
<a href="<?php echo site_url('submit/view/' . $bestseller_item['id']); ?>"
target="_blank"><?php echo $bestseller_item['Title']; ?></a>
</h4>
<p><?php echo $bestseller_item['textarea']; ?></p>
</div>
<p class="text-right"><b
class="author">By: <?php echo $bestseller_item['Author']; ?></b>
</p>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
My Controller
public function index()
{
$data['bestseller'] = $this->Submit_Database->get_bestseller();
$this->load->view('templates/header');
$this->load->view('pages/home', $data);
$this->load->view('templates/footer');
}
$query = $this->db->order_by('sales', 'DESC')->get_where('submit', array('id' => $id));
You are adding the order by clause after the query has already ran.
I have an error in the product detail, I do not know what else to change where again. when I change $detail into $categories succeed but is the same as function submenu. It was not according to my wishes
My View
<!-- navbar -->
<div class="row">
<?php foreach ($categories as $row) : ?>
<div class="col-sm-3">
<h5><?php echo $row->categoriesName; ?></h5>
<ul><li><a href="<?php echo base_url();?>member/detail">
<?php echo $row->productName; ?></a> </li></ul>
</div>
<?php endforeach; ?>
</div>
<!-- product detail -->
<?php foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; ?>
My Controller
public function home() {
$data=array('title'=>'Pasar Online | Ayo Belanja Sekarang',
'username'=> $this->session->userdata('username'),
'product' => $this->product_model->all(),
'categories' => $this->categories_model->get_main(),
'isi' =>'member/index');
$this->load->view('template_home/wrapper',$data);
}
public function detail() {
$data['username'] = $this->session->userdata('username');
$data['categories'] = $this->categories_model->get_main();
$data['detail'] = $this->categories_model->get_details();
$this->load->view('member/coba',$data);
}
My Model
public function get_main(){
$this->db->select('product.*,categories.*');
$this->db->from('product');
$this->db->join('categories','product.categoriesID=categories.categoriesID');
$this->db->limit(5);
$query = $this->db->get();
if($query->num_rows() > 0) {
$results = $query->result();
} return $results;
}
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
}
still can not display a single product
I do not know where my mistake... please help me, thanks
you are iterating details array in wrong format.
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
This will return only single row without any indexing like - 0,1,2...
and on view page you are using foreach loop and foreach loop use indexing to iterate value you can iterate like that --
<div class="box">
<h1 class="text-center"><?php echo $detail->productName; ?></h1>
<p class="price"><?php echo $detail->price; ?></p>
</div>
please use this way hope it will work..
In codeigniter row() selects only first row of your result in object format.So no need to use foreach loop.Just try like this...
<div class="box">
<h1 class="text-center"><?php echo $detail->productName;?></h1>
<p class="price"><?php echo $detail->price;?></p>
</div>
And
In model
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
it is because of $detail variable is empty, no data available in this variable, always check for data in variable before looping
<?php if(!empty($detail)){
foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; } ?>
I have a problem using codeigniter pagination. The problem is when I open the 2nd page during paging it is showing the error 404 page not found. I do not know why but for the 1st page it is working.
Model:
public function home($limit='',$segment=''){
$data = array();
$this->db->where(array('produk.status_produk'=>'1'))->limit($limit,$segment);
$i=0;
foreach ($this->get_all() as $item){
$data[$i] = $item;
$this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
if($this->db->get('foto_produk')->num_rows() > 0){
$this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
$foto = $this->db->get('foto_produk')->result();
foreach ($foto as $pic) {
$data[$i]->image = $pic->image;
$data[$i]->thumb = $pic->thumb;
}
} else {
$data[$i]->image = '';
$data[$i]->thumb = '';
}
$i++;
}
return $data;
}
Controller:
$cari = $this->input->post('search');
$this->db->like('deskripsi_produk', $cari);
$this->db->or_like('nama_produk', $cari);
$this->db->order_by('id_produk','desc');
$t = $this->produk_m->home();
$this->data->search = $this->input->post('search');
$this->load->library('pagination');
$perPage = 2;
$uri_segment = 2;
$total = count($t);
$config['base_url'] = site_url('search');
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = '2';
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$this->data->pagination = $this->pagination->create_links();
$this->db->like('deskripsi_produk', $cari);
$this->db->or_like('nama_produk', $cari);
$this->db->order_by('id_produk','desc');
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));
$this->template->set_judul('PT Bintang Sriwijaya')
->set_css('style')
->set_parsial('sidebar','sidebar_view',$this->data)
->set_parsial('topmenu','top_view',$this->data)
->render('search',$this->data);
}
View:
<h2>Hasil Pencarian</h2><br />
<?php if($produk):?>
<h3><center><?php echo $pagination;?></center></h3><br />
<?php foreach($produk as $item): ?>
<div class="produk-wrap">
<div class="image-wrap">
<div class="image-iner">
<a href="<?php echo base_url().'index.php/produk/'.$item->nama_kategori.'/'.$item->url_produk;?>">
<?php if($item->thumb == ''): ?>
<div class="no-image">
<span>Belum ada Gambar</span>
</div>
<?php else: ?>
<img src="<?php echo base_url().$item->thumb ?>" />
<?php endif; ?>
<?php if ($item->stok == 0): ?>
<div class="trans">
<span>Stok Habis</span>
</div>
<?php endif; ?>
</div>
</div>
<div class="produk-name">
<?php echo $item->nama_produk;?>
</div>
<?php if($item->harga_baru != 0): ?>
<div class="harga-lama">
Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
</div>
<div class="harga-jual">
Rp. <?php echo $this->cart->format_number($item->harga_baru) ?>
</div>
<?php else: ?>
<div class="harga-jual">
Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
</div>
<?php endif; ?>
</a>
</div>
<?php endforeach; ?>
<?php else:
echo "Data yang dicari belum ada";
endif;?>
please change these lines
$uri_segment = 2;
to
$uri_segment = 3;
and
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));
to
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(3));
and try beacause segment 1 is for controller, and 2 for methods in it, 3 may be the pagination element.