CodeIgniter Pagination - first page’s link doesn’t work, why? - php

I use pagination library on my site, my config is here:
$data['url_keyword'] = url_title($keyword, '_');
$config['base_url'] = base_url().'image/'.$data['url_keyword'].'/';
$config['per_page'] = 1;
$config['uri_segment']=3;
my urls are: mysite/image/keyword/1 (page2), mysite/image/keyword/2 (page3), mysite/image/keyword/3 (page4), mysite/image/keyword/4 (page5)
When I’m on page 2 (mysite/image/keyword/1) and click previous - it gets me into mysite/image/keyword/ - which doesnt work, but mysite/image/keyword/0 WORKS. Library just doesn’t add zero at the end of url.
how to fix it?

Try this:
if (!$this->uri->segment(3))
$page = 0;
//Your other logic
By default if $this->uri->segment(x) is empty it will return false.
Without seeing your controller or view I can't be more specific on where to place this or what you are naming your variable. This should work in the instances in which you call: yoursite.com/image/keyword

Related

Problems using Joomlas JPagination class. Error 404 page not found

I've created a module for Joomla that fetches some data from a database and creates a table with it. I added JPagination to my module and I got the footer buttons to show and everything.
public function addPagination($params)
{
$count = $params->get("count");
$multiPage = $params->get("multiple_pages");
//Add controls for changing pages
if($multiPage)
{
jimport('joomla.html.pagination');
$limitStart = 0;
$pagination = new JPagination(count($this->vacanciesRows) , $limitStart, $count);
echo $pagination->getListFooter();
}
}
but when I click some of the pages (all except the first one) I'm getting error 404. I'm sure I've missed something but I have very little to none experience with Joomla. I'll include pastebins with my helper.php and my mod_xxx_xxx.php
A module can't have a pagination. It has no own URL. Only components have that. If you check the links your module creates, you'll notice that they are invalid. You can try to do Ajax magic but then you need a component providing the data.
In Joomla only components can react to incoming URLs directly.

Use hyphen(-) instead of slash(/) or underscore( _ ) in Routes

I'm Using Codeigniter 3.x , Using routes.php I want to create dynamic routes, for example I have a class name Class1.
I want output url
mysite.com/Class1-Student-Search
But using hyphen(-) is not working
If I put a slash(/), it works,
$route['(:any)/Student-Search'] = "search";
it returns
mysite.com/Class1/Student-Search
and using underscore (_) also work.
$route['(:any)_Student-Search'] = "search";
returns
mysite.com/Class1_Student-Search
But I want to use hyphen(-), if I put it, it will go to 404 error page, I used these four solutions but not work for me.
$route['(:any)-Student-Search'] = "search";
$route['([a-zA-Z]+)-Student-Search'] = "search";
$route['([a-zA-Z-0-9]+)-Student-Search'] = "search";
$route['(.*)-Student-Search'] = "search";
And if i hardcode the value in route
$route['Class1-Student-Search'] = "search";
Then it also working
You trying to create a dynamic routes which is not possible in codeigniter if you see the following flow chart of codeigniter you understand what i mean.
also you can see this chart in codeigniter official website
when you try to redirect or call some url it's work like this
Every request first goes to route there for you can't make it dynamic
Here is my solution, it is working for me, do like this.
$route['(:any)-Student-Search'] = "search";
then in your link button, hopefully in your view, href the link like this.
href="/<?php echo $row->classname; ?>-Student-Search"
the point is that not only you have to make your routes, also suffix it in your href link also the same way.

CodeIgniter: Is possible to have current page link by pagination library?

