Jump to content

Recommended Posts

I'm looking to search a db and select all rows matching a variable ($town), then cycle through the rows one at a time. What i'm looking to do is display a row on a page, then have the user click next and it displays the next row that matches and so on till all rows matching are run through. I only want one row to be printed at a time, one for each page.

 

I already have a php page written that allows a search by last name, town, town and last name or to display all rows in the db. I've been trying to figure out how to do this since last night after I got my last problem worked out.

 

Link to comment
https://forums.phpfreaks.com/topic/120140-cycle-through-rows/
Share on other sites

Dunno if it's the most effective way, but assuming that each row has a unique id, I would first do a query that returns all of the id's of the matching rows, and keep that as a session array ($rowIds), and have another session var that's holds the current array position of the current row's id ($currentPos).  User wants to press the next row button, $currentPos++, select query where id = $rowIds[$currentPos].  Previous button same thing, except $currentPos--

Link to comment
https://forums.phpfreaks.com/topic/120140-cycle-through-rows/#findComment-618957
Share on other sites

Dunno if it's the most effective way, but assuming that each row has a unique id, I would first do a query that returns all of the id's of the matching rows, and keep that as a session array ($rowIds), and have another session var that's holds the current array position of the current row's id ($currentPos).  User wants to press the next row button, $currentPos++, select query where id = $rowIds[$currentPos].  Previous button same thing, except $currentPos--

Yes the table has and auto incrementing id, so that should work then.

 

Question, I have a delete function setup, but when it deletes a row the other rows still hold the same id value. Does this even matter or should I somehow reassign the id's? like say I have rows that have the id's 1234 and I delete the row with id 2 now I have 134, does this effect anything?

Link to comment
https://forums.phpfreaks.com/topic/120140-cycle-through-rows/#findComment-618967
Share on other sites

No, that doesn't really affect anything, as long as you're not designing your code to depend on the numbers like that.  For instance, retrieving results and then trying to make a for loop to cycle through it, instead of using a foreach or while loop.  If for some reason it makes you sleep better at night to have them like that,  There is no specific "reset" command (AFAIK).  AFAIK you have to drop the column and then add it back and it will then auto-inc them 1-x. 

Link to comment
https://forums.phpfreaks.com/topic/120140-cycle-through-rows/#findComment-618975
Share on other sites

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.