i am using codeigniter, i am trying to create a page where if the url entered example.com/mobil/bekas/toyota/avanza it shows all used car that has toyota as brand and avanza as model, and if the url entered example.com/mobil/bekas/toyota it shows all used car that has toyota as brand.
Here is my controller :
public function bekas($brand_nama,$model_nama='NULL')
{
$this->load->model('listing_model');
$data['cars'] = $this->listing_model->viewListingByBrandAndModel($brand_nama, $model_nama);
$this->load->view('product_listing.php', $data);
}
Here is the model :
function viewListingByBrandAndModel($brand_nama, $model_nama)
{
$this->load->library('pagination');
$this->load->library('table');
$config['base_url'] = 'http://example.com/mobil/bekas/'.$brand_nama.'/'.$model_nama;
$config['total_rows'] = $this->db->select('*')
->join('car_list_tbl','car_list_tbl.car_list_ID = user_listing_tbl.car_list_ID')
->join('member_tbl','member_tbl.mID = user_listing_tbl.mID')
->join('model_tbl','model_tbl.model_ID = car_list_tbl.model_ID')
->join('series_tbl','series_tbl.series_ID = car_list_tbl.series_ID')
->join('body_type_tbl','body_type_tbl.body_type_nama = car_list_tbl.body_type_nama')
->join('brand_tbl','brand_tbl.brand_name = car_list_tbl.brand_name')
->where('car_list_tbl.brand_name',$brand_nama)
->like('model_tbl.model_nama', $model_nama)
->where('user_listing_tbl.listing_type','BEKAS')
->get('user_listing_tbl')->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 10;
$config['display_pages'] = TRUE;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$config['prev_link'] = false;
$config['next_link'] = false;
$config['next_tag_open'] = '<li><a href="#"><i class="fa fa-chevron-left">';
$config['next_tag_close'] = '</i></a></li>';
$config['prev_tag_open'] = '<li><a href="#"><i class="fa fa-chevron-right">';
$config['prev_tag_close'] = '</i></a></li>';
$this->pagination->initialize($config);
//Pagination End
$sql = $this->db->select('*')
->join('car_list_tbl','car_list_tbl.car_list_ID = user_listing_tbl.car_list_ID')
->join('member_tbl','member_tbl.mID = user_listing_tbl.mID')
->join('brand_tbl','brand_tbl.brand_name = car_list_tbl.brand_name')
->join('model_tbl','model_tbl.model_ID = car_list_tbl.model_ID')
->join('series_tbl','series_tbl.series_ID = car_list_tbl.series_ID')
->where('car_list_tbl.brand_name',$brand_nama)
->like('model_tbl.model_nama', $model_nama)
->where('user_listing_tbl.listing_type','BEKAS')
->get('user_listing_tbl', $config['per_page'], $this->uri->segment(5));
return $sql->result();
I am still newbie at web programming, can i have inputs on which part i am lacking ? Because it works when i type example.com/mobil/bekas/toyota/avanza but it wont show anything when i type example.com/mobil/bekas/toyota
1 ) You are passing NULL as a string in parameter
2) Please use the if condition base on the $model_name at the time of the database query. Do not pass the extra conditions in query with like model_name LIKE '';
$this->db->select('*')
->join('car_list_tbl','car_list_tbl.car_list_ID = user_listing_tbl.car_list_ID')
->join('member_tbl','member_tbl.mID = user_listing_tbl.mID')
->join('brand_tbl','brand_tbl.brand_name = car_list_tbl.brand_name')
->join('model_tbl','model_tbl.model_ID = car_list_tbl.model_ID')
->join('series_tbl','series_tbl.series_ID = car_list_tbl.series_ID')
->where('car_list_tbl.brand_name',$brand_nama);
if($model_nama){
$this->db->like('model_tbl.model_nama', $model_nama);
}
$this->db->where('user_listing_tbl.listing_type','BEKAS');
->get('user_listing_tbl', $config['per_page'], $this->uri->segment(5));
return $this->db->result();
Your function declaration has a small syntax error. The value NULL is a special value in PHP and as such, should not be surrounded in quotes. For this reason, you were querying the database for the type 'toyota' with the make 'NULL' rather than NULL.
Changing your controller code should fix the issue:
public function bekas($brand_nama,$model_nama=NULL)
{
//...
}
Related
My team and I are making a search function with pagination and the base table works fine, even the pagination works fine. The problem is when we use the search function. Upon searching, an error message pops up on the first page:
Error message on the first page, although the results for the search query shows up. But when you turn to the page 2 of the search query, error message disappears and everything is normal.
This is the url of the first page where the error above shows up:
project1/index.php/configname/view?name=smith
This is the url when you turn to the next page where there are no errors:
project1/index.php/configname/view/8?name=smith
When we manually insert a zero as before the question mark, no error shows up, like so:
project1/index.php/configname/view/0?name=smith
We figured it has something to do with the uri segment not showing up on the first page, so we are looking for a solution where the zero will be the default and will be there upon loading the view/searching.
here is our config function:
public function view()
{
$data['title'] = 'View';
$config = array();
$search = $this->input->get("name");
$config["base_url"] = base_url() . "/index.php/configname/view";
$config["total_rows"] = $this->Users_model->get_search_count($search);
$config["per_page"] = 8;
$config["uri_segment"] = 3;
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET);
$config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['first_tag_close'] = '</span></li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['prev_tag_close'] = '</span></li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['next_tag_close'] = '</span></li>';
$config['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['last_tag_close'] = '</span></li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['num_tag_close'] = '</span></li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['list'] = $this->Users_model->get_all($config['per_page'], $page, $search);
$data['links'] = $this->pagination->create_links();
$this->load->view('include/header', $data);
$this->load->view('include/sidetopbar', $data);
$this->load->view('user/viewloans', $data);
}
This is our model:
function get_all($limit, $start, $st)
{
$sql = "select * from $this->tbl where full_name like '%$st%' order by date DESC limit " . $start . ", " . $limit;
$query = $this->db->query($sql);
return $query->result();
}
function get_search_count($st = NULL)
{
if ($st == "NULL") $st = "";
$sql = "select * from $this->tbl where full_name like '%$st%'";
$query = $this->db->query($sql);
return $query->num_rows();
}
This is the line in which the error was referring to for our Pagination.php on CI, but we didn't change anything from it:
if ($this->prefix !== '' OR $this->suffix !== '')
{
$this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
}
TL;DR: Error message shows up on the first page upon using our search function. We just want to make it disappear as the search queries properly work, just the error makes us mad.
in this section
if ($this->prefix !== '' OR $this->suffix !== '')
{
$this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
}
check if $this->suffix is null set it to 1
if(!is_numeric($this->suffix)){
$this->suffix = 1;
}
CodeIgniter seems to treat the search query string as a page number suffix: when it gets the page number from the url, it removes this suffix from the page number. When the page number is missing from the url, it tries to remove this suffix from a null value, and because of the more strict type checking in php 8 this gives the error. (I don't get this error with CI3 on php 7.4.)
It's not in the documentation (found here: https://github.com/bcit-ci/CodeIgniter/pull/4384 ), but you can also set the current page for the Pagination library manually using $config['cur_page'] = ... ;
If you set this to something that isn't considered "empty" by php, CodeIgniter will use this page number instead of getting the one from the url. And if you set it to a page number that does not make sense, for example, -1, CodeIgniter will use 0 instead. So you could try setting it to -1 when the segment is missing:
$config['cur_page'] = $this->uri->segment(3) ?: -1;
Not sure if this triggers other errors on php 8 though!
I'm Quit New and trying to Make Pagination Inside Codeigniter...
I'm to create pagination with one extra argument.. link first argument is for getting ID for database search.. and than create pagination... I've done so far but the next page link is not correct as it should be. please check my code and let me know what's wrong i am doing.
this is model code..
my modelclass name is movie
public function get_movies_by_actor($actor_id,$num=10,$start=0)
{
$this->db->select(" f.film_id,
f.language_id,
a.actor_id,
f.title,
f.description,
l.name AS language,
c.name AS category,
concat(a.first_name, ' ', a.last_name ) AS actor_name
",FALSE);
$this->db->from('actor AS a');
$this->db->join('film_actor AS fa', 'fa.actor_id=a.actor_id');
$this->db->join('film AS f', 'f.film_id=fa.film_id', 'left');
$this->db->join('language AS l', 'l.language_id=f.language_id', 'left');
$this->db->join('film_category AS fc', 'f.film_id=fc.film_id', 'left');
$this->db->join('category AS c', 'c.category_id=fc.category_id', 'left');
$this->db->where('a.actor_id', $actor_id);
$this->db->order_by('f.film_id','desc')->limit($start,$num);
$query = $this->db->get();
return $query->result();
}
this and controller code.
my controller class name is movies
public function actor_movie($actor_id,$start=0)
{
$data['movies']=$this->movie->get_movies_by_actor($actor_id,$start,10);
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/movies/actor_movie/'.$actor_id.'/';
$config['total_rows'] = $this->movie->get_total_movies();
$config['per_page'] = 10;
$config['display_pages'] = FALSE;
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$config['next_tag_open'] = '<li class="next">';
$config['next_link'] = 'Older Post →';
$config['next_tag_close'] = '</li>';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_link'] = '< Newer Post';
$config['prev_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['pagination_links'] = $this->pagination->create_links();
$data['title'] = '$this->pagination->create_links()';
$this->load->view('header');
$this->load->view('actor_movies',$data);
$this->load->view('footer');
}
just you need to add uri_segment in pagination $config array in Controller Function...
$config['uri_segment'] = 4;
I am pulling my hair out on this one. I am using CodeIgniters pagination library and right now it is always stuck on page 1 as the current page. I have checked a bunch of StackOverflow questions and I don't have the same problem as anyone else.
Here is my url structure
website.com/leaders/page/[page_number]
Here is the pagination code in my controller
$this->load->library('pagination');
$config['per_page'] = $query_config['limit'];
$config['base_url'] = base_url() . 'leaders/page/';
$config['total_rows'] = 2000; // I actually use a function for this number
$config['full_tag_open'] = '<div id="paginate">';
$config['full_tag_close'] = '</div>';
$config['first_link'] = '« First';
$config['last_link'] = 'Last »';
$config['use_page_numbers'] = true;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
When I echo the pagination in the view it looks like it works. The urls on each link is correct and everything looks fine. The last link shows the last page url and the current page is 1. However when I click on page 2 or any other page from the pagination, it still shows page 1 as the current page even though the url is as follows
website.com/leaders/page/2
I used the $this->uri->segment(3) to grab the page number for my database queries so the page number is in the right segment. Just to double check I set the $config['uri_segment'] values to 1,2,3,4,5,6 just to make sure.
I found out the problem while writing this but I am still confused
Then I thought maybe there is something going on with the url itself as I have a route directing it to the index method in the controller. Here is what my routes file looks like
routes.php
$route['leaders/page/(:num)'] = 'leaders/index';
$route['leaders/page'] = 'leaders/index';
Then I tried setting the base_url for the pagination config to send it to the index directly like so:
$config['base_url'] = base_url . 'leaders/index';
Now it seems to be working properly. But how do I make it so that it works with the url structure I had before? I just think it looks nicer and I don't really need a method in the controller for this. Is there something conflicting in my routes.php file?
Thanks
define cur_page and define controller like :
public function index($page=''){
//...
$page = ($page!='')? $page : 0;
$config["cur_page"] = $page;
//...
}
Use this in your code, hope this will work-
if ($this->uri->segment(3) > 0) {
$offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
} else {
$offset = $this->uri->segment(3);
}
I've been working for about 6 hours to make CI pagination work as I expected, and I don't know if is the order of the config elements or just my browser joking with me.
Below is my configuration array for pagination to work properly.
As you can see this is normal code, but my problem was, when I rendered for the first time, my pagination view, all seems to be ok, but if $config['per_page'] = 10; was set to 10, when I clicked on 11 link of pagination links, link number 2 showed Cseguimiento/buscar_reportes/# and looks like current page was 2 not 11.
I was very tired and I started to change the order of $config array, and suddenly it worked. SO I preesnt it here.
$config['base_url'] = base_url().'Cseguimiento/buscar_reportes/';
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$config['next_link'] = '>';
$config['prev_link'] = '<';
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_tag_open"] = '<li>';
$config["first_tag_close"] = '</li>';
$config["last_tag_open"] = '<li>';
$config["last_tag_close"] = '</li>';
$config["next_tag_open"] = '<li>';
$config["next_tag_close"] = '</li>';
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li>";
$config["cur_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config['total_rows'] = $this->mseguimiento->filas($fecha_inicio,$fecha_fin);
$config['per_page'] = 10;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$page = $config['uri_segment'] * $config['per_page'];
$this->pagination->initialize($config);
$offset = ($this->uri->segment(3)-1)*$config['per_page'];
$output = array(
'pagination_link' => $this->pagination->create_links(),
'lista_reportes' => $this->mseguimiento->fetch_details($this->pagination->per_page, $offset,$fecha_inicio,$fecha_fin)
);
My problem is that when I press on the pagination link the URL CHANGE and the segment array also so, first I started with this URL:
site/argument/argument
and when I press the pagination link the URL turn into:
site/method/startIndex.
Is there a way to use pagination without using the URL query index ?
EDIT:
Here is my controller method where I've receive the arguments from the URL:
function index ($par1, $par2 = null, $par3 = null)
{
$data['records'] = $this->site_model->getRecords($par1, $par2, $par3);
$this->load->view('main', $data);
}
And this is the Model method where I've do the DB query:
function getRecords($par1, $par2 = null, $par3 = null )
{
if (!isset($par2) && !isset($par3)) {
$where = "lvlOne = '".$par1."'";
} elseif(isset($par2) && !isset($par3)) {
$where = "lvlOne = '".$par1."' AND lvlTwo = '".$par2."'";
} elseif(isset($par2) && isset($par3)) {
$where = "lvlOne = '".$par1."' AND lvlTwo = '".$par2."' AND lvlThree = '".$par3."'";
}
$this->db->from('mph_products');
$this->db->where($where);
$query = $this->db->get();
return $query->result();
}
How can I paginate this results? Because I've got conflicts with the pagination library URL
EDIT:
I've found a way to do what I've wanted to.
There is a config option of the Pagination Library where you put the suffix of the pagination anchor and you can pass the arguments to it.
$config['suffix'] = "$arg1/$arg2/$arg3";
:D
This library codeigniter pagination + bootstrap design
<?php
class Paginacao
{
public $CI;
function __construct() {
$this->CI = &get_instance();
$this->CI->load->library('pagination');
}
function criar($total_rows,$base_url,$per_page=15,$num_links=5)
{
$paginacao['total_rows'] = $total_rows;
$paginacao['base_url'] = site_url($base_url);
$paginacao['per_page'] = $per_page;
$paginacao['num_links'] = $num_links;
$paginacao['full_tag_open'] = '<br /><div class="pagination pagination-centered"><ul>';
$paginacao['full_tag_close'] = '</ul></div>';
$paginacao['first_link'] = 'Primeira';
$paginacao['first_tag_open'] = '<li>';
$paginacao['first_tag_close'] = '<li>';
$paginacao['last_link'] = 'Ultima';
$paginacao['last_tag_open'] = '<li>';
$paginacao['last_tag_close'] = '</li>';
$paginacao['next_link'] = 'Próximo';
$paginacao['next_tag_open'] = '<li>';
$paginacao['next_tag_close'] = '</li>';
$paginacao['prev_link'] = 'Anterior';
$paginacao['prev_tag_open'] = '<li>';
$paginacao['prev_tag_close'] = '</li>';
$paginacao['cur_tag_open'] = '<li class="active"><a href="#">';
$paginacao['cur_tag_close'] = '</a></li>';
$paginacao['num_tag_open'] = '<li>';
$paginacao['num_tag_close'] = '</li>';
$this->CI->pagination->initialize($paginacao);
$html = $this->CI->pagination->create_links();
return $html;
}
}
As I understand, you want not to see page number in url, but CI Pagination class always add number to link. For more details see Pagination class.
Also you can use jquery datatable for client or server pagination.
I have a controller with a method that looks something like this:
public function browsecategory($category_id)
{
//find any subcategories for this category
$this->load->model('category/category_model');
$this->load->model('category/product_category_model');
$records['categories'] = $this->category_model->find_all_by('parent_id', $category_id);
//add some product data too.
$records['products'] = $this->product_category_model->find_all_by('category_id', $category_id);
Template::set('records', $records);
Template::render();
}//end browsecategory
All the examples I've seen for the codeigniter pagination "stuff" is using one query.
I need to combine two data sets and serve on one view.
Any suggestions?
EDIT 1
I've tried to follow MDeSilva's suggestion below. And although the pagination object is correctly calculating the number of links to create, all items appear on all pages.
Here's the code in the model that gets the data:
public function get_categories_and_products($limit=12, $offset=0, $category_id=null)
{
print '<BR>the function got the following offeset:'.$offset;
$query = "(SELECT cat.category_id, cat.title, cat.image_thumb, cat.deleted, cat.display_weight ";
$query = $query."FROM bf_categories cat ";
$query = $query."WHERE cat.parent_id=".$category_id;
$query = $query." AND cat.category_id <>".$category_id;
$query = $query.") UNION (";
$query = $query."SELECT p.product_id, p.name, p.image_thumb, p.deleted , p.display_weight";
$query = $query." FROM bf_product p ";
$query = $query."Inner join bf_product_category cp ";
$query = $query."on p.product_id=cp.product_id ";
$query = $query."Where cp.category_id=".$category_id.")";
$this->db->limit($limit, $offset);
$catsandprods= $this->db->query($query);
return $catsandprods->result() ;
}
And here's the code in the controller:
public function browsecategory($category_id, $offset=0)
{
$this->load->library('pagination');
$total = $this->product_model->get_cats_prods_count($category_id);
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 4;
$config['total_rows'] = $total;
$config['per_page'] = 5;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
print $offset;
//Call the model function here to get the result,
$records= $this->product_model->get_categories_and_products(5,$offset,$category_id);
//add to breadcrumb trail
$this->build_bread_crumb_trail($category_id);
$breadcrumbs = $this->breadcrumbs->expand_to_hyperlinks();
Template::set('currentcategory',$category_id);
Template::set('breadcrumbs', $breadcrumbs);
Template::set('records', $records);
Template::render();
}
I've debugged and I can see that the line of code "$this->db->limit($limit, $offset);" in the model is not working. It always returns the full record set...
Can you tell me what I'm missing?
Thanks.
This is the way to generate pagination links in CI, for your requirement have a query with a join,
public function index($offset = 0) {
$language_id = 1;
$artwork_id = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$artwork_id = $this->input->post('serach_artwork_id', TRUE) ? $this->input->post('serach_artwork_id', TRUE) : null;
$data['artwork_id'] = $artwork_id;
}
$this->load->library('pagination');
$limit = 10;
$total = $this->Artwork_model->get_artwork_count($language_id, $artwork_id);
$config['base_url'] = base_url().'artwork/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
//Call the model function here to get the result,
$data['artworks'] = $this->Artwork_model->get_artworks($language_id, $limit, $offset, $artwork_id);
$this->template->write('title', 'Artwork : Manage Artwork');
$this->template->write_view('content', 'artwork/index', $data);
$this->template->render();
}
Here is an example for query with multiple joins in the model
public function get_artworks($language_id = 1, $limit = 10, $offset = 0, $arwork_id = null)
{
$this->db->select('a.id, a.price, a.is_shop, at.title,at.status,at.date_added,ats.name as artist_name');
$this->db->from('artworks a');
$this->db->join('artwork_translations at', 'a.id = at.artwork_id');
$this->db->join('artists ats', 'a.artist_id = ats.id');
$this->db->where('at.language_id', $language_id);
if(!is_null($arwork_id) && !empty($arwork_id) && $arwork_id != 'all')
{
$this->db->where('a.id =', $arwork_id);
}
$this->db->order_by('a.id DESC');
$this->db->limit($limit, $offset);
$artworks = $this->db->get();
return $artworks->result();
}
In the View
<?= $this->pagination->create_links(); ?>
The pagination class doesn't care about the how the data source is constructed, just so you hand it the data result object it wants. So you would just pass limits & offsets into your data queries, or else pull all your data and slice it up afterwards.
However, I don't really understand how you are thinking to combine these different bits of data into a single result to display - are you trying to display all categories, then paginate the products? If so, you are set up incorrectly
Simply use PHP function array_merge
<?php $beginning = 'foo'; $end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
$this->load->view('view.php', $result);
?>
and extract according to keys for array used.
It is really simple & working