Jump to content

Recommended Posts

Hi,

 

I was wondering if there is a way to determine a specific entry's number (location) within it's table? Not an auto increment field, as entries may be deleted. I want to be able to display to the user the current record they are on, i.e. "Viewing record 8 out of 37"

 

Is there a mysql function that can do this?

 

Thanks for your help!

Link to comment
https://forums.phpfreaks.com/topic/3183-find-entry-number-within-a-table/
Share on other sites

OK

I think I have an idea, but pay close attention its sort of complicated.

 

Setup an auto_increment field, not to tell what number row it is, but just to serve as a unique value to the row.

 

Then, when you pull a row from the database, to find out what row it is, you would find out how many rows there are up to that record.

 

So, you would run another query as follows

$number_record=mysql_num_rows(mysql_query("SELECT * FROM `table` WHERE id<=$row_id"));

 

the id field is the auto_increment, andthe $row_id variable needs to be populated with the id from the row you are pulling.

 

Hope this helps you

 

Chris

Well, if you know that there are 37 -- because you know how many rows were returned -- then it should be easy to keep a counter in PHP as you iterate though the result set. However, if you want it to come back from the DB, you'll have to use a server variable, set it to 0 before you issue your select query, and have one of the columns in the SELECT statement return the value and increment the server variable.

 

Hope that helps.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.