Jump to content

cycle through rows


Winston_Smith

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.