I have created pagination item by CodeIgniter pagination library with following code:
echo $this->pagination->create_links()
Everything is working well.
Now i want to load data by ajax and i have done already ajax part. But problem is to make clicked item as a current item and to arrange link for current item as it is no longer current.
Suppose i have a pagination as following:
[1] 2 [3] [4] [5] [6] [>] [Last >]
Now 2 is current item and 4 is clicked item.
I have checked CodeIgniter Pagination library, but it doesn't have any option to enable or disable current page link. Is it possible to have current page link without modifying the library?
Thanks in advance.
I take it all back. The Pagination library will need to be changed.
https://github.com/EllisLab/CodeIgniter/blob/develop/system/libraries/Pagination.php
Line 560
$output .= $this->cur_tag_open.$loop.$this->cur_tag_close
Will need to be replaced with
$append = $this->prefix.$i.$this->suffix;
$output .= $this->num_tag_open.'<a href="'.$this->base_url.$append.'"'.$attributes.$this->_attr_rel('start').'>'.$loop.'</a>'.$this->num_tag_close;
That should do it.
You don't need to modify anything on server side. just use plain old vanila jquery.
Here's how to do it.
Let's say i wrapped the links in a <div id="pagination"></div>
Now, to call ajax, use jQuery(body).on('click','#pagination a', function(e){....}).
Here in this code, jQuery(this) will refer to the link that was clicked.
Next, after successful call, remove the active (or any other class that is given ) from all the links. like jQuery('#pagination a').removeClass('active').
Next, if you still remember, jQuery(this) still refered to the current clicked item. (better save it as var current = jQuery(this) at the start of the clicked even so that the reference does not change. Now, just add the class. jQuery(current).addClass('active')
You are done! No need to do anything fancy anything in server side. Sometimes client side is enough.
P.S. i forgot how CI renders the pagination links. if you give the links, i can write the actual code.
var_dump($this->pagination); everything is inside
then if you check Pagination.php library you have these params available too:
var $cur_tag_open = '';
var $cur_tag_close = '';
so try calling them when initializing pagination
$pagination['cur_tag_open'] = '<span class="current-link">';
$pagination['cur_tag_close'] = '</span>';
$this->pagination->initialize($pagination); //now current link should be wrapped into the <span class="current-link"></span>
I don't believe you need to change anything in the Pagination library. You just need to use the uri_segment() function.
The Pagination library takes an argument as follows:
$config['uri_segment'] = 5;
The default value is 3. [Use a value that will work with your URI structure].
This variable is used along with the uri_segment() function to determine the current page, in the latest version of CodeIgniter 2.1.4 you will find it starting on line 142 in the Pagination.php library.
The appropriate uri_segment for the page identifier could be calculated as follows, if using a URI like this one: http://example.com/controller/function/page/20 then you can get the current page as follows,
$page = $this->uri_segment(4);
Hope that helps.
You really dont need to change anything.
Once you have your automatic page set
$page = 2 // Page should be set automatically
Now try this
$config["cur_page"] = $page;

CodeIgniter Pagination : Does not create first page's link

my pagination problem still continue ;
I just making pagination in simple stuff :
$config['base_url'] = site_url('admin/index/page/');
$this->load->database();
$config['total_rows'] = $this->db->count_all('sms');
$config['per_page'] = 20;
$offset = $this->uri->segment(4, 0);
$this->pagination->initialize($config)
And my view page has this command for view pagination :
<?php echo $this->pagination->create_links(); ?>
And after more than 20 rows , pagination starts to paging the list, but html output shows like this :
1 2 3
As in view , First Page Number 1 does not have link either jumping next page , there is no link on page number 1. Just its on strong .
My second problem is : I have just 30 record but CI pagination creates 3rd page which is coming with empty rows !
I am not sure why some Class ( specially pagination makes so much trouble to users ? ) If i need to pay something ( maybe hidden licence? ) for get away from trouble instead of using simple pagination class without loosing so much time for searching issue on internet , I am ready for it !
I was very mad because of this pagination problem and I was studying the source code of the pagination library and I saw this -- var $uri_segment = 3; The default of the pagination library uses the 3rd uri segment, in your case and in my case we wanted to use the 4th uri segment. To suit our needs change this code:
$config['base_url'] = site_url('admin/index/page/');
$this->load->database();
$config['total_rows'] = $this->db->count_all('sms');
$config['per_page'] = 20;
$offset = $this->uri->segment(4, 0);
$this->pagination->initialize($config)
TO
$config['base_url'] = site_url('admin/index/page/');
$this->load->database();
$config['total_rows'] = $this->db->count_all('sms');
$config['per_page'] = 20;
$offset = $this->uri->segment(4, 0);
$config['uri_segment'] = 4; // add this line to override the default
$this->pagination->initialize($config)
Kindly post back here if this will solve your problem or if the problem still exists so I can help :)
Nwei this is not in the pagination class documentation. I hope this will be added to the docs because I'm seeing many developers having a hard time with the pagination class.
If you are familiar with how pagination works, then you should be aware that pagination does not add a link tag to the current page. So I'm not sure what you're asking in regards to "pagination class does not create 1 page link".
Also, I don't see where you're offset is being used within the pagination functionality. It seems like you set it but you don't use it. This can cause an incorrect pagination result which I assume is why you get more pagination links than you expect.
$offset = $this->uri->segment(4, 0);
$this->pagination->initialize($config);
One way to change the markup for the "current page" link is 'Customizing the "Current Page" Link'
$config['cur_tag_open'] = '<a href="#">';
The opening tag for the "current" link.
$config['cur_tag_close'] = '</a>';
The closing tag for the "current" link.
The other option is to dive into the pagination class and remove the functionality it has for not adding a link to the current page.
Really, if you're on the current page, it doesn't add any benefit to link to the same page you're already on which I'm assuming is the reason the current page doesn't have a link. If you went to page 2 then the 2 would be disabled. Hope that makes some sense.
No, there no need to pay anything ...CI pagination works perfectly ..you see this library http://codeigniter.com/user_guide/libraries/pagination.html
make sure your $config['total_rows'] = $this->db->count_all('sms'); this code is working correct and providing the correct number of rows and also i would like to suggest use ..$config['uri_segment'] = 3; or $config['uri_segment'] = 4; depend on your urirather than using $offset = $this->uri->segment(4, 0);
Hofefully these few change will work for you....
I was also having the same issue in pagination when using segment number 4. I solved it by using segment number 3. Thats also my problem because I really need to use segment number 4 But Oh well it has bug. I hope this can be solved ASAP. Or if u really need to fix this issue now u can try to fork the source code of pagination class.

