How to echo page number with codeigniter pagination library? - codeigniter

I am using the codeigniter pagination library to page my search results. I am using a limit and an offset to generate the results.
I would like to know how, apart from in the pagination links, echo the number of the page the user is currently viewing.
For example;
You are currently viewing page X out of Y results.
How is this possible??

This also gives the current page no
$currentPage = floor(($this->uri->segment(n)/$config['per_page']) + 1);
where n is the segment
echo "Current Page:".$currentPage;

You could extend the Pagination class (system/libraries/Pagination.php).
Create a file called MY_Pagination.php and put it in application/libraries/
Add this code to your extend pagination class;
class MY_Pagination extends CI_Pagination {
public function __construct() {
parent::__construct();
}
public function current_place() {
return 'You are currently viewing page '.$this->cur_page.' out of '.ceil(($this->total_rows/$this->per_page)). 'results';
}
}
Don't forget to make sure the class extension prefix is set correctly to MY_ in the config.php (about line 109)
Then you can access it in your controller like;
$current = $this->pagination->current_place();
Which you can pass into your view.

Depending on how you are setting up the offset. Ours is setup with an offset of the perpage. So our default is 10 per page. So for page one the offset is 0. For page two the offset is 10 and so on. So for me to get the current page I would take the offset divided by the current limit plus 1. So ($cur_page/$per_page)+1 would get me the current page.

Related

increment function increments by 2 not with 1

Every time user clicks post then that post view should be incremented by 1.
But instead of 1 it is incrementing by 2.
There are many some pages where this post can be clicked.
I have tried using increment function
$view = PostAd::where('id',$id)->first();
$view->increment('viewcount',1);
full code
$view = PostAd::find($id);
$view->increment('viewcount',1);
$data['ads'] = PostAd::find($id);
$data['post']= PostAd::with('postimage')->where('id',$id)->get();
$data['postimage'] = PostAd::with('pimage')->where('id',$id)->get();
$data['details']= PostAd::with('category')->where('id',$id)->first();
$data['comments'] = Comment::where('post_id',$id)->get();
$data['favourite'] = Favourite::where('post_id',$id)->first();
$data['identify'] = PostAd::with(['category','category.children'])->get();
Answer is simple. Use this...
$view->increment('viewcount');
Increment by a custom count (COUNT)
$view = PostAd::where('id',$id)->first();
$view->increment('viewcount',COUNT);
Ex: Increment by 5
$view->increment('viewcount',5);
Read more here
You can use default 1 increment.
$query->increment('viewcount');
If you need custom increment, use
$query->increment('viewcount',increment_value);
OR
PostAd::where('id', $id)
->update('viewcount' => DB::raw('viewcount + 1'));
If you use pagination with json, for example, after it loads the page request, a new request will be created to add ?page=1.
Try to keep the pagination code only in the view that requires pagination. Create a .js file to be listed only on pages that require this functionality. Not globally.

How to convert CodeIgniter urls into user friendly ones?

I've explored lot of questions and articles regarding this but I can't find how to get this done.
I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)
Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)
My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.
Thanks for reading.
Edit:
Route.php
$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
This is how I'm building the anchor links (view_file->index.php, called from Index method):
<?php
foreach model(in the table)
echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
end foreach
?>
I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.
$route['(:any)'] = function ($1)
{
$result = 'urlController/urlMethod/'.$1;
return $result;
};
I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?
You could achieve it with CodeIgniter URI Routing. Considering
localhost/sitename/galaxy-tab-s3
maps to
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.
My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like
$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';
Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using
header('Location: http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');
NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.
Please use below routing code, to achieve it.
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
$route['(:any)'] = 'specs/display/$1/$1/$1';
Let me know if you need any help.

Google Sitemap issue Contao 4.4

