Joomla Pagination Error - joomla

$limit = $this->_app->getUserStateFromRequest('global.list.limit', 'limit', $this->_app->getCfg('list_limit'), 'int');
$limitstart = $this->_app->getUserStateFromRequest(OPTIOIN_NAME.'.limitstart', 'limitstart', 0, 'int');
$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0); // In case limit has been changed
EG : PAGES 1 -> Set number of records be shown to 10
, When I go to PAGES 2 -> Pagination is automatically set 10. I want to have different pagination settings for different pages.
What it does is, it keep a global variable. so even when I moved to another page those pagination settings are saved and applied. if I change OPTIOIN_NAME.'.limitstart' or 'global.list.limit' to something else it gives me Warning: Attempt to assign property of non-object in D:\wamp\www\jink\libraries\joomla\registry\registry.php on line 342. How can I fix this without breaking the code.
Thanks

function __construct()
{
$this->_app =& JFactory::getApplication();
parent::__construct();
// Get pagination request variables
$limit = $this->_app->getUserStateFromRequest(OPTIOIN_NAME.'.bs.limit', 'limit', $this->_app->getCfg('list_limit'), 'int');
$limitstart = $this->_app->getUserStateFromRequest(OPTIOIN_NAME.'.bs.limitstart', 'limitstart', 0, 'int');
// In case limit has been changed, adjust it
$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
// Set States
$this->setState(OPTIOIN_NAME.'.bs.limit', $limit);
$this->setState(OPTIOIN_NAME.'.bs.limitstart', $limitstart);
}
function pagination()
{
if($this->_pagination == NULL)
$this->_pagination = new JPagination(20, $this->getState(OPTIOIN_NAME.'.bs.limitstart'), $this->getState(OPTIOIN_NAME.'.bs.limit'));
return $this->_pagination;
}
In the constructor get the values and set it to the session. pagination function should be called in the controller.
In the getList or whatever the function you wish to limit the rows
// Set the Limits and Filters
$limit = $this->getState(OPTIOIN_NAME.'.bs.limit');
$limitstart = $this->getState(OPTIOIN_NAME.'.bs.limitstart');
//setup the pagination
$this->_pagination = new JPagination($total, $limitstart, $limit);
//get the data within limits
$this->_data = $this->_getList($query, $limitstart, $limit);
//$total is the total number of rows return by the count(*) query.

Related

Get object value from array in cache laravel

I have an array in Redis cache like that
127.0.0.1:6379> MGET laravel:campaign1107
1) "a:1:{s:21:\"unsubscriberCount1107\";i:2;}"
127.0.0.1:6379>
Now I need to get unsubscriber1107 value. I tried to this way
dd(cache()->get($arrayCacheKey[$cacheKey]));
but, it's doesn't work. How can I access this object?
My code for set cache
public function updateUnsubscriberCountCache($campaign_id, $type)
{
$assoc = [];
$arrayCacheKey = 'campaign'.$campaign_id.'';
$cacheKey = 'unsubscriberCount'.$campaign_id.'';
if($type == 'unsubscriberLogCount') {
$cacheKey = 'unsubscriberCount'.$campaign_id.'';
if( cache()->get($cacheKey) > 0) {
cache()->increment($cacheKey);
//cache()->forget($cacheKey);
} else {
$total = UnsubscribeLog::select('unsubscribe_logs.*')->leftJoin('tracking_logs', 'tracking_logs.message_id', '=', 'unsubscribe_logs.message_id')->where('tracking_logs.campaign_id', '=', $campaign_id)->distinct('subscriber_id')->count('subscriber_id');
//cache()->forever($cacheKey, $total);
$assoc[$cacheKey] = $total;
cache()->forever($arrayCacheKey, $assoc);
}
}
}
You're storing the value as an array using $arrayCacheKey but earlier in the code you're trying to access it using the $cacheKey which has a different value.
If you want to get the value of unsubscriber1107 you will need to use a combination of both keys:
$campaignData = cache()->get($arrayCacheKey); //To get the array value from the cache
$count = $campaignData ? $campaignData[$cacheKey] : null; //get the count
The above assumes that the va

