Winston_Smith Posted August 18, 2008 Share Posted August 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120140-cycle-through-rows/ Share on other sites More sharing options...
.josh Posted August 18, 2008 Share Posted August 18, 2008 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-- Quote Link to comment https://forums.phpfreaks.com/topic/120140-cycle-through-rows/#findComment-618957 Share on other sites More sharing options...
Winston_Smith Posted August 18, 2008 Author Share Posted August 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/120140-cycle-through-rows/#findComment-618967 Share on other sites More sharing options...
.josh Posted August 18, 2008 Share Posted August 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120140-cycle-through-rows/#findComment-618975 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.