codeigniter pagination error in id passing - php

$category_id=$_GET['category_id'];
$data['adsd']=$this->mymodel->select_ads_by_category($category_id);
$config["base_url"]=site_url('main/category_ads_display?category_id='.$category_id.'');
$config["total_rows"]=count($data['adsd']);
$limit=$config["per_page"]=1;
$config["uri_segment"]=3;
$config["use_page_numbers"]=TRUE;
$this->pagination->initialize($config);
if($this->uri->segment(3))
$page=($this->uri->segment(3)-1)*$limit;
else
$page=0;
Hello friends i have pasted my code above,,everything is working fine,but when i click on links of pagination the url is like this =localhost/blabla/main/category_ads_display?category_id=5/2(not working)
but i need the url to be as = localhost/blabla/main/category_ads_display/2?category_id=5(working)
please help me on this

Try this way
$category_id=$this->uri->segment(3);
$data['adsd']=$this->mymodel->select_ads_by_category($category_id);
config["base_url"]=site_url('main/category_ads_display/'.$category_id);
$config["total_rows"]=count($data['adsd']);
$limit=$config["per_page"]=1;
$config["uri_segment"]=4;
$config["use_page_numbers"]=TRUE;
$this->pagination->initialize($config);
if($this->uri->segment(4))
$page=($this->uri->segment(3)-1)*$limit;
else
$page=0;
Now your new link will be like this localhost/blabla/main/category_ads_display/5/2 for page 2 and 5 will be your category id

Related

Codeigniter Pagination showing that I am on the last page