Laravel 5.4 LengthAwarePaginator

My brain suddenly crashed on this one. Anyone care to help me is highly appreciated.
This is LengthAwarepaginator in laravel 5.4
Here is the code.
$collection = [];
foreach ($maincategories->merchantCategory as $merchantCat) {
foreach ($merchantCat->merchantSubcategory as $merchantSub) {
foreach($merchantSub->products as $products){
$collection[] = $products;
}
}
}
$paginate = new LengthAwarePaginator($collection, count($collection), 10, 1, ['path'=>url('api/products')]);
dd($paginate);
It displays perfectly but the problem is the items is 100. That's all my items and I specify it correctly. I need to display only 10.
Base on LengthAwarePaginator constructor. Here is the reference.
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
Here is the screen shot.
Where did I go wrong? TY
When manually creating a paginator, you have to slice the result set yourself. The first parameter to the paginator should be the desired page of results, not the entire result set.
From the pagination documentation:
When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. If you're unsure how to do this, check out the array_slice PHP function.
I would suggest using a Collection to help out with this a little:
// ...
$collection = collect($collection);
$page = 1;
$perPage = 10;
$paginate = new LengthAwarePaginator(
$collection->forPage($page, $perPage),
$collection->count(),
$perPage,
$page,
['path' => url('api/products')]
);
For the heavy select and to avoid any multiple select to calculate the total from the table we unable to use model paginate
use Illuminate\Pagination\LengthAwarePaginator;
in your controller function
if(!isset($input["total"])){ //initial request
$total = //Find the total only one time.
$request->request->add(['total' => $total]); //Add it in the request so that we can print it in blade
}else
$total = $input["total"]; //After initial request
$currentPage = LengthAwarePaginator::resolveCurrentPage(); //page variable from GET or POST
$perPage = 30; //Page Length
$offset = ($currentPage - 1) * $perPage; //find the offset to pass in query
$specificRecords = /*Collect specific page records in array
if mysql then Select * from table limit $perPage offset $offset
if ms sql then OFFSET {$offset} ROWS FETCH NEXT {$perPage} ROWS ONLY */
$records = new LengthAwarePaginator($specificRecords,$total,$perPage,Null,[ "path" => "/pagepath" ]);
in blade:
<center>{{$records->appends(Request::except([ 'page','_token' ]))->links()}}</center>
Check Page and total variable in page tags ensure you added page in except list :)

Pagination with array not working in laravel 5.1

I have to set pagination in array result.
Here is my code.
My Controller code
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
public function getCVList(){
.
.
.
$jobseeker1 = array_merge($jobseekers, $apps_array);
// in $jobseeker1 there are 6 result.
$jobseeker = $this->paginate($jobseeker1, 3);
return view('frontend.CVList', compact('jobseeker'));
}
public function paginate($items, $perPage, $pageStart = 1) {
$offSet = ($pageStart * $perPage) - $perPage;
// Get only the items you need using array_slice
$itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage, Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
In blade temoplate i used rander() method and thare are also display pagination. But in all page display same record.
Thanks....
This is because you are not reading the page number clicked in the paginator, you are setting "3" always as the page to display. Try this:
//include the request
use Illuminate\Http\Request;
Now, read the current page:
public function getCVList(Request $request){
$perPage = 3;
// read the page number. When page number is not presented, then you
// set it as 0
$page = $request->get('page', 0);
$page = ($page == 0)? ($page * $perPage) : ($page * $perPage) - $perPage;
// now, calling the paginator do magic dynamically
$jobseeker = $this->paginate($jobseeker1, $perPage, $page);

Codeigniter pagination is not passing the correct link value in url

I have created a pagination in codeigniter, In that I have totally 13 records and i want to display 5 records in each page. When i click on the pagination link number 2,It should pass value 2 in url and it show the next 5 records but I am getting value 5 in url instead of 2.
My controller code:
public function activities($page_num = 1){
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num))
$page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);
$data['activity_list'] = $this->skill_model->all_activities(1,$limit_start,$limit_end);
}
This is my view code:
<?php echo '<div class="pagination" style="float:right;">'.$this->pagination->create_links().'</div>'; ?>
My Model part:
function all_activities($flag,$limit_start,$limit_end){
$this->db->select('*');
$this->db->from('skills_activities');
//echo $limit_start." ".$limit_end;
if($flag == 1 ){
$this->db->limit($limit_start, $limit_end);
$query = $this->db->get();
return $query->result_array();
} else {
$query = $this->db->get();
return $query->num_rows();
}
}
When i click on pagination link I am getting the following url:
http://192.168.1.97/projects/homecare/admin/skills/activities/5
but I should get:
http://192.168.1.97/projects/homecare/admin/skills/activities/2
I don't know where i have done a mistake.
can anybody help me?
Thanks in advance.
$config['use_page_numbers'] = true;
This will produce page number in the url instead of record number. By default its false.
So you controller will look like this
public function activities($page_num = 1)
{
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['use_page_numbers'] = true;//you missed this line
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num)) $page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);

