Codeigniter pagination first_url base_url issue - codeigniter

My pagination is not working properly. The problem is when I'm clicking on 2nd page or any other page it does the right thing. But when I click on 1st page number or the page number on which I'm right now it redirects me to base_url like localhost/project/project. It should not happen it should go localhost/project/project/0 or localhost/project/project/1 or localhost/project/project/2 or that page number.
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$total_rows = $this->Product_model->fetch_data('','',$keyword);
//$offset = 0;
$config = array();
$config["base_url"] = base_url().'product';
$config["total_rows"] = $total_rows->num_rows();
$config["cur_page"] = $page;
$config["per_page"] = 1; //display atleast 6 per page
$config["num_link"] = 2;
$config["uri_segment"] = 4;
$config['first_url'] = base_url() . 'product/0';

Related

Codeigniter Pagination Links Error After ID

sorry if my question is duplicate, i have a problem with my codeigniter pagination. My url is: http://mysite/news/category/26/(page number), i have only 5 data and the page limit is 10, but the generated links active on page 3, i believe that is because of id/26/
This is my model :
function categpry($id){
$string_query = "SELECT * FROM news n JOIN category c ON n.category_id = c.id_category AND n.category_id = $id ORDER BY n.category_id DESC";
$query = $this->db->query($string_query);
$config['base_url'] = site_url('news/category/' . $id);
$config['total_rows'] = $query->num_rows();
$config['per_page'] = '10';
$num = $config['per_page'];
$offset = $this->uri->segment(4);
$offset = ( ! is_numeric($offset) || $offset < 1) ? 0 : $offset;
if(empty($offset)){
$offset = 0;
}
$this->pagination->initialize($config);
$news = $this->db->query($string_query." limit $offset,$num");
return $nes;
}
the generated links is 1 - 2 - 3, its should be no link because i only have 5 data
please help me on this, thank you
finally I fixed it with change the script with this :
function category($id){
$string_query = "SELECT * FROM news n JOIN category c ON n.category_id = c.id_category AND n.category_id = $id ORDER BY n.category_id DESC";
$query = $this->db->query($string_query);
$config['base_url'] = site_url('news/category/' . $id);
$config['total_rows'] = $query->num_rows();
$limit = $config["per_page"] = 10;
$config["uri_segment"] = 4;
$config["use_page_numbers"] = TRUE;
$this->pagination->initialize($config);
if($this->uri->segment(4))
$page = ($this->uri->segment(4)-1)*$limit;
else
$page = 1;
$news= $this->db->query($string_query." limit $page,$limit");
return $news;
}
Thank you

how can i hide controller name from the url in codeigniter

i have both admin panel and also one more front panel.....i have different controller for both and in my routes.php i made changes but because of that some links are not working
$route['default_controller'] = "site";
$route['404_override'] = '';
$route['contact'] = 'site/contact';
$route['events'] = 'site/events';
$route['terms'] = 'site/terms';
$route['privacy'] = 'site/privacy';
$route['newevent'] = 'site/add_event';
$route['(:any)'] = "site/$1";
//$route['(:any)'] = "backos/$1";
$route['listevents'] = 'backos/events';
$route['listwhatsapp'] = 'backos/whatsapp';
$route['add_artist'] = 'backos/add_artist';
$route['add_club'] = 'backos/add_club';
$route['index'] = 'backos/index';
$route['check'] = 'backos/check';
$route['add_event'] = 'backos/add_event';
$route['add_city'] = 'backos/add_city';
$route['add_locality'] = 'backos/add_locality';
//$route['(:any)'] = "backos/$1";
this is my routes.php file.
try this...
$route['default_controller'] = "site";
$route['404_override'] = '';
$route['contact'] = 'site/contact';
$route['events'] = 'site/events';
$route['terms'] = 'site/terms';
$route['privacy'] = 'site/privacy';
$route['newevent'] = 'site/add_event';
$route['listevents'] = 'backos/events';
$route['listwhatsapp'] = 'backos/whatsapp';
$route['add_artist'] = 'backos/add_artist';
$route['add_club'] = 'backos/add_club';
$route['index'] = 'backos/index';
$route['check'] = 'backos/check';
$route['add_event'] = 'backos/add_event';
$route['add_city'] = 'backos/add_city';
$route['add_locality'] = 'backos/add_locality';
// this is a catch all if you don't put it as
// the last one everything else below it wont work!
$route['(:any)'] = "site/$1";

Codeigniter - make email unreadable by bots