Replace string in Array in array with truncate string

I'm using CodeIgniter for my website. I'm also using the tumblr API on my site to show posted news.
Because showing the entire text is a bit too much, I want to truncate the body copy to 150 characters, I do this by using the character_limiter function of CI.
The code is as followed in my 'home' controller:
public function index() {
//Title for home page
$data['title'] = "Home - Welcome";
// Obtain an array of posts from the specified blog
// See the config file for a list of settings available
$tumblr_posts = $this->tumblr->read_posts();
foreach($tumblr_posts as $tumblr_post) {
$tumblr_post['body'] = character_limiter($tumblr_post['body'], 150);
}
// Output the posts
$data['tumblr_posts'] = $tumblr_posts;
// Load the template from the views directory
$this->layout->view('home', $data);
}
The problem is, that $tumblr_post['body'] isn't shortened when I echo it on my view page. Doing it like above works in Asp.net (C#) but it doesn't seem to work in php, anyone know why and how to solve it or is there an other way?
Your problem is with the foreach loop. You need to add a & before $tumblr_post to pass it by reference. This makes sure you are actually editing the values in the array. Without the &, you're just editing a local variable and not the array.
Try it like this (notice the &):
foreach($tumblr_posts as &$tumblr_post) {
$tumblr_post['body'] = character_limiter($tumblr_post['body'], 150);
}

Categories