Is there a way to open directly the page in which a record appears?
Example:
function index($id){
$this->paginate = array(
'limit'=>12,
'page' => ????
);
$items = $this->Item->paginate();
/**
How can i calculate the page number, by the $id?
*/
}
If you got the ID, and you are auto_incrementing the ID in your database, you can count row before this ID:
$nbRow = $this->Item->find('count', array('conditions' => array('id <=' => $id))) ;
Then to find the page, you just have to divide (int division) by the number of item per page:
$page = ceil($nbRow / 12) ; // 12 or whatever item per page you have
Related
In my WordPress v5.8.1 I have multiple authors, I am building a new comments navigation view for each author.
There are tens of comments for each authors post, but below code I am not able to get the comments count for the respective authors posts:
function user_nav_counts($views) {
global $current_user, $wp_query;
unset($views['mine']);
unset($views['approved']);
unset($views['moderated']);
unset($views['spam']);
unset($views['trash']);
$author_ID = $current_user->ID;
$types = array(
array('status' => 'approved'),
array('status' => 'moderated'),
array('status' => 'trash')
);
foreach ($types as $type) {
$query = array(
'status' => $type['status'],
'type' => 'comment',
'post_author' => $author_ID,
'post_type' => array('song', 'book'),
'count' => true,
);
$result = new WP_Comment_Query($query);
if ($type['status'] == 'approved'):
$class = ($wp_query->query_vars['comment_approved'] == 'approved') ? ' class="current"' : '';
$views['approved'] = sprintf(__('(<span class="approved-count">%d</span>)</span>', 'approved'), ('wp-admin/edit-comments.php?comment_status=approved'), $result->count);
elseif ($type['status'] == 'moderated'):
$class = ($wp_query->query_vars['comment_moderated'] == 'moderated') ? ' class="current"' : '';
$views['moderated'] = sprintf(__('(<span class="moderated-count">%d</span>)</span>', 'moderated'), ('wp-admin/edit-comments.php?comment_status=moderated'), $result->count);
elseif ($type['status'] == 'trash'):
$class = ($wp_query->query_vars['comment_trash'] == 'trash') ? ' class="current"' : '';
$views['trash'] = sprintf(__('(<span class="trash-count">%d</span>)</span>', 'trash'), ('wp-admin/edit-comments.php?comment_status=trash'), $result->count);
endif;
}
return $views;
}
if (!current_user_can('edit_others_posts')) {
add_filter('views_edit-comments', 'user_nav_counts', 10, 1);
}
With the above code, I am able to build the new nav but the count ($result->count) is always zero.
Even in the var_dump($result), the public 'found_comments' => int 0 is also zero.
How can I get the count of each authors comments count?
the count ($result->count) is always zero
WP_Comment_Query doesn't have a property named $count, and the above call would actually cause PHP to throw a notice saying "Undefined property: WP_Comment_Query::$count".
Even in the var_dump($result), the public 'found_comments' => int 0 is also zero.
That is because the no_found_rows arg defaults to true, which means SQL_CALC_FOUND_ROWS is disabled by default.
But you should not enable it if you set count to true.
How can I get the count of each authors comments count?
Just initialize WP_Comment_Query without passing any query args and then use the query() method to get the total comments count, like so:
$comment_query = new WP_Comment_Query;
$count = $comment_query->query( $query );
But if count is not set (to true), then you would want to set no_found_rows to true and number to a non-zero integer. Example:
$query = array(
// ... your args here.
'count' => false, // 1. Don't set or set to false
'no_found_rows' => false, // 2. Set to false (default is true)
'number' => 5, // 3. Set to a non-zero value
);
$comment_query = new WP_Comment_Query( $query );
// total for the current results set, e.g. on page 1
$count = count( $comment_query->comments );
// total for all pages, same as when LIMIT is not set
$total = $comment_query->found_comments;
So you would want to use the above if you want to paginate the comments, but if you simply want the total comments, then use the count arg instead.
Edit: However, you could also simply use get_comments() like so: $views['approved'] = get_comments( $query ); if you don't need to access anything else in the class instance.
Note about the status arg
approved and moderated are not valid values for the status arg (which references the comment_approved column in the comments table), and the default statuses included in WordPress core are (see get_comment_count()):
Name
status value
Description
Pending
hold or 0
Comment is awaiting moderation
Approved
approve or 1
Comment is approved
Spam
spam
Comment is marked as spam
Trash
trash
Comment is trashed
Post Trashed
post-trashed
The comment's post is trashed
PS: approve and hold are not the actual database value, but WP_Comment_Query accepts those values in place of 1 and 0 respectively.
I think in your function you can try use something like this:
$result = new WP_Comment_Query($query);
....
$views['counts'] = count($result->get_comments());
The more details is here
I list of tasks. Everyone task has Id in DB. Id column has setted up autoincrement.
I made this list sortable via jquery by sortable() function from jquery ui.
Now if I change order of times I change order of Id in DB. But bcz of UPDATE does not work when values of Id are same - I need to change ordering one time from 200 and second time from 0. Is it good solution? Ans is it okay to change Id or shoula I create another column for this purpose?
Thanks
$i = 0;
// load all data from db in ascending order bcz of find first id value
$first_index = $database -> select (
'items', // table
'id', // column
[ 'ORDER' => ['id' => 'ASC' ] ]// ordering
);
// if first id value is equal to 0 then set up i as 1000 bcz if there are same values UPDATE table below will not work
if ( $first_index[0] == 0 ) {
$i = 200;
} else if ( $first_index[0] == 200 ) {
$i = 0;
};
// convert array (item[]=1&item[]=2) to separate items
foreach ($_POST['item'] as $value) {
// Execute statement:
$affected = $database -> update ('items',
[ 'id' => $i ], // SET (set id as $i which is increasing for each value)
[ 'id' => $value ] // WHERE
);
$i++;
}
I have a Job model and a Visit model, a Job can have many Visit's. I have set up the relationships appropriately in my Models.
A Visit also belongs to a Supplier, so a Supplier can have many visits.
A subcontractor is a Supplier who has an engineer value of 0 in the database.
I want to return an associative array with the amount of Jobs raised per subcontractor, something like this:
$supplier_job_count = array(
'Subcontractor1' => 23,
'Subcontractor2' => 3,
'Subcontractor3' => 7,
'Subcontractor4'=> 0
);
Here is what I have:
$conditions = array();
// get all the visits in the database
$visits = $this->Job->Visit->find('all');
// extract all the visit id's
$visit_supplier_ids = Hash::extract($visits, '{n}.Visit.supplier_id'); // 11612 records
// get all the subcontractors in the database
$subcontractors = $this->Job->Visit->Supplier->find('all', array('order' => 'id ASC', 'conditions' => array('Supplier.engineer' => 0)));
// extract all the subcontractor id's
$subcontractor_ids = Hash::extract($subcontractors, '{n}.Supplier.id'); // 1288 records
// intersect arrays so we only want matching values from both, i.e. only want visits whose supplier is a subcontractor
$visit_subcontractors = array_values(array_intersect($visit_supplier_ids, $subcontractor_ids));
// find all visits who is a subcontractor id
$visits = $this->Job->Visit->find('all', array('conditions' => array('Visit.supplier_id' => $visit_subcontractors)));
// extract out the job id's for the subcontractor visits
$visit_jobs_ids = Hash::extract($visits, '{n}.Visit.job_id');
// pass the job id's to the conditions for the jobs
$conditions['Job.id'] = $visit_jobs_ids;
// get all subcontractor jobs
$subcontractor_jobs = $this->Job->find('all', array('conditions' => $conditions));
$subcontractor_job_count = array();
foreach ($subcontractors as $key => $value) {
// this is where I am getting stuck
$subcontractor_job_count[$value['Supplier.name']] = ));
}
I am sort of half way there, just struggling to get a count of how many jobs raised per subcontractor, any ideas?
I am using yii YiinfiniteScroller. There are 260 records. i have displayed 12 records per page. Initially it loads first 12 records correctly. when i scroll the page, it loads the same 12 records (duplicate) And it repeats continuously when i scrolling only 12 records are repeated every time
Here is the code for view page
<?php
$this->widget('ext.yiinfinite-scroll.YiinfiniteScroller', array(
'contentSelector' => '#Load_addcont',
'itemSelector' => 'ul.wlist',
'loadingText' => 'Loading next 12 rows...',
'donetext' => 'Loading finish.',
'pages' => $pages,
));
?>
Here is the code for controller site
$pages = new CPagination($row_count);
$pages->setPageSize(12);
$pages->applyLimit($criteria);
$Wineries = Wineries::model()->findAll($criteria);
$flags = array();
Yii::log("Pagination Wineries row_count = $row_count page # = " . $pages->getCurrentPage(), 'error', 'actionSelectwineries.end');
Yii::log("Pagination Wineries GET = " . CVarDumper::dumpAsString($_GET), 'error', 'actionSelectwineries.end');
What is the problem? why same records are repeating? Pls help me if you know
I have reached very strange problem using Zend Framework 1 and binding the resultset to BvbGrid.
We have a method which queries an external API. You need to specify the following params: methodName, params.
So the controller recieves a resultset as an array from the external API.
The resultset (in the database) is 4 columns - 1 string, 2 DateTime, 3 int, 4 int. And is with about 3000 rows.
When I recieve the resultset in the controller I bind it to Bvb Grid, which should evaluate in the view as columns with search boxes.
So far, so good, when I search in the first or in the last column (string, int), it searches in the whole resultset.
But when I search 2, 3 (DateTime, int) it searches only in the current page.
I don't understand why is that. There is no difference in data binding, or something.
The code is as follows
public function indexAction() {
$pagesize = $this->_request->getPost('rows');
$pagenum = $this->_request->getPost('page');
$pagesize = ($pagesize) ? $pagesize : 30;
$pagenum = ($pagenum) ? $pagenum : 1;
$params = array();
$params['page_num'] = $pagenum;
$params['page_limit'] = $pagesize;
if ($this->_request->isXmlHttpRequest()) {
// additional search filter
if ($this->_request->getPost('username')) {
$params['username'] = $this->_request->getPost('username');
}
if ($this->_request->getPost('lastlogin')) {
$params['lastlogin'] = $this->_request->getPost('lastlogin');
}
if ($this->_request->getPost('type')) {
$params['type'] = $this->_request->getPost('type');
}
if ($this->_request->getPost('level')) {
$params['level'] = $this->_request->getPost('level');
}
}
if (($order_by = $this->_request->getPost('sidx')) && ($order_type = $this->_request->getPost('sord'))) {
$params['order_by'] = $order_by;
$params['order_by_type'] = $order_type;
}
$resultset = $this->web_service->_apiRequest('SearchUsers', $params);
/**
* The resultset is like
* array(
'found_rows' => 3000,
'body' => array(
'username' => 'blabla',
'lastlogin' => '2014-02-25 13:33:38.1234',
'type' => 1,
'level' => 199
)
);
*/
$this->view->count = $resultset['found_rows'];
if ($resultset['found_rows'] > 0 || $this->_request->isXmlHttpRequest()) {
$grid = new Bvb_Grid_Deploy_JqGrid($this->_gridconfig);
$grid->setSource(new Bvb_Grid_Source_Array($resultset['body']));
$grid->getSource()->setTotalRecords($resultset['found_rows']);
$grid->updateColumn('username', array('width' => '110',));
$grid->updateColumn('lastlogin', array('width' => '110',));
$grid->updateColumn('type', array('width' => '110',));
$grid->updateColumn('level', array('width' => '110',));
$grid->setJqgParams(array('caption' => 'resultset'
, 'forceFit' => true
, 'viewrecords' => true
, 'rowList' => array(15, 30, 50)
, 'altRows' => false
)
);
$grid->ajax(get_class($grid));
$grid->setNumberRecordsPerPage(30);
$grid->setExport(array());
$this->view->grid = $grid->deploy();
}
I have tried to examine the BvbGrid's code, but found nothing strange.
In another project the same code is used, and it works fine, without any problems on searching in any column. I have dump'd the API response to be sure it gives me 3000 records, and it actually is and they are in the way as in the comment. I really cannot find the reason why i.e. the lastlogin column searches only on the current page.
If someone can help me, or give me directions where to look at to fix the issue (maybe somewhere in the BvbGrid's code?) I will appreciate it.
Thank you in advance.