pagination doesn't work in codeigniter

I have successfully created pagination on some of the pages on the application on which I am working with, but I can't make it on this one:
I have 7 records in the database, and when
page is displayed all 7 records are displayed instead of 5, as I would like to be.
Sure enough, links for the paging are not displayed.
Here is my controller code:
public function displayAllFaqCategories()
{
//initializing & configuring paging
$currentUser = $this->isLoggedIn();
$this->load->model('faqCategoriesModel');
$this->db->order_by('sorder');
$limit = 5;
$offset = 3;
$offset = $this->uri->segment(3);
$this->db->limit(5, $offset);
$data['faq_categories'] = $this->faqCategoriesModel->selectCategoriesAndParents();
$totalresults = $this->db->get('faq_categories')->num_rows();
//initializing & configuring paging
$this->load->library('pagination');
$config['base_url'] = site_url('/backOfficeUsers/faqcategories');
$config['total_rows'] = $totalresults;
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$errorMessage = '';
$data['main_content'] = 'faq/faqcategories';
$data['title'] = 'FAQ Categories';
$this->load->vars($data,$errorMessage);
$this->load->vars($currentUser);
$this->load->view('backOffice/template');
} // end of function displayAllFaqCategories
And here is my model function code:
public function selectCategoriesAndParents($selectWhat = array())
{
$data = array();
$query = $this->db->query("SELECT fq . * , COALESCE( fqp.$this->parent_name, '0' ) AS parentname
FROM $this->table_name AS fq
LEFT OUTER JOIN $this->table_name AS fqp ON fqp.catid = fq.parentid");
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
} // end of function selectCategoriesAndParents
In the view, bellow of the table with the records I have the following code:
<?php echo $this->pagination->create_links();?>
Any help will be deeply appreciated.
Regards,Zoran
You've mixed two different things together I think. You're partially using the ActiveRecord class of CI, but then running the query yourself.
The simplest change would be:
// get all the rows
$data['faq_categories'] = $this->faqCategoriesModel->selectCategoriesAndParents();
// figure out the count of all of them
$totalresults = count($data['faq_categories']);
// only take some of the rows of the array, instead of keeping all of them and then showing all 7 of your records
$data['faq_categories'] = array_splice($data['faq_categories'], $offset, $limit);
Hopefully that should fix it!
To further explain what the original problem is, I think when you run this:
$totalresults = $this->db->get('faq_categories')->num_rows();
It takes the previous line $this->db->limit(5, $offset); into account, so it only returns 5 rows. Then, when you tell the pagination library that you only want to show 5 per page, the library thinks that it is actually showing all the results, so there is no need for pagination links!
Edit like this
$offset = $this->uri->segment(3) ? $this->uri->segment(3) : 0;

Resources