Routing and pagination CI3 - steno - 06-04-2020
I have a problem with routing in CI.
I have a controller with parameters that supports categories and subcategories
category$1/subcategory$2
I came up with such entries, but they do not quite work.
Code: $route['(:any)'] = 'ads/cat/$1';
$route['(:any)/(:any)'] = 'ads/cat/$1/$2';
$route['(:any)/(:any)/(:num)'] = 'ads/cat/$1/$2/$3';
Everything works with pagination in subcategories category/subcategory/2, category/subcategory/3, etc.
But in category it's going like category/2, category/2/3, category/2/4, category/2/5 etc.
Code: public function cat( $cat_alias = null, $subcat_alias = null)
{
$this->load->library('pagination');
$limit = 20 ;
if (!empty($subcat_alias) && !ctype_digit((string)$subcat_alias) )
{
$start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
}
else
{
$start = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
}
echo $start;
echo '<br>';
if (isset($_GET['miasto'])) {
$city = $_GET['miasto'];
}
$where = array( 'catalogue' => $cat_alias );
$ad_category = $this->Site_model->get_single( 'categories' , $where );
$children = $this->Site_model->get_childrens($ad_category->id);
foreach ($children as $child)
{
$childs[] = $child->id;
}
if ( !empty( $ad_category ) )
{
if (!empty($subcat_alias) && !ctype_digit((string)$subcat_alias))
{
$where = array( 'catalogue' => $subcat_alias );
$ad_category2 = $this->Site_model->get_single( 'categories' , $where );
$title = $ad_category2->title;
$newtitle = str_replace('{miasto}', (!empty($city)) ? $city : 'Warszawa', $title);
$description = $ad_category2->description;
$newdescription = str_replace('{miasto}', (!empty($city)) ? $city : 'Warszawa', $title);
$data['meta']['title'] = $newtitle;
$data['meta']['desc'] = $newdescription;
$where = array('category_id' => $ad_category2->id);
}
else
{
$where = array( 'category_id' => $childs );
$title = $ad_category->title;
$newtitle = str_replace('{miasto}', (!empty($city)) ? $city : 'Warszawa', $title);
$description = $ad_category->description;
$newdescription = str_replace('{miasto}', (!empty($city)) ? $city : 'Warszawa', $title);
$data['meta']['title'] = $newtitle;
$data['meta']['desc'] = $newdescription;
}
$ads = $this->Site_model->get( 'ads' , $where, $limit, $start );
$data['ads'] = $ads;
$data['counter'] = $this->Site_model->get_count( 'ads' , $where);
if ( !empty( $ads ) )
{
// foreach ( $ads as $ad )
// {
// $ad_ids[] = $ad->id;
// }
// $where = array( 'id' => $ad_ids );
if (!empty($city))
{
echo $city;
$wher = array( 'slug' => $city );
$ad_city = $this->Site_model->get_single( 'cities' , $wher );
$where_filter['city_id'] = $ad_city->id;
$data['ads'] = $this->Site_model->filter('ads', $where, $where_filter, $limit, $start);
}
else
{
$data['ads'] = $this->Site_model->get( 'ads' , $where, $limit, $start, $sortby, $asdes );
}
if (!empty($subcat_alias)) {
$config['base_url'] = base_url() . $cat_alias.'/'.$subcat_alias.'/';
$config['uri_segment'] = 3;
}
else
{
$config['base_url'] = base_url() .$cat_alias.'/';
$config['uri_segment'] = 2;
}
$config['total_rows'] = $data['counter'];
$config['use_page_numbers'] = TRUE;
// Określamy liczbę wpisów na stronie
$config['per_page'] = 20;
$config['full_tag_open'] = ' <div class="wl-paginator"><nav aria-label="Page navigation example"><ul class="pagination d-flex align-items-center justify-content-center">';
$config['full_tag_close'] = '</ul></nav></div>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li class="page-item">';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '<i class="fa fa-chevron-left" aria-hidden="true"></i>';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '<i class="fa fa-chevron-right" aria-hidden="true"></i>';
$config['next_tag_open'] = '<li class="page-item page-next">';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item">';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a class="page-link" href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$config['attributes'] = array('class' => 'page-link');
// Inicjalizujemy bibliotekę paginacji z powyższymi ustawieniami
$this->pagination->initialize($config);
// Przypisujemy do zmiennej numery dostępnych stron
$data['pagination'] = $this->pagination->create_links();
$data['categories'] = $this->Site_model->get_categories();
$data['current'] = $this->uri->segment(1);
// $data['meta']['title'] = $ad_category->title;
// $data['meta']['desc'] = $ad_category->description;
$this->load->view( 'cat' , $data );
$currentURL = current_url(); //http://myhost/main
echo '<br>';
// echo $current;
echo '<br>';
echo $this->uri->segment(2);
echo '<br>';
echo $this->uri->segment(3);
echo '<br>';
$params = $_SERVER['QUERY_STRING']; //my_id=1,3
echo '<br>';
$fullURL = $currentURL . '?' . $params;
echo $fullURL;
echo '<br>';
}
else
{
$this->session->set_flashdata( 'alert' , 'Aktualnie brak ogłoszeń w tej kategorii.' );
redirect( '/' );
}
}
else
{
$this->session->set_flashdata( 'alert' , 'Niepoprawna nazwa kategorii.' );
redirect( '/' );
}
}
|