Helo. I’m trying to make a items pagination. I have 3 function, first displaying category, 2nd displaying sucategory. 3rd displaying items is cold get_books_by_subcategory. 3rd function get a segment->url(3) argumnet, i want to make a pagination in the same function. but i cant do it.
This is functions code in controller:
function get_category()
{
$query = $this->Kategorie_model->get_category();
$this->response['podkategorie'] = '';
$this->response['kategorie'] = '';
$podkategorie = '';
if($query->num_rows() > 0)
{
foreach($query->result() as $item)
{
$podkategorie = $this->get_sub_category($item->CAT_ID);
$this->response['kategorie'] .= $this->load->view('Ksiegarnia/left', array('kategorie' =>$item, 'podkategorie'=>$podkategorie), true);
}
}
$data = $this->response['kategorie'];
return $data;
}
function get_sub_category($id)
{
$this->response['wynik'] = '';
$query = $this->Kategorie_model->get_sub_category($id);
if($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$link = site_url('ksiegarnia/get_books_by_subcategory/'.$row->SUBC_ID);
$this->response['wynik'] .= '<div class="subcat_name">'.$row->SUBC_Name.'</div>';
}
}
else
{
$this->response['wynik'] = '<H1>BRAK DANYCH </H1>';
}
return $this->response['wynik'];
}
function get_books_by_subcategory()
{
$widok['center'] = '';
$widok['left'] = $this->get_category();
$widok['right'] = $this->load->view('Ksiegarnia/right', '', true);
$id = $this->uri->segment(3);
if(isset($id) and is_numeric($id))
{
$query = $this->Kategorie_model->get_books_by_subcategory($id, $this->uri->segment(4));
if($query->num_rows() > 0)
{
foreach($query->result() as $item)
{
$widok['center'] .= $this->load->view('Ksiegarnia/get_books', array('data' =>$item), true);
}
}
else
{
$widok['center'] = $this->load->view('Ksiegarnia/get_books', array('tytul' =>'<h1>brak danych</h1>'), true);;
}
$widok['center'] .= $this->pagination->create_links();
$this->load->view('Ksiegarnia/index', $widok);
}
}
And this is my model:
function get_books_by_subcategory($id, $offset=0)
{
$config['base_url'] = 'http://lukaszbielecki.cba.pl/ksiegarnia/CI/index.php/ksiegarnia/get_books_by_subcategory/'.$id;
$config['per_page'] = 7;
$this->db->where('SUB_CATEGORY_SUBC_ID', $id);
$config['total_rows'] = $this->db->get('books')->num_rows();
$config['num_links'] = 20;
$this->pagination->initialize($config);
return $this->db->get('books',$config['per_page'],$offset);
//$wynik = $this->db->query("Select * from books where SUB_CATEGORY_SUBC_ID = '".$id."'");
//return $wynik;
}
The arguments in url is changing, but dispalyin only a items from first subcategory.
Help, please.
On this line $widok['center'] = $this->load->view('Ksiegarnia/get_books', array('tytul' =>'<h1>brak danych</h1>'), true);; in your controller, you have an additional semicolon ; that is causing a syntax error.
Related
Hi I need hel I'm trying to get pagination with search results.
I don't know what I am doing wrong. Because my results don't show up.
Here is my Model
function search_posts($query, $pagina, $por_pagina)
{
$this->db->select();
$this->db->from('posts');
$this->db->like("post_title", $query, 'both');
$this->db->or_like("post", $query, 'both');
$this->db->order_by('post_id', 'desc');
$this->db->limit($pagina, $por_pagina);
$query = $this->db->get();
return $query->result_array();
}
public function contar_filas()
{
$this->db->select();
$this->db->from('posts');
$query = $this->db->count_all_results();
return $query;
}
Here is my Controller
public function buscar()
{
$this->load->model('M_Articulos');
$data['posts'] = '';
$data['sin_resultados'] = '';
$query = trim($this->input->get('query', TRUE));
if (sizeof($query) == 0)
{
show_404();
}
if ($query)
{
if ($this->uri->segment(3))
{
$pagina = $this->uri_segment(3);
}
else
{
$pagina = 0;
}
$por_pagina = 4;
$data['posts'] = $this->M_Articulos->search_posts($query, $pagina, $por_pagina);
if ($data['posts'] != FALSE)
{
$data['posts'] = $this->M_Articulos->search_posts($query, $pagina, $por_pagina);
$config['base_url'] = base_url() . 'admin/buscar?query=' . $query;
$config['per_page'] = $por_pagina;
$config['total_rows'] = $this->M_Articulos->contar_filas();
$config['uri_segment'] = 3;
$config['enable_query_strings'] = TRUE;
$config['query_string_segment'] = 'page';
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
}
else
{
$data['sin_resultados'] = 'nada';
}
}
else
{
$data['sin_resultados'] = 'nada';
}
$this->load->view('admin/layouts/header');
$this->load->view('admin/modules/buscar', $data);
$this->load->view('admin/layouts/footer');
}
does anyone know how to make it work?
I'm going to be more specific. What i want is that when i search somethin in search page there is a pagination. Thanks
I'm unable to retrieve similar post data from db with codeigniter. In my blog, I have a tags field which is keeping data like 'php,mysql,mongo,java,jquery'
I just try to get similar post which is related with current posts tags. But im not getting expected result. and the problem is in my query. Its show only three post and that is 1st, last, and number 3rd one.
[CONTROLLER]
public function showpost()
{
$data = array();
$this->load->view('header',$data);
$data['post'] = $query->result();
$data['similar'] = $this->crudModel->getSimilarPost();
$this->load->view('showfull',$data);
$this->load->view('footer');
}
[MODEL]
public function getSimilarPost()
{
$query = $this->db->get_where('blogs',array('id' => $this->uri->segment(3)));
foreach($query->result() as $row){ $tags = $row->tags; }
$match = explode(',', $tags);
for($i = 0; $i < count($match); $i++)
{
$this->db->like('tags',$match[$i]);
$this->db->from('blogs');
$sqlQuery = $this->db->get();
}
return $sqlQuery->result();
}
[VIEW]
foreach($similar as $row)
{
echo($row->btitle.'<br/>');
}
Try this.
public function showpost()
{
$data = array();
$this->load->view('header',$data);
$data['post'] = $query->result(); // why this line??
$data['similar'] = $this->crudModel->getSimilarPost();
$this->load->view('showfull',$data);
$this->load->view('footer');
}
[MODEL]
public function getSimilarPost()
{
$query = $this->db->get_where('blogs',array('id' => $this->uri->segment(3)));
foreach($query->result() as $row){ $tags = $row->tags }
$match = explode(',', $tags);
$result = [];
for($i = 0; $i < count($match); $i++)
{
$this->db->like('tags',$match[$i]);
$this->db->from('blogs');
$sqlQuery = $this->db->get();
if($sqlQuery->num_rows()>0)
$result[] = $sqlQuery->result();
}
return $result;
}
[VIEW]
$check = [];
foreach($similar as $row)
{
foreach($row as $data)
{
if(!in_array($data->btitle,$check))
{
$check[] = $data->btitle;
echo $data->btitle.'<br/>';
}
}
}
I am trying to get the count() of my sql table. However, I keep getting this error.
Call to a member function master_banner() on null
How can I go about changing my code?
Controller
public function index()
{
//$this->landing_banners_upload();
$config["total_rows"] = count($this->Backend_banner_model->master_banner_list());
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$data['per_page']= $config["per_page"];
$data["no_pages"] = $config['total_rows']/$config['per_page'];
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$offset=$page+$config["per_page"];
$data['banners']=$this->Backend_banner_model->master_banner($page,$offset);
count($data['banners']);
$data["links"] = $this->pagination->create_links();
echo $data["links"];
$this->load->view('layout/header');
$this->load->view('backendBanner/landing_banners_upload_view', $data);
$this->load->view('layout/footer');
}
Model
public function master_banner_list(){
$sql="select * from banner";
$query = $this->db->query($sql);
$data = array();
if ($query->num_rows() > 0){
foreach ($query->result() as $row) {
$data[] = $row;
}
}
return $data;
}
public function master_banner($start,$offset){
$sql="SELECT * FROM banner
WHERE RowNum > '".$start."' AND RowNum <= '".$offset."'";
$query = $this->db->query($sql);
$data = array();
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
Controller[In Article Page Article Properly work with pagination, store user email id in 'articles' database , now i tried to get the user firstname, and lastname from users table but not work properly ]
public function articles()
{
$data['title'] = "Articles";
$config = array();
$config["base_url"] = base_url() . "sd/articles/";
$config["total_rows"] = $this->model_users->record_count_articles();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->model_users->fetch_result_articles($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if ($this->session->userdata ('is_logged_in')){
$data['profile']=$this->model_users->profilefetch();
$this->load->view('sd/header',$data);
$this->load->view('sd/articles', $data);
$this->load->view('sd/footer', $data);
} else {
$this->load->view('sd/sdheader', $data);
$this->load->view('sd/articles', $data);
$this->load->view('sd/sdfooter', $data);
}
}
Model [ Get Users Name in Article Page ]
public function record_count_articles() {
return $this->db->where('status','1')->count_all("articles");
}
public function fetch_result_articles($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->where('status','1')->order_by('id', 'DESC')->get("articles");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Add These Lines [ But Not Work]
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
$query = $this->db->select('firstname')->select('lastname')->where('email',$data[0]->email)->get("users");
$data['name_info']=$query->result_array();
}
return $data;
}
return false;
You have 2 problem here. please have a look on comments in code.
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
//1) $data[0]->email keep repeating same email.
// inner $query variable should be different.
$innerQuery = $this->db->select('firstname,lastname')->where('email',$row->email)->get("users");
//2) you need to store query result on array.
// $data['name_info'] stores only single record.
$data[]=$innerQuery ->result_array();
}
return $data;
}
return false;
You should avoid query in loop if you can achieve it by join
EDIT: Lets try this with join
public function fetch_result_articles($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db
->join('users u','u.email = a.email','left')
->where('a.status','1')->order_by('a.id', 'DESC')->get("articles a");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
I have not tested the code. but it is better way than loop.
i am getting error in my program. in this program i want to make if periode active show data when the periode active but when periode not active not show data anything. but i try this condition i am getting error Trying to get property of non-object in model.
this is modal
function get_id_periode_open()
{
$this->db->where('tgl_dari <= curdate()');
$this->db->where('tgl_sampai >= curdate()');
// $this->db->where('status', 'open');
$row = $this->db->get('periode')->row();
$id_periode = $row->id_periode; //line error
return $id_periode;
}
this is my controller
function index($nama = '',$period = 0,$geup = 0)
{
$this->load->model('mukt');
$id_periode = $this->mukt->get_id_periode_open();
$this->data['id_periode_sekarang'] = $id_periode;
if ($id_periode)
{
$this->data['ukt2'] = $this->mukt->get_ukt($perpage,$offset);
}
else
{
echo "string";
$this->data['contents'] = '<div class="twelve wide column">Tidak ada periode aktif</div>';
}
if (!$nama AND $this->input->post('nama'))
{
$nama = $this->input->post('nama');
}
else if(!$nama)
{
$nama = 0;
}
if ($this->input->post('id_periode')){
$period = $this->input->post('id_periode');
}
else if(!$period)
{
$period = 0;
}
if ($this->input->post('geup')){
$geup = $this->input->post('geup');
}
else if(!$geup)
{
$geup = 0;
}else{
$geup = urldecode($geup);
}
// echo $period. urldecode($geup). $nama;
$count = $this->mukt->get_count_peserta_ukt($period, urldecode($geup), $nama);
// print_r($count);
// exit;
// Set up pagination
$offset = 0;
$perpage = 1;
$uri_segment = 6;
if ($count > $perpage) {
$this->load->library('pagination');
$this->load->config('pagination');
$config = $this->config->item('pag');
$config['base_url'] = site_url('pelatih/ukt/'.$nama.'/'.$period.'/'.$geup);
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = $uri_segment;
$config['first_page'] = 'Awal';
$config['last_page'] = 'Akhir';
$config['next_page'] = '«';
$config['prev_page'] = '»';
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
if($this->uri->segment($uri_segment)){
$offset = $this->uri->segment($uri_segment);
}
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
$this->data['periode'] = $this->mperiode->get_periode();
if ($period AND $geup)
{
$this->data['ukt2'] = $this->mukt->get_cari_peserta_ukt($period, $geup ,$nama,$perpage,$offset);
}
else if($nama && (!$period || !$this->input->post('geup')))
{
$this->data['ukt2'] = $this->mukt->get_cari_peserta_ukt("", "",$nama,$perpage,$offset);
}
else
{
$this->data['ukt2'] = $this->mukt->get_ukt($perpage,$offset);
}
$this->data['title'] ='UKM Taekwondo | ukt';
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('pelatih/ukt/view_ukt', $this->data, true);
$this->load->view('template/wrapper/pelatih/wrapper_anggota',$this->data);
please tell me how to solved this error.
thank you.
may be your query returns no result, so try doing:
$query = $this->db->get('periode');
if( $query->num_rows() > 0 ) {
$row = $query->row();
$id_periode = $row->id_periode;
return $id_periode;
}
return 0;