I am using contao 4.4 instance.I have a problem in google sitemap generation.
I have a newsletter page (page type = regular ) . In that page I have some newsletter articles (with teaser) . When I generate the sitemap, the url of these articles generated twice. When I checked the core I found a class which creates the page array for generating sitemap
vendor/contao/core-bundle/src/Resources/contao/classes/Backend.php
line no 662 - 680 .
Which append 'articles/' to the articles with teaser. So the sitemap generates url
with articles/
List item
without articles/
The first one is the correct url.Second Url generate 404. How I fix the issue ?
My siteconfiguration is as follows
->created a regular page with hidden in navigation and created articles with configuration show teaser
->created another page and created elements as 'teaser articles' and select articles from the above page
So your site structure is like this?
Page 1 with multiple articles
Page 2 with one article containing an „article teaser“ element, this page should be excluded from the sitemap
Is that correct? If yes this might be the solution:
In your „site structur“ you can EDIT Page 2: Scroll down to „Expert settings“, there is an option „Show in sitemap“ and you can select „Never show“.
I resolved the issue.
The url's were regenerated from the newsletter-bundle. In core bundle, the url of the article with teaser is generated. The newsletter-bundle is also contain hook for creating searchable page array. The hook regenerates the url. I wrote a function in vendor/contao/newsletter-bundle/src/Resources/contao/classes/Newsletter.php to check whether the array contain any duplicate url.
Modified function getSearchablePages() ( line 889-897
)
if($this->checkValidUrl($arrPages, $objItem, $strUrl)){
$arrPages[] = sprintf($strUrl, ($objItem->alias ?: $objItem->id));
}
Defining new function for finding duplicate
public function checkValidUrl($arrPages, $objItem, $strUrl)
{
$alias = $objItem->alias ?: $objItem->id;
$urlExplode = explode('%s', $strUrl);
$articleUrl = $urlExplode[0] . 'articles';
foreach ($arrPages as $arrPage) {
$validarticleUrl = $articleUrl . '/' . $alias;
if (strcasecmp($arrPage,$validarticleUrl) == 0) {
return false;
}
}
return true;
}

Removing part of a Laravel URL if certain conditions apply

I have a number of routes on my website that use a custom pagination solution, like /categories/{category}/page/{page?}. I currently have each controller set like the following:
public function index($category, $page = null) {
if(!isset($page) || !is_numeric($page) || $page < 1) {
return redirect()->route('parent', ['category' => $category]);
}
}
This has the desired effect of checking if the provided page number exists, is, indeed, a number, and if it is greater than zero. In the event that any of those conditions are false, it redirects to the parent route, /categories/{category}.
The problem is, for each of these solutions, they all require me to write in the name of the parent route to do the redirect. If I want to just cut off the /page/ and onwards, but keep any get variables and post variables, how would I do that?
If you are trying to redirect back to the previous page with the inputs, use the following code.
return redirect()->back()->withInput();
In the above case, it will work only if you are coming to the url /categories/{category}/page/{page?} from /categories/{category}

Changing the URL/URI of pagination in Codeigniter

I'm using the pagination class in Codeigniter to page out some info from my database.
It's all working perfectly, however, i'm trying to tidy up the URLs that people use to access the pages.
Currently, i'm pulling out the offset from the URL and have set it to 20 results per page:
http://www.mysite.com/page/20
So if you go to (in this instance) page two, it starts from db entry 20.
The trouble is that firstly, since i've set it to 20 results per page, it means I get odd URLs like http://www.mysite.com/page/60 which would be page 4.
Secondly it's a double figure which i'd like to tidy up.
Basically want I want to do is set the URLs to look like:
http://www.mysite.com/page/2
http://www.mysite.com/page/3
http://www.mysite.com/page/4
etc...
I set the use page numbers to TRUE, but the trouble is it then sets the offset to 2, 3, 4 etc when it should be 20, 40, 60.
Any ideas? I was thinking of some kind of calculation in the model to work out how many entries it should offset based on the individual page number from the URL (when use page numbers is set to TRUE). I can't seem to get it right though...
In codeigniter number in url means: row per page
So if you need to change per page number with page number change your code like this:
Controller
class news extends CI_Controller{
function index($page = 'page', $page_number = 0){
$row_per_page = 20; // define page per page
// for example if page number == 4 then offset 80
$offset = $page_number * $row_per_page
// we need to request data from database
// we need this query "SELECT * FROM `mytable` limit 80,20"
// offset = 80 AND per page = 20
$this->db->get('mytable', $offser, $row_per_page);
$this->load->library('pagination');
// you can change this url by URI Routing and other solutions.
/*
change application/config/routes.php to nice url
$route['news/page/(:num)'] = "news/index/page/$1";
*/
$config['base_url'] = 'http://example.com/index.php/news/index/page/';
// Change $count_of_rows with your query num_rows you can use SQL_CALC_FOUND_ROWS
$config['total_rows'] = $count_of_wors;
$config['per_page'] = $row_per_page;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
This is very simple. Enjoy CI

Resources