So here is what I had before. If I'd go to ciblog/categories/posts/5/, it would take me to a page showing all posts with a category with the id of 5.
And here is what I want to do. I want to go to ciblog/categories/posts/5 and it would show be 5 posts lets say. Then when I go to ciblog/categories/posts/5/3 it would offset by 3 so now I have the next 3 posts.
Currently I have 4 posts, just for testing. If I directly through my url go to ciblog/categories/posts/5 it shows 3 posts, and if I go to ciblog/categories/posts/5/3 it shows my one other post, so the amount of posts I am getting is correct based on the url.
BUT at the bottom it always shows that I am on page 2. When I use F12 and look at the elements both my pagination links show ciblog/categories/posts/5
So its not adding on the number at the end
Here is what I have for the pagination for this page. I used print_r and everything in $config is correct.
public function posts($id, $offset=0){
$category = $this->category_model->get_category($id);
if(empty($category))
{
show_404();
}
$config['base_url'] = base_url().'categories/posts/'.$id;
$config['total_rows'] = $this->post_model->get_posts_by_category_count($id);
$config['per_page'] = 3;
$config['uri_segment'] = 3;
$config['attributes'] = array('class' => 'pagination-link');
$this->pagination->initialize($config);
$data['title'] = $category->name;
$data['posts'] = $this->post_model->get_posts_by_category($id,$config['per_page'],$offset);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
Routes:
$route['categories/posts/(:num)/(:num)'] = 'categories/posts/$1/$2';
Reference:
I have been following a tutorial here: https://www.youtube.com/watch?v=WoQTjJepDWM&index=8&list=PLillGF-RfqbaP_71rOyChhjeK1swokUIS
Code for this tutorial is here: https://github.com/bradtraversy/ciblog
I am now adding on to this. The only changes I have made are shown above
$config['base_url'] = base_url().'categories/posts/'. $id . '/' . $offset;
$config['uri_segment'] = 4;
Because your pagination depends on offset, not id of category, then you should let it find for the offset with uri_segment 4, and your base_url must send an offset variable to find the offset.

Issue with Codeigniter Pagination And Search

I am trying to give facility of pagination and search keyword in one report with Code-igniter Framework.
Below is the Controller code for that Purpose:
$queryString="?start_date=".$values['start_date']."&end_date=".$values['start_date']."&keyword=".$values['keyword']."";
if($this->uri->segment(3)){
$offset=$this->uri->segment(3);
$config['base_url'] = base_url()."reports/mysearch_search/".$offset.$queryString;
}else{
$config['base_url'] = base_url()."reports/mysearch_search/".$queryString;
}
$query = $this->db->query("SELECT * FROM (`dlrReport`) WHERE LEFT(`res_submit_date`,10) >= '".$values['start_date']."' AND LEFT(`res_submit_date`,10) <= '".$values['end_date']."' AND (number LIKE '%".$values['keyword']."%' OR source LIKE '%".$values['keyword']."%')");
$config['total_rows']=$query->num_rows();
$config['per_page'] = 2;
$this->pagination->initialize($config);
$where = array('LEFT(res_submit_date,10) >=' => $values['start_date'],'LEFT(res_submit_date,10) <=' =>$values['end_date']);
$this->db->where($where);
$this->db->where("(number LIKE '%".$values['keyword']."%' OR source LIKE '%".$values['keyword']."%')");
$res = $this->db->get('dlrReport', $config['per_page'], $this->uri->segment(3));
$dlr['details']= $res->result();
$dlr['start_date']=$values['start_date'];
$dlr['end_date']=$values['end_date'];
$dlr['keyword']=$values['keyword'];
$this->load->view('mysearch',$dlr);
Getting Below link in pagination links which is not working:
http://Host/project/reports/mysearch_search/?start_date=2014-01-23&end_date=2014-01-23&keyword=/2
Url i am expecting in pagination links is like:
http://Host/project/reports/mysearch_search/2?start_date=2014-01-23&end_date=2014-01-23&keyword=
How can i will Get right Url for searching Purpose?
Here is nice example from codeignter itself. Codeigniter Pagination check this.
By default codeigniter is expecting the pagination url like this.
http://localhost/project/reports/mysearch_search/2014-01-23/2014-01-23/2
$start_date =$this->uri->segment(5);
$end_date = $this->uri->segment(6);
$keyword = $this->uri->segment(7);
and if you need this kind of url then you have to change the
http://example.com/index.php?c=test&m=page&per_page=20
then you have to change to set this on config file
$config['enable_query_strings'] set to TRUE

Joomla : how to get the url of a specific Menu itemID?

Friends a newbie question.........I need help in getting the URL of a specific Menu itemID. The situation is like this:
I am running Joomla and asking for a user to input for a menu ID and choose a layout for that menu ID.
I want to do something else with this URL of the Menu itemID.
How can I get the URL of this Menu itemID provided by the user?
For Example if the user input is liek $this->get ('menulayoutid'>; and he inputs and ID of 54 then how do I get the URL for Menu ID 54.
Please note: I want to get this URL from within my PHP file and not in the browser so that I can use the value of that URL for some other purpose.
Kindly help.
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
Source: http://forum.joomla.org/viewtopic.php?p=1836005
However, we get the Itemid from anywhere (user input, from our own developed module using the "menu item" field type in the xml file as described in the Joomla Docs - Standard form field types)
// get the menuItemId from wherever...
// as described above or as in other posts here and do whatever with that!
$menuItemId = 'fromWherever'; // as an example "107";
// build the link to the menuItemId is just easy and simple
$url = JRoute::_('index.php?Itemid=' . $menuItemId);
i think if we need only a link to a specific menu id, this is the best solution, because we have absolutely less requests and a clean code
this works also in Joomla 3.0, 3.1
I just want to add that if you need to target a specific menu you pass the menu name as an argument to getMenu().
$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu( 'menu-name' );
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);
I'm not sure if Joomla changed the way this works since 2.5 or even 1.7 but I spent the worse half of 2 hours looking for this.
Hopefully it helps someone.
$menuID = $params->get('menuItem'); // from module field menu ex. '105'
$js = new JSite;
$menu = $js->getMenu();
$link = $menu->getItem($menuID)->route;
//Returns URL Friendly Link -> menu/article
//Then format it ->
$link = 'http://www.yoursite.com/index.php/'.$link;
echo 'Borrowed Menu Link Path";
When you need to get your active menu item ID in Joomla to display some specific content for only that menu item or just to show the ID of the menu item, insert the following code where you wish to display the active menu item ID:
<?php
$currentMenuId = JSite::getMenu()->getActive()->id;
echo $currentMenuId;
?>

Codeigniter Pagination - I'm stumped

Ok, I followed the instructions in the example perfectly. Ultimately, pagination works, kind of.
I get all of the pages listed: 1 | 2 | > | Last. Etc.
The first one is active, like it should be. I did the querying correctly as well, because each link will result in the correct query.
However, when I click on number 2, it will show me the next set of products correctly, but it will display the pagination from the first page.
Whatever pagination button I click on, will return the main pagination set: 1 (selected) | 2 | > | Last. It never changes! I'm loosing my patience, can someone help?
I think I might know whats going on. You need to tell the pagination library which segment of the URL holds the offset.
For example, if your URL is /products/browse/all/20, you need to tell CodeIgniter that the 4th segment holds the offset
$config['uri_segment'] = 4;
The default for the library is URL segment #3. If the offset in your URL is not in position 3 and you forget to tell the pagination library this, it will interpret the wrong segment as being the offset. This can lead to the kind of behaviour you describe above where the pagination does not appear to change.
I also came across same error and finally was able to fix it. Just thought to share the code script, may be someone will be able to use it.
=====> Controller
// Default function
function index()
{
// Display listing
$this->listing();
}
function listing($argDataArr = array())
{
// Initialize pagination
$pageArr['base_url'] = $this->config->item('categoryBeAction')."/listing";
$pageArr['total_rows'] = 15; //assume
$pageArr['per_page'] = 5; //assume
//You need to tell the pagination library which segment of the URL holds the offset.
$pageArr['uri_segment'] = 4; //URL eg: http://localhost/myproject/index.php/backend/category/listing/5
$this->pagination->initialize($pageArr);
// Get list of categories
// Create data array and pass data to get function
$dataArr['limitRows'] = $pageArr['per_page'];
$dataArr['limitOffset'] = $this->uri->segment(4); //the dynamic value from this segment will be used as offSet
$viewArr['listArr'] = $this->category_model->get($dataArr);
//rest of the code...
}
======> Model
function get($argDataArr = array())
{
//Select the fields required
$this->db->select('id, name, parent_id, status');
$this->db->from($this->config->item('tbl_category','dbtables'));
$this->db->where('parent_id', $parentId);
$this->db->limit($argDataArr['limitRows'], $argDataArr['limitOffset']);
$this->db->order_by("name", "asc");
$query_result = $this->db->get();
return $query_result;
}
======> View page
<!-- Pagination -->
<tr>
<td align="right">
<?php echo $this->pagination->create_links(); ?>
</td>
</tr>
Which example?
echo $this->pagination->create_links();
^^Is this in your view?

codeigniter pagination for image

I want to paginate query results in CodeIgniter to look like this:
Problem 1 :
The Pagination class always outputs numeric links. I just want to show next and back links.
Problem 2 :
$data['links'] = $this->pagination->create_links(); returns all of the links as a string. How can I separate the next and back links and put next to the right and back to the left ?
suppose url is: http://localhost/controller/method/
do following in your controller function
...
function method($page_num)
{
...
$data['next_link'] = $page_num + 1;
$data['prev_link'] = $page_num;
...
$this->load->view('<veiw_name>', $data);
}
do this in your view
...
Prev
Next
....

Categories