0

I am trying to find the position/index of the current record within a group of records for display in a view. So for instance, I may have 10 records in the database, and the user may be viewing record #5 out of 10. So I want to be able to display this info on the view.ctp: You are viewing record# 5 of 10 total records.

I already know how to get the count of records as follows:

$total_record_count = $this->TblAsset->find('count');

I don't need to know the $id of the record, I already have that value passed in via the URL as the Primary Key value for the record.

3 Answers 3

1

If you're fetching the records one by one, there's no standard definition of what "number" the record is. If you have all the records in an array you can use the current record's index. Otherwise you have to count the ordinal by whatever method you use to order the records. For example you could count the number of records that have earlier created date than the current record and add 1 to the result.

3
  • Actually the info would be in an array, as placed there by the Paginator. I guess I need to know a couple of things. If I have the primary key value for the current record, how can I look up its array index? And then if I had that info, maybe I could add 1 to the value (0 indexed) and multiply by the current page# in use by the paginator with the number of records per page. Sep 21, 2011 at 16:36
  • I'd go with that strategy. Use a foreach( $array as $key => $value ), when you find the correct $value[ 'whatever' ] you have the index in $key.
    – JJJ
    Sep 21, 2011 at 17:22
  • Thanks for your suggestions. I now understand the process better. I ended up doing something like this: // get list of keys for display of record position $record_position = ''; $conditions = array(); $record_position_query = $this->TblAsset->find('list', array('fields' => 'TblAsset.id')); $record_position_list = array_keys($record_position_query); //resolve all keys to numeric array $record_position = array_search($id,$record_position_list) + 1; But recently I have had second thoughts that maybe I should do something like this within the Model as a behavior. Sep 23, 2011 at 22:11
0

Are you using the built in CakePHP pagination? It sounds like you should be, it also has a convenient helper for easily displaying "showing X records out of Y".

It does all the necessary queries for you, so you won't need to do any manual counting queries yourself. I recommend you look into it.

5
  • Yes, I am using the Paginator. The user has clicked on a record in the paged list of records to get to this view.ctp displayed record. But I also allow the users to hit a Next/Previous/First/Last button to move around in the group of records. I looked at the helper, but it is just showing X records of Y. I don't know which record of X records is currently being viewed. Sep 21, 2011 at 16:39
  • I don't want IDs (as in primary key values), I need the index of the array entry for the current record. Like you see when you use debug, you will see the index value within [] brackets. From there, I am hoping to be able to calculate the actual record position based upon which page of the paginator is being displayed. Maybe there is an easy way to lookup the index for a record based upon its ID? Sep 21, 2011 at 18:41
  • Read the second link I posted, specifically look at the %start% and %end% tokens at the top. If that isn't what you need, update your question with an example with the array result you want.
    – Dunhamzzz
    Sep 21, 2011 at 18:49
  • %start% is the number of the first record, %end% is the number of the last record. But I need %current_record% - which doesn't exist. %current% is only the page number, not record number. I actually don't even need an array, just a single numeric value. Thanks for your patience and answers. Sep 21, 2011 at 20:20
  • %current_record% will just be the array of data paginate returns? Edit your question with your desired result to make it clear
    – Dunhamzzz
    Sep 22, 2011 at 7:54
0

I found that the easiest solution for this is to add an index which you increment and add the current page -1 * number of results/page

Example: I have a query with a limit of 10 I can hardcode it or add another variable

$paginatorParams = $this->Paginator->params(); // which returns an array with 'limit' as  an index

so I will have:

$index = 0;
$paginatorParams = $this->Paginator->params();
foreach($records as $record):
   $index++;
   echo $index + ( ((int)$this->Paginator->counter('{:page}') - 1) * $paginatorParams['limit'] );
endforeach;

So on page 1 you will have result 1 + ((page 1 - 1)*10) = 1
And on page 2, and so on you will have result 1 + ((page 2 - 1)*10) = 11

Also on my opinion it should have a function which returns the record number, so you could edit the Paginator Helper, or use my example in your view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.