Is there a method in codeigniter to make email links unreadable by bots? If not does anyone recommend a decent simple solution?
Oops..
Codeigniter has a function safe_mailto() in the URL helper
I was told this will work for google search appliance
<!--googleoff: index-->
my email
<!--googleon: index-->
Source: http://perishablepress.com/tell-google-to-not-index-certain-parts-of-your-page/
Please let me know if is incorrect.
There probably isn't any perfect solution here, especially if you don't want to inconvenience your users.
One simple method (which could be combined with the google comments mentioned by ajreal), would be to use entity encoding to obfuscate their source. This would block simple bots.
See here for some details: http://www.wbwip.com/wbw/emailencoder.html
CodeIgniter has a safe_mailto() method in the URL Helper class that obfuscates e-mail addresses in the HTML source code, without affecting the end user.
https://codeigniter.com/userguide3/helpers/url_helper.html#safe_mailto
Example Template Code:
<p>My e-mail address is <?php $this->load->helper('url'); echo safe_mailto('abc#gmail.com'); ?>.</p>
Example HTML Output:
<p>My e-mail address is <script type="text/javascript">
//<![CDATA[
var l=new Array();
l[0] = '>';
l[1] = 'a';
l[2] = '/';
l[3] = '<';
l[4] = '|109';
l[5] = '|111';
l[6] = '|99';
l[7] = '|46';
l[8] = '|108';
l[9] = '|105';
l[10] = '|97';
l[11] = '|109';
l[12] = '|103';
l[13] = '|64';
l[14] = '|99';
l[15] = '|98';
l[16] = '|97';
l[17] = '>';
l[18] = '"';
l[19] = '|109';
l[20] = '|111';
l[21] = '|99';
l[22] = '|46';
l[23] = '|108';
l[24] = '|105';
l[25] = '|97';
l[26] = '|109';
l[27] = '|103';
l[28] = '|64';
l[29] = '|99';
l[30] = '|98';
l[31] = '|97';
l[32] = ':';
l[33] = 'o';
l[34] = 't';
l[35] = 'l';
l[36] = 'i';
l[37] = 'a';
l[38] = 'm';
l[39] = '"';
l[40] = '=';
l[41] = 'f';
l[42] = 'e';
l[43] = 'r';
l[44] = 'h';
l[45] = ' ';
l[46] = 'a';
l[47] = '<';
for (var i = l.length-1; i >= 0; i=i-1) {
if (l[i].substring(0, 1) === '|') document.write("&#"+unescape(l[i].substring(1))+";");
else document.write(unescape(l[i]));
}
//]]>
</script>.</p>

customized pagination in codeigniter

I have codeigniter applicatin which have pagination code that work like a charm. Pagination allow the visitor to select how much records he would like to see per page. Now, I would like to add the following:
displaying X to Y from total of Z records.
Now, Z is easy...i have that solved. Can anyone tell me how could I achive this?
Here is the function that i have:
public function displayAllUsers()
{
if ($this->uri->segment(3) !="") {
$limit = $this->uri->segment(3);
} else {
$limit = 10;
}
$offset = 4;
$offset = $this->uri->segment(4);
$this->db->limit($limit, $offset);
$data['users'] = $this->backOfficeUsersModel->get();
$totalresults = $this->db->get('back_office_users')->num_rows();
$this->session->set_userdata('totalresults', $totalresults);
//initializing & configuring paging
$this->load->library('pagination');
$config['base_url'] = site_url('/backOfficeUsers/displayAllUsers/'.$limit.'/');
$config['total_rows'] = $totalresults;
$config['per_page'] = $limit;
$config['uri_segment'] = 4;
$config['full_tag_open'] = '<div class="dataTables_paginate paging_bootstrap pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['cur_tag_open'] = '<li><a href=# style="color:#ffffff; background-color:#258BB5;">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
//....irelevant content.....
} // end of function displayAllUsers
Any help will be deeply appreciated.
It shouldn't be that hard. In fact, it's kinda obvious how you can get the value for X and Y if you understand pagination.
First of all, the difference between X and Y will of course be equal to $limit - 1. The value of X should be equal to the variable, $offset, plus 1 (Offset generally starts from 0 not 1). And from there, I'm sure that you understand that Y is equal to $offset + $limit.
$X = $offset + 1;
$Y = $offset + $limit;
In case, you're at the last page, you'll want to make sure you have the right value for Y. You'll need to check the total rows you grab from the database, if it's less than the limit per page, then change it to something like this
if (fetched_rows < $limit)
{
$Y -= ($limit - fetched_rows);
}

codeigniter pagination not working as expected

I tried a lot but cant seem to figure out this, any help appreciated. My articles have votes, and I have a page which is supposed to show most voted articles. Am using codeigniter btw.
The controller:
function most_voted()
{
$per_page = 3;
$cur_page = $this->uri->segment(4);
/*
if($cur_page == "") $cur_page = 1;
else $cur_page = (integer)$cur_page;
*/
$offset = ($cur_page - 1) * $per_page;
if($offset < 0) $offset = 0;
$this->load->model('article_model');
$result_rows = $this->article_model->GetMostVoted($per_page,$cur_page);
$total_rows = sizeof($result_rows) + 10;
echo "total rows is : ".$total_rows.'<br>';
echo "cur page is : $cur_page <br>";
//$this->load->library('pagination');
$config['base_url'] = base_url().'articles/most_voted/page/';
$config['uri_segment'] = 4;
$config['num_links'] = 3;
$config['first_link'] = '<<First';
$config['last_link'] = 'Last>>';
$config['prev_link'] = '< Previous';
$config['next_link'] = 'Next >';
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['articles'] = $result_rows;
$data['view_file_name'] = 'articles/all_articles_view';
$this->load->view('includes/template',$data);
//echo $this->db->last_query();
}
The model :
function GetMostVoted($limit,$offset)
{
$this->db->order_by('votes','desc');
$q=$this->db->get('cgh_articles',$limit,$offset);
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
The problem :
Issue is that though I get pagination like
< Previous 1 2 3 4 5 Next >
but clicking on 2 goes to url : page/3
clicking on 3 goes to url : page/6
clicking on 4 goes to url : page/9
and so on.
I want a click on 2 to go to page/2, 3 to page/3, and so on. Any suggestions as to whats going wrong?
If you need any more info, please let me know, thanks.
The way the pagination class works is that it returns the item at which the page should start.
For example, url : hostanme/controller/page/9 means that the page should be rendered starting with the 9th most voted article. See the example in the codeigniter documentation.
Also, you have $total_rows = sizeof($result_rows) + 10;. Why is that ?
There is an excellent tutorial for this on nettuts